Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Nagy <ronagy@icloud.com>2018-05-24 17:30:35 +0300
committerMyles Borins <mylesborins@google.com>2018-05-24 20:25:03 +0300
commit2a9c83321b7893395d9472755290c31acf4228a4 (patch)
tree8584cf07ec650e7d708c4cce167db6e62ae59811
parent0b1ba20fc07158896c09a9dc219d8f933a99d98d (diff)
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>
-rw-r--r--lib/_http_server.js6
-rw-r--r--test/parallel/test-http-req-res-close.js15
2 files changed, 17 insertions, 4 deletions
diff --git a/lib/_http_server.js b/lib/_http_server.js
index 425ddef6f95..3d5a1f8f624 100644
--- a/lib/_http_server.js
+++ b/lib/_http_server.js
@@ -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.
diff --git a/test/parallel/test-http-req-res-close.js b/test/parallel/test-http-req-res-close.js
index 240134cb5d0..daba55f4342 100644
--- a/test/parallel/test-http-req-res-close.js
+++ b/test/parallel/test-http-req-res-close.js
@@ -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());
}));