Skip to content

Commit

Permalink
http: fix res emit close before user finish
Browse files Browse the repository at this point in the history
PR-URL: nodejs#20941
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
ronag authored and MylesBorins committed May 24, 2018
1 parent 0b1ba20 commit 2a9c833
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
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
15 changes: 12 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,21 @@

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;
}));
req.on('close', common.mustCall(() => {
assert.strictEqual(req._readableState.ended, true);
}));
res.socket.on('close', () => server.close());
}));

Expand Down

0 comments on commit 2a9c833

Please sign in to comment.