From e119614a68a3627f9c05a26938cf58603cfd2f9e Mon Sep 17 00:00:00 2001 From: Paolo Insogna Date: Mon, 18 Jul 2022 14:37:36 +0200 Subject: [PATCH] http: wait for pending responses in closeIdleConnections PR-URL: https://github.com/nodejs/node/pull/43890 Fixes: https://github.com/nodejs/node/issues/43771 Reviewed-By: Matteo Collina Reviewed-By: Rafael Gonzaga --- lib/_http_server.js | 4 +++ ...st-http-server-close-idle-wait-response.js | 26 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 test/parallel/test-http-server-close-idle-wait-response.js diff --git a/lib/_http_server.js b/lib/_http_server.js index dbb9de4c13c6aa..dbac094adb1bc0 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -490,6 +490,10 @@ Server.prototype.closeIdleConnections = function() { const connections = this[kConnections].idle(); for (let i = 0, l = connections.length; i < l; i++) { + if (connections[i].socket._httpMessage && !connections[i].socket._httpMessage.finished) { + continue; + } + connections[i].socket.destroy(); } }; diff --git a/test/parallel/test-http-server-close-idle-wait-response.js b/test/parallel/test-http-server-close-idle-wait-response.js new file mode 100644 index 00000000000000..429c653f7463ce --- /dev/null +++ b/test/parallel/test-http-server-close-idle-wait-response.js @@ -0,0 +1,26 @@ +'use strict'; + +const common = require('../common'); + +const { createServer, get } = require('http'); + +const server = createServer(common.mustCall(function(req, res) { + req.resume(); + + setTimeout(common.mustCall(() => { + res.writeHead(204, { 'Connection': 'keep-alive', 'Keep-Alive': 'timeout=1' }); + res.end(); + }), common.platformTimeout(1000)); +})); + +server.listen(0, function() { + const port = server.address().port; + + get(`http://localhost:${port}`, common.mustCall((res) => { + server.close(); + })).on('finish', common.mustCall(() => { + setTimeout(common.mustCall(() => { + server.closeIdleConnections(); + }), common.platformTimeout(500)); + })); +});