Permalink
Browse files

http: fix res emit close before user finish

PR-URL: #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 2a9c83321b7893395d9472755290c31acf4228a4
Showing with 17 additions and 4 deletions.
  1. +5 −1 lib/_http_server.js
  2. +12 −3 test/parallel/test-http-req-res-close.js
View
@@ -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.
@@ -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());
}));

0 comments on commit 2a9c833

Please sign in to comment.