diff --git a/index.js b/index.js index f23a13f..90d9e59 100644 --- a/index.js +++ b/index.js @@ -196,7 +196,7 @@ function createFastCGIHandler(options) { err = new Error("Multiplexed connections not supported by the FastCGI application"); } emulatedStdout.push(null); - if (emulatedStdout._readableState && emulatedStdout._readableState.length > 0 && emulatedStdout._readableState.flowing !== null && !emulatedStdout.endEmitted) { + if (emulatedStdout._readableState && emulatedStdout._readableState.flowing !== null && !emulatedStdout.endEmitted) { emulatedStdout.on("end", function() { emulatedStderr.push(null); eventEmitter.emit("error", err); @@ -207,7 +207,7 @@ function createFastCGIHandler(options) { } } else { emulatedStdout.push(null); - if (emulatedStdout._readableState && emulatedStdout._readableState.length > 0 && emulatedStdout._readableState.flowing !== null && !emulatedStdout.endEmitted) { + if (emulatedStdout._readableState && emulatedStdout._readableState.flowing !== null && !emulatedStdout.endEmitted) { emulatedStdout.on("end", function() { emulatedStderr.push(null); eventEmitter.emit("exit", appStatus, null); @@ -470,6 +470,10 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e } }); + res.on("close", function() { + if(handler.stdout) handler.stdout.unpipe(res); //Prevent server crashes with write after the end + }); + function handlerConnection() { handler.init(); handler.stdout.on("data", dataHandler); @@ -493,7 +497,7 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e serverconsole.errmessage(preparedStderr); } handler.stdout.unpipe(res); //Prevent server crashes with write after the end - res.end(); + if(!res.finished) res.end(); } }); }