From 2a9c83321b7893395d9472755290c31acf4228a4 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Thu, 24 May 2018 16:30:35 +0200 Subject: [PATCH] http: fix res emit close before user finish PR-URL: https://github.com/nodejs/node/pull/20941 Reviewed-By: James M Snell Reviewed-By: Matteo Collina Reviewed-By: Ruben Bridgewater --- lib/_http_server.js | 6 +++++- test/parallel/test-http-req-res-close.js | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/_http_server.js b/lib/_http_server.js index 425ddef6f954f6..3d5a1f8f6242f7 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 240134cb5d0902..daba55f43427c2 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()); }));