From c6e11cf55a57d971639cc6c483fa888529d6d2ce Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Thu, 21 Oct 2021 11:41:22 +0200 Subject: [PATCH] http: response should always emit 'close' Fixes: https://github.com/nodejs/node/issues/40528 --- lib/_http_server.js | 2 +- test/parallel/test-http-response-close.js | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/_http_server.js b/lib/_http_server.js index 045e0b2f719717..0d06919ea017a4 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -839,7 +839,7 @@ function resOnFinish(req, res, socket, state, server) { } function emitCloseNT(self) { - if (!self.destroyed) { + if (!self._closed) { self.destroyed = true; self._closed = true; self.emit('close'); diff --git a/test/parallel/test-http-response-close.js b/test/parallel/test-http-response-close.js index 0435cd0ee37da5..848d316d8adfe7 100644 --- a/test/parallel/test-http-response-close.js +++ b/test/parallel/test-http-response-close.js @@ -78,3 +78,25 @@ const assert = require('assert'); }) ); } + +{ + const server = http.createServer( + common.mustCall((req, res) => { + res.on('close', common.mustCall()); + res.destroy(); + }) + ); + + server.listen( + 0, + common.mustCall(() => { + http.get( + { port: server.address().port }, + common.mustNotCall() + ) + .on('error', common.mustCall(() => { + server.close(); + })); + }) + ); +}