From 8c9b2ce68cf2c4768f1047be76445a31fc7c25a3 Mon Sep 17 00:00:00 2001 From: David Kamholz Date: Sun, 26 Oct 2014 15:56:34 -0700 Subject: [PATCH 1/2] send a response if the cgi script failed --- cgi.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cgi.js b/cgi.js index c9adb9a..50988c4 100644 --- a/cgi.js +++ b/cgi.js @@ -156,6 +156,11 @@ function cgi(cgiBin, options) { debug('cgi spawn %d stdout "end" event', cgiSpawn.pid); if (cgiResult) { cgiResult.cleanup(); + if (!res.headersSent) { + res.writeHead(500, { 'Content-type': 'text/plain' }); + res.write('Internal Server Error'); + res.end(); + } } if (onData) { options.stderr.removeListener('data', onData); From acd454a0c629566fb85d148369833161f70bf076 Mon Sep 17 00:00:00 2001 From: David Kamholz Date: Mon, 5 Jan 2015 11:44:48 -0800 Subject: [PATCH 2/2] add timeout option to kill hung scripts --- cgi.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cgi.js b/cgi.js index 50988c4..c634d60 100644 --- a/cgi.js +++ b/cgi.js @@ -105,6 +105,14 @@ function cgi(cgiBin, options) { var cgiSpawn = spawn(cgiBin, options.args, options); debug('cgi spawn (pid: %d)', cgiSpawn.pid); + var exited = false; + + if (options.timeout) { + setTimeout(function() { + if (!exited) cgiSpawn.kill(); + }, options.timeout * 1000); + } + // The request body is piped to 'stdin' of the CGI spawn req.pipe(cgiSpawn.stdin); @@ -148,6 +156,7 @@ function cgi(cgiBin, options) { cgiSpawn.on('exit', function(code, signal) { debug('cgi spawn %d "exit" event (code %s) (signal %s)', cgiSpawn.pid, code, signal); + exited = true; // TODO: react on a failure status code (dump stderr to the response?) });