http: fix res emit close before user finish

PR-URL: https://github.com/nodejs/node/pull/20941
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Robert Nagy 2018-05-24 16:30:35 +02:00 committed by Myles Borins
parent 3f4caec1d1
commit 8ce20aff2d
No known key found for this signature in database
GPG Key ID: 933B01F40B5CA946
2 changed files with 17 additions and 4 deletions

View File

@ -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') {
@ -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.

View File

@ -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());
}));