From bd4d56130f1518fe735f0896b693fe23dea4ee2f Mon Sep 17 00:00:00 2001 From: David Glasser Date: Mon, 30 Nov 2026 12:30:28 -0800 Subject: [PATCH 1/2] http: don't destroy connections still writing a response closeIdleConnections() skips a connection only while its response is not `finished`, but `finished` is set as soon as end() is called, before the queued data has been written to the socket. A response to a slow client can therefore be destroyed mid-write by closeIdleConnections(), or by server.close() which calls it, truncating the body while the end() callback and 'finish' still fire. Check `writableFinished` instead, which is only true once everything has been handed to the socket. Fixes: https://github.com/nodejs/node/issues/67910 Refs: https://github.com/nodejs/node/pull/43890 Signed-off-by: David Glasser --- lib/internal/http/server.js | 5 ++- ...http-server-close-idle-pending-response.js | 45 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-http-server-close-idle-pending-response.js diff --git a/lib/internal/http/server.js b/lib/internal/http/server.js index f6031f98a0a..00275ccb95a 100644 --- a/lib/internal/http/server.js +++ b/lib/internal/http/server.js @@ -759,10 +759,13 @@ Server.prototype.closeIdleConnections = function closeIdleConnections() { return; } + // Get connections which don't have an incomplete incoming request. 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) { + // Skip connections with incomplete outgoing responses. + if (connections[i].socket._httpMessage && + !connections[i].socket._httpMessage.writableFinished) { continue; } diff --git a/test/parallel/test-http-server-close-idle-pending-response.js b/test/parallel/test-http-server-close-idle-pending-response.js new file mode 100644 index 00000000000..01955481fa5 --- /dev/null +++ b/test/parallel/test-http-server-close-idle-pending-response.js @@ -0,0 +1,45 @@ +'use strict'; + +// Test that server.close() (which calls closeIdleConnections()) does not +// destroy a connection whose response has been ended but has not yet been +// fully written to the socket. Such a response has `finished` set, but is +// not `writableFinished`; destroying the socket would drop the queued data +// and truncate the response. + +const common = require('../common'); +const assert = require('assert'); +const { createServer, get } = require('http'); + +// Larger than the socket buffers, so that part of the body is still queued in +// Node when end() returns. +const size = 32 * 1024 * 1024; + +const server = createServer(common.mustCall((req, res) => { + // Wait for the request to be fully received, so that the connection is + // idle as far as the parser is concerned and the sweep considers it. + req.resume(); + req.on('end', common.mustCall(() => { + res.writeHead(200, { 'Content-Length': size }); + res.end(Buffer.alloc(size), common.mustCall()); + assert.strictEqual(res.finished, true); + assert.strictEqual(res.writableFinished, false); + + // The response is still being written, so this must leave the connection + // alone and wait for it. + server.close(common.mustCall()); + })); +})); + +server.listen(0, common.mustCall(() => { + get({ port: server.address().port }, common.mustCall((res) => { + assert.strictEqual(res.statusCode, 200); + let received = 0; + res.on('data', (chunk) => { + received += chunk.length; + }); + res.on('error', common.mustNotCall()); + res.on('end', common.mustCall(() => { + assert.strictEqual(received, size); + })); + })); +})); From f53e97f206fe82a8725bb03eef2ef3d9d9b51c58 Mon Sep 17 00:00:00 2001 From: David Glasser Date: Wed, 2 Dec 2026 16:14:41 -0800 Subject: [PATCH 2/2] test: tighten the close-idle pending-response test Use a non-keep-alive client so server.close() does not wait for the client's idle connection to time out, and assert that part of the body is still queued in Node (socket.writableLength > 0) rather than only that the response is not writableFinished, which is true right after end() even for a tiny body. Signed-off-by: David Glasser --- .../test-http-server-close-idle-pending-response.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-http-server-close-idle-pending-response.js b/test/parallel/test-http-server-close-idle-pending-response.js index 01955481fa5..845da35c353 100644 --- a/test/parallel/test-http-server-close-idle-pending-response.js +++ b/test/parallel/test-http-server-close-idle-pending-response.js @@ -23,6 +23,8 @@ const server = createServer(common.mustCall((req, res) => { res.end(Buffer.alloc(size), common.mustCall()); assert.strictEqual(res.finished, true); assert.strictEqual(res.writableFinished, false); + // Part of the body is still queued in Node, not yet handed to the socket. + assert.ok(res.socket.writableLength > 0); // The response is still being written, so this must leave the connection // alone and wait for it. @@ -31,7 +33,10 @@ const server = createServer(common.mustCall((req, res) => { })); server.listen(0, common.mustCall(() => { - get({ port: server.address().port }, common.mustCall((res) => { + // No keep-alive agent: once the response is complete the client closes the + // connection, so server.close() finishes without waiting for an idle + // keep-alive connection to time out. + get({ port: server.address().port, agent: false }, common.mustCall((res) => { assert.strictEqual(res.statusCode, 200); let received = 0; res.on('data', (chunk) => {