Closed
Description
- Version: v14.5.0
- Platform: Linux - kernel 5.4.0-40-generic
- Subsystem: Stream utils (?)
What steps will reproduce the bug?
stream.finished
is failing to detect a closed ServerResponse
in the case the request is aborted (response emits close
but doesn't emit finish
).
This is happening when stream.finished
is called with an already closed ServerResponse
. I thought it should work in this case because if I give a destroyed PassThrough
stream for example it works properly - it executes the callback.
const stream = require("stream");
const pass = new stream.PassThrough();
pass.on("close", () => {
stream.finished(pass, (err) => {
// it works
console.log("stream.finished worked", err);
});
});
pass.destroy();
Now in the case of a response the callback isn't executed:
const stream = require("stream");
const http = require("http");
http
.createServer((req, res) => {
res.on("finish", () => {
// res will not be finished
console.log("response finished")
});
res.on("close", () => {
// res will be closed
console.log("response closed");
stream.finished(res, (err) => {
// but this callback will not be executed
console.log("stream.finished worked");
});
});
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");
res.write("Hello world");
})
.listen(3000, "localhost", () => {
console.log(`Server running at http://localhost:3000/`);
const req = http.get("http://localhost:3000", (res) => {
console.log("Request was made");
setTimeout(() => req.abort(), 100);
});
});
What is the expected behavior?
I expected console output to be:
Server running at http://localhost:3000/
Request was made
response closed
stream.finished worked
What do you see instead?
The output is:
Server running at http://localhost:3000/
Request was made
response closed