From bd4d56130f1518fe735f0896b693fe23dea4ee2f Mon Sep 17 00:00:00 2001 From: David Glasser Date: Mon, 30 Nov 2026 12:30:28 -0800 Subject: [PATCH 1/4] 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/4] 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) => { From 41c4adf9b146b831104cc83df24833161a17f1cf Mon Sep 17 00:00:00 2001 From: David Glasser Date: Thu, 3 Dec 2026 14:15:22 -0800 Subject: [PATCH 3/4] Remove assertion added in previous commit Looks like it doesn't pass on Windows. --- test/parallel/test-http-server-close-idle-pending-response.js | 2 -- 1 file changed, 2 deletions(-) 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 845da35c353..9cd4ff0af4e 100644 --- a/test/parallel/test-http-server-close-idle-pending-response.js +++ b/test/parallel/test-http-server-close-idle-pending-response.js @@ -23,8 +23,6 @@ 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. From 0cb68cedcfd0a1660ca8640c10d2a7f0bb24d72e Mon Sep 17 00:00:00 2001 From: David Glasser Date: Fri, 4 Dec 2026 14:33:23 -0800 Subject: [PATCH 4/4] test: keep the body queued by corking the socket Instead of writing a large body and hoping it gets buffered, simulate buffering by using `cork`, and temporarily stop `uncork` calls from having effect. This should make the test work on Windows. Signed-off-by: David Glasser --- ...http-server-close-idle-pending-response.js | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) 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 9cd4ff0af4e..036a50bfe05 100644 --- a/test/parallel/test-http-server-close-idle-pending-response.js +++ b/test/parallel/test-http-server-close-idle-pending-response.js @@ -10,23 +10,38 @@ 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 body = 'hello'; 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()); + const socket = res.socket; + res.writeHead(200, { 'Content-Length': body.length }); + res.flushHeaders(); + + // Keep the body queued in the socket's write buffer while server.close() + // runs: cork the socket, and make the uncork() that end() performs a + // no-op. (Writing a body larger than the socket buffers is not a reliable + // way to leave data queued, because on some platforms, including Windows, + // the kernel may accept all of it synchronously.) + socket.cork(); + // uncork() is inherited, so it is restored below by deleting the override. + assert.ok(!Object.hasOwn(socket, 'uncork')); + socket.uncork = () => {}; + res.end(body, common.mustCall()); assert.strictEqual(res.finished, true); assert.strictEqual(res.writableFinished, false); + assert.ok(socket.writableLength >= body.length); // The response is still being written, so this must leave the connection // alone and wait for it. server.close(common.mustCall()); + + // Now let the body be written. + delete socket.uncork; + socket.uncork(); })); })); @@ -36,13 +51,14 @@ server.listen(0, common.mustCall(() => { // keep-alive connection to time out. get({ port: server.address().port, agent: false }, common.mustCall((res) => { assert.strictEqual(res.statusCode, 200); - let received = 0; + let received = ''; + res.setEncoding('utf8'); res.on('data', (chunk) => { - received += chunk.length; + received += chunk; }); res.on('error', common.mustNotCall()); res.on('end', common.mustCall(() => { - assert.strictEqual(received, size); + assert.strictEqual(received, body); })); })); }));