Fix "write after end" server crashes

This commit is contained in:
Dorian Niemiec 2024-03-28 07:21:02 +01:00
parent 7307229e90
commit 3d5d57b7b4

View file

@ -297,9 +297,17 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
res.write(data);
interpreter.stdout.removeListener("data", dataHandler);
interpreter.stdout.pipe(res, {end: false});
dataHandler = function () {}; //Prevent event listener memory leaks
}
}
};
res.prependListener("close", function() {
if(handler.stdout) handler.stdout.unpipe(res); //Prevent server crashes with write after the end
});
res.on("error", function() {}); //Suppress response stream errors
if (interpreter.stdout) {
interpreter.stdout.on("data", dataHandler);
interpreter.stderr.on("data", function (data) {
@ -321,7 +329,9 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
serverconsole.errmessage("There were CGI application errors:");
serverconsole.errmessage(preparedStderr);
}
res.end();
interpreter.stdout.removeListener("data", dataHandler);
interpreter.stdout.unpipe(res); //Prevent server crashes with write after the end
if(!res.finished) res.end();
}
});
}