Prevented "write after end" SVR.JS crashes (again)

This commit is contained in:
Dorian Niemiec 2024-02-21 01:36:39 +01:00
parent 6e882fdbc8
commit f3fb1f928a

View file

@ -196,7 +196,7 @@ function createFastCGIHandler(options) {
err = new Error("Multiplexed connections not supported by the FastCGI application"); err = new Error("Multiplexed connections not supported by the FastCGI application");
} }
emulatedStdout.push(null); 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() { emulatedStdout.on("end", function() {
emulatedStderr.push(null); emulatedStderr.push(null);
eventEmitter.emit("error", err); eventEmitter.emit("error", err);
@ -207,7 +207,7 @@ function createFastCGIHandler(options) {
} }
} else { } else {
emulatedStdout.push(null); 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() { emulatedStdout.on("end", function() {
emulatedStderr.push(null); emulatedStderr.push(null);
eventEmitter.emit("exit", appStatus, 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() { function handlerConnection() {
handler.init(); handler.init();
handler.stdout.on("data", dataHandler); handler.stdout.on("data", dataHandler);
@ -493,7 +497,7 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
serverconsole.errmessage(preparedStderr); serverconsole.errmessage(preparedStderr);
} }
handler.stdout.unpipe(res); //Prevent server crashes with write after the end handler.stdout.unpipe(res); //Prevent server crashes with write after the end
res.end(); if(!res.finished) res.end();
} }
}); });
} }