Skip to content

http: fix res emit close before user finish #20941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ function resOnFinish(req, res, socket, state, server) {

res.detachSocket(socket);
req.emit('close');
res.emit('close');
process.nextTick(emitCloseNT, res);

if (res._last) {
if (typeof socket.destroySoon === 'function') {
Expand All @@ -585,6 +585,10 @@ function resOnFinish(req, res, socket, state, server) {
}
}

function emitCloseNT(self) {
self.emit('close');
}

// The following callback is issued after the headers have been read on a
// new message. In this callback we setup the response object and pass it
// to the user.
Expand Down
16 changes: 13 additions & 3 deletions test/parallel/test-http-req-res-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

const common = require('../common');
const http = require('http');
const assert = require('assert');

const server = http.Server(common.mustCall((req, res) => {
let resClosed = false;

res.end();
res.on('finish', common.mustCall());
res.on('close', common.mustCall());
req.on('close', common.mustCall());
res.on('finish', common.mustCall((() => {
assert.strictEqual(resClosed, false);
})));
res.on('close', common.mustCall(() => {
resClosed = true;
// assert.strictEqual(res._writableState.ended, true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove commented code.

}));
req.on('close', common.mustCall(() => {
assert.strictEqual(req._readableState.ended, true);
}));
res.socket.on('close', () => server.close());
}));

Expand Down