From 646483f499ba64812885983962d662cd06c0139b Mon Sep 17 00:00:00 2001 From: dennev Date: Wed, 14 Oct 2026 04:52:05 +0000 Subject: [PATCH 1/4] http: do not reuse expired free sockets A free socket with an idle timeout is normally destroyed by its timer. When the event loop is blocked long enough for that timeout to expire, the timer cannot run, and `addRequest()` can reuse the socket even though the server may already have closed the connection. This can cause the request to fail with `ECONNRESET`. Record when a socket enters the free pool and, before reuse, remove and destroy a free socket whose idle timeout has expired according to the timers' clock. Refs: https://github.com/nodejs/node/issues/54293 Assisted-by: a closed-source coding agent Signed-off-by: dennev --- doc/api/http.md | 5 +- lib/internal/http/agent.js | 35 +++- ...ttp-agent-keepalive-expired-free-socket.js | 160 ++++++++++++++++++ 3 files changed, 193 insertions(+), 7 deletions(-) create mode 100644 test/parallel/test-http-agent-keepalive-expired-free-socket.js diff --git a/doc/api/http.md b/doc/api/http.md index 240f507b5ba4..62b5e778c6b4 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -353,7 +353,10 @@ An object which contains arrays of sockets currently awaiting use by the agent when `keepAlive` is enabled. Do not modify. Sockets in the `freeSockets` list will be automatically destroyed and -removed from the array on `'timeout'`. +removed from the array on `'timeout'`. When a request would reuse a free +socket that has been in the list for at least its timeout, for example because +the event loop was blocked and `'timeout'` could not be emitted in time, the +agent destroys that socket instead. ### `agent.getName([options])` diff --git a/lib/internal/http/agent.js b/lib/internal/http/agent.js index 013be05d1f9f..6af93ea85d14 100644 --- a/lib/internal/http/agent.js +++ b/lib/internal/http/agent.js @@ -60,6 +60,9 @@ const { kReadBytesOrError, streamBaseState, } = internalBinding('stream_wrap'); +// Call getLibuvNow() on the binding: its fast API call reads the binding +// from the receiver. +const timersBinding = internalBinding('timers'); const { validateNumber, validateOneOf, @@ -72,6 +75,7 @@ const kOnKeylog = Symbol('onkeylog'); const kRequestOptions = Symbol('requestOptions'); const kRequestAsyncResource = Symbol('requestAsyncResource'); const kFreeSocketDataGuard = Symbol('freeSocketDataGuard'); +const kFreeSince = Symbol('freeSince'); // New Agent code. @@ -283,6 +287,7 @@ function Agent(options) { socket.once('error', freeSocketErrorListener); installFreeSocketDataGuard(socket); + socket[kFreeSince] = timersBinding.getLibuvNow(); freeSockets.push(socket); }); @@ -410,15 +415,22 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */, while (freeSockets.length && freeSockets[0].destroyed) { freeSockets.shift(); } - socket = this.scheduling === 'fifo' ? - freeSockets.shift() : - freeSockets.pop(); - while (socket && hasPendingRead(socket)) { - debug('PENDING DATA on FREE socket - destroying poisoned socket'); - socket.destroy(); + const now = timersBinding.getLibuvNow(); + while (freeSockets.length) { socket = this.scheduling === 'fifo' ? freeSockets.shift() : freeSockets.pop(); + if (isFreeSocketExpired(socket, now)) { + debug('free socket expired'); + socket.destroy(); + socket.emit('agentRemove'); + } else if (hasPendingRead(socket)) { + debug('PENDING DATA on FREE socket - destroying poisoned socket'); + socket.destroy(); + } else { + break; + } + socket = undefined; } if (!freeSockets.length) delete this.freeSockets[name]; @@ -717,6 +729,17 @@ Agent.prototype.destroy = function destroy() { } }; +// A free socket is destroyed by its idle timeout timer, but timers cannot run +// while the event loop is blocked, so the socket can still be in the pool after +// its timeout has passed, when the server may have closed the connection. +// kFreeSince is read from the timers' clock when the socket enters the pool, +// after keepSocketAlive(). The socket's idle timer was last started then or +// earlier (at its last read or write), so this does not expire a socket before +// that timer is due. +function isFreeSocketExpired(socket, now) { + return socket.timeout > 0 && now - socket[kFreeSince] >= socket.timeout; +} + function setRequestSocket(agent, req, socket) { req.onSocket(socket); const agentTimeout = agent.options.timeout || 0; diff --git a/test/parallel/test-http-agent-keepalive-expired-free-socket.js b/test/parallel/test-http-agent-keepalive-expired-free-socket.js new file mode 100644 index 000000000000..867b1b3911ed --- /dev/null +++ b/test/parallel/test-http-agent-keepalive-expired-free-socket.js @@ -0,0 +1,160 @@ +// Flags: --expose-internals --no-warnings --allow-natives-syntax +'use strict'; + +const common = require('../common'); + +// A free socket expires by its idle timeout timer. When the event loop is +// blocked longer than that timeout, the timer cannot run, and the next +// request must not reuse the socket, because the server may already have +// closed it. +// Refs: https://github.com/nodejs/node/issues/54293 + +const assert = require('node:assert'); +const http = require('node:http'); +const { internalBinding } = require('internal/test/binding'); + +function count(lists) { + return Object.values(lists).reduce((n, list) => n + list.length, 0); +} + +// Makes `sockets` concurrent requests, then calls `callback` once all their +// sockets are in the agent's free pool. +function fillPool(agent, port, sockets, callback) { + let free = 0; + agent.on('free', function onFree() { + if (++free === sockets) { + agent.removeListener('free', onFree); + setImmediate(callback); + } + }); + for (let i = 0; i < sockets; i++) { + http.get({ port, agent }, common.mustCall((res) => res.resume())); + } +} + +function testReuse(agentOptions, blockMs, expectReuse, next) { + const server = http.createServer(common.mustCall((req, res) => { + res.end('ok'); + }, 2)); + // No Keep-Alive hint from the server, so the agent's `timeout` alone sets + // the socket's idle timeout. + server.keepAliveTimeout = 0; + + server.listen(0, common.mustCall(() => { + const agent = new http.Agent({ keepAlive: true, ...agentOptions }); + const { port } = server.address(); + + fillPool(agent, port, 1, common.mustCall(() => { + const [socket] = Object.values(agent.freeSockets)[0]; + assert.strictEqual(socket.timeout, agentOptions.timeout ?? 0); + common.sleepSync(blockMs); + const req = http.get({ port, agent }, common.mustCall((res) => { + res.resume(); + res.on('end', common.mustCall(() => { + agent.destroy(); + server.close(next); + })); + })); + assert.strictEqual(req.reusedSocket, expectReuse); + assert.strictEqual(socket.destroyed, !expectReuse); + })); + })); +} + +// The expired sockets release their slots at once, so a burst of requests +// after the block opens no more than maxSockets connections. +function testMaxSockets(next) { + const timeout = common.platformTimeout(500); + const server = http.createServer(common.mustCall((req, res) => { + res.end('ok'); + }, 6)); + server.keepAliveTimeout = 0; + + server.listen(0, common.mustCall(() => { + const agent = new http.Agent({ keepAlive: true, timeout: 60_000, maxSockets: 2 }); + const { port } = server.address(); + + fillPool(agent, port, 2, common.mustCall(() => { + const pooled = [...Object.values(agent.freeSockets)[0]]; + assert.strictEqual(pooled.length, 2); + for (const socket of pooled) { + socket.setTimeout(timeout); + } + common.sleepSync(timeout * 2); + let done = 0; + for (let i = 0; i < 4; i++) { + http.get({ port, agent }, common.mustCall((res) => { + res.resume(); + res.on('end', () => { + if (++done === 4) { + agent.destroy(); + server.close(next); + } + }); + })); + } + assert(pooled.every((socket) => socket.destroyed)); + assert.strictEqual(agent.totalSocketCount, 2); + assert.strictEqual(count(agent.sockets), 2); + assert.strictEqual(count(agent.requests), 2); + })); + })); +} + +// addRequest() reads the timers' clock through a fast API call when it takes a +// socket from the pool; the optimized function must be able to make it. +function testOptimizedAddRequest(next) { + const server = http.createServer(common.mustCall((req, res) => { + res.end('ok'); + }, 3)); + + server.listen(0, common.mustCall(() => { + const agent = new http.Agent({ keepAlive: true, timeout: 60_000 }); + const { port } = server.address(); + + function get(callback) { + http.get({ port, agent }, common.mustCall((res) => { + res.resume(); + res.on('end', common.mustCall(() => setImmediate(callback))); + })); + } + + eval('%PrepareFunctionForOptimization(http.Agent.prototype.addRequest)'); + get(common.mustCall(() => get(common.mustCall(() => { + eval('%OptimizeFunctionOnNextCall(http.Agent.prototype.addRequest)'); + const fastCalls = getFastCallCount(); + get(common.mustCall(() => { + agent.destroy(); + server.close(next); + })); + if (common.isDebug) { + assert.strictEqual(getFastCallCount() - fastCalls, 1); + } + })))); + })); +} + +// The fast API call counter exists in debug builds only. +function getFastCallCount() { + if (!common.isDebug) return 0; + const { getV8FastApiCallCount } = internalBinding('debug'); + return getV8FastApiCallCount('timers.getLibuvNow'); +} + +// The cases run one after another, so that one case's block does not delay +// the timers of another. +const cases = [ + // The idle timeout passed during the block. + (next) => testReuse({ timeout: 100 }, 300, false, next), + (next) => testReuse({ timeout: 100, scheduling: 'fifo' }, 300, false, next), + // The idle timeout did not pass during the block. + (next) => testReuse({ timeout: 60_000 }, 300, true, next), + // A socket without an idle timeout is not expired by the clock. + (next) => testReuse({}, 300, true, next), + testMaxSockets, + testOptimizedAddRequest, +]; + +(function run(i) { + if (i < cases.length) cases[i](common.mustCall(() => run(i + 1))); +})(0); From 8f996f5cd79c8a8e11dede3fc0e5959ca241a20c Mon Sep 17 00:00:00 2001 From: dennev Date: Wed, 11 Nov 2026 10:41:36 +0900 Subject: [PATCH 2/4] http: do not reuse destroyed free sockets A destroyed socket can stay in the agent's free pool until its 'close' event. `addRequest()` only dropped destroyed sockets from the head of the pool, so a request made before that event could still take one, for example from the tail of the pool with the default `lifo` scheduling, and fail with `ECONNRESET`. Skip destroyed sockets when selecting a socket to reuse. Destroyed sockets rejected during selection are removed at once, as expired sockets are. Leading destroyed sockets are still pruned and counted until their 'close' event. Assisted-by: a closed-source coding agent Signed-off-by: dennev --- lib/internal/http/agent.js | 4 +-- ...ttp-agent-keepalive-expired-free-socket.js | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/internal/http/agent.js b/lib/internal/http/agent.js index 6af93ea85d14..e2c636a0c0c0 100644 --- a/lib/internal/http/agent.js +++ b/lib/internal/http/agent.js @@ -420,8 +420,8 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */, socket = this.scheduling === 'fifo' ? freeSockets.shift() : freeSockets.pop(); - if (isFreeSocketExpired(socket, now)) { - debug('free socket expired'); + if (socket.destroyed || isFreeSocketExpired(socket, now)) { + debug('free socket destroyed or expired'); socket.destroy(); socket.emit('agentRemove'); } else if (hasPendingRead(socket)) { diff --git a/test/parallel/test-http-agent-keepalive-expired-free-socket.js b/test/parallel/test-http-agent-keepalive-expired-free-socket.js index 867b1b3911ed..47b2e45a7528 100644 --- a/test/parallel/test-http-agent-keepalive-expired-free-socket.js +++ b/test/parallel/test-http-agent-keepalive-expired-free-socket.js @@ -101,6 +101,39 @@ function testMaxSockets(next) { })); } +// A destroyed socket can stay in the free pool until its 'close' event. A +// request made before that event must not get it. +function testDestroyedSocket(next) { + const server = http.createServer(common.mustCall((req, res) => { + res.end('ok'); + }, 3)); + // No Keep-Alive hint from the server, so the pooled sockets have no idle + // timeout and cannot expire. + server.keepAliveTimeout = 0; + + server.listen(0, common.mustCall(() => { + const agent = new http.Agent({ keepAlive: true }); + const { port } = server.address(); + + fillPool(agent, port, 2, common.mustCall(() => { + const [first, last] = Object.values(agent.freeSockets)[0]; + last.destroy(); + const req = http.get({ port, agent }, common.mustCall((res) => { + assert.strictEqual(req.socket, first); + res.resume(); + res.on('end', common.mustCall(() => { + agent.destroy(); + server.close(next); + })); + })); + assert.strictEqual(req.reusedSocket, true); + assert.strictEqual(agent.totalSocketCount, 1); + assert.strictEqual(count(agent.freeSockets), 0); + assert.strictEqual(count(agent.sockets), 1); + })); + })); +} + // addRequest() reads the timers' clock through a fast API call when it takes a // socket from the pool; the optimized function must be able to make it. function testOptimizedAddRequest(next) { @@ -152,6 +185,7 @@ const cases = [ // A socket without an idle timeout is not expired by the clock. (next) => testReuse({}, 300, true, next), testMaxSockets, + testDestroyedSocket, testOptimizedAddRequest, ]; From 748284a2303af8193d233eb8d16ef83b4606034c Mon Sep 17 00:00:00 2001 From: dennev Date: Thu, 12 Nov 2026 13:20:05 +0900 Subject: [PATCH 3/4] test: cover a pool with expired and unexpired free sockets When the agent takes a socket from the free pool, it removes expired sockets until it finds one that has not expired. No case had a pool that holds both kinds, so nothing checked that the request then reuses the unexpired socket and that only the expired one is removed. Assisted-by: a closed-source coding agent Signed-off-by: dennev --- ...ttp-agent-keepalive-expired-free-socket.js | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/test/parallel/test-http-agent-keepalive-expired-free-socket.js b/test/parallel/test-http-agent-keepalive-expired-free-socket.js index 47b2e45a7528..d14ef0f364c6 100644 --- a/test/parallel/test-http-agent-keepalive-expired-free-socket.js +++ b/test/parallel/test-http-agent-keepalive-expired-free-socket.js @@ -101,6 +101,51 @@ function testMaxSockets(next) { })); } +// When the socket that the scheduling picks first has expired and the other +// one has not, the expired one is removed and the request reuses the other. +function testMixedPool(scheduling, next) { + const server = http.createServer(common.mustCall((req, res) => { + res.end('ok'); + }, 3)); + // No Keep-Alive hint from the server, so both sockets enter the pool with + // the agent's `timeout` as their idle timeout. + server.keepAliveTimeout = 0; + + server.listen(0, common.mustCall(() => { + const agent = new http.Agent({ + keepAlive: true, + timeout: 60_000, + scheduling, + }); + const { port } = server.address(); + + fillPool(agent, port, 2, common.mustCall(() => { + const [first, last] = Object.values(agent.freeSockets)[0]; + // The socket that the scheduling picks first gets an idle timeout that + // passes during the block. + const [expired, kept] = scheduling === 'fifo' ? + [first, last] : + [last, first]; + expired.setTimeout(100); + common.sleepSync(300); + const req = http.get({ port, agent }, common.mustCall((res) => { + assert.strictEqual(req.socket, kept); + res.resume(); + res.on('end', common.mustCall(() => { + agent.destroy(); + server.close(next); + })); + })); + assert.strictEqual(req.reusedSocket, true); + assert.strictEqual(expired.destroyed, true); + assert.strictEqual(kept.destroyed, false); + assert.strictEqual(agent.totalSocketCount, 1); + assert.strictEqual(count(agent.freeSockets), 0); + assert.strictEqual(count(agent.sockets), 1); + })); + })); +} + // A destroyed socket can stay in the free pool until its 'close' event. A // request made before that event must not get it. function testDestroyedSocket(next) { @@ -185,6 +230,9 @@ const cases = [ // A socket without an idle timeout is not expired by the clock. (next) => testReuse({}, 300, true, next), testMaxSockets, + // The idle timeout of one of two pooled sockets passed during the block. + (next) => testMixedPool('lifo', next), + (next) => testMixedPool('fifo', next), testDestroyedSocket, testOptimizedAddRequest, ]; From 078c6d918678a4e44656c8cf9d71d1afceff6e2e Mon Sep 17 00:00:00 2001 From: dennev Date: Sat, 14 Nov 2026 02:28:16 +0900 Subject: [PATCH 4/4] http: read free socket expiry from its idle timer Instead of recording when a socket enters the free pool, check the socket's idle timer, which records on the timers' clock when it was last started or refreshed. This removes the clock read and the property write from the 'free' handler, and a pooled socket whose timeout is set again is now expired by its restarted timer. With an agent timeout, the client clears the socket's timeout when it keeps the socket alive, and the agent restarts the timer as the socket enters the pool, so in that flow the expiry is measured from the same moment as before. socket.timeout is still checked, because a timer that fired before it was cleared keeps its _idleTimeout. Assisted-by: a closed-source coding agent Signed-off-by: dennev --- doc/api/http.md | 6 +- lib/internal/http/agent.js | 16 ++-- ...ttp-agent-keepalive-expired-free-socket.js | 77 +++++++++++++++++++ 3 files changed, 89 insertions(+), 10 deletions(-) diff --git a/doc/api/http.md b/doc/api/http.md index 62b5e778c6b4..24418e9cd3ec 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -354,9 +354,9 @@ the agent when `keepAlive` is enabled. Do not modify. Sockets in the `freeSockets` list will be automatically destroyed and removed from the array on `'timeout'`. When a request would reuse a free -socket that has been in the list for at least its timeout, for example because -the event loop was blocked and `'timeout'` could not be emitted in time, the -agent destroys that socket instead. +socket whose timeout has passed, for example because the event loop was blocked +and `'timeout'` could not be emitted in time, the agent destroys that socket +instead. ### `agent.getName([options])` diff --git a/lib/internal/http/agent.js b/lib/internal/http/agent.js index e2c636a0c0c0..20aef9f83760 100644 --- a/lib/internal/http/agent.js +++ b/lib/internal/http/agent.js @@ -22,6 +22,7 @@ 'use strict'; const { + MathTrunc, NumberIsFinite, NumberParseInt, ObjectKeys, @@ -56,6 +57,7 @@ const { const { onStreamRead, } = require('internal/stream_base_commons'); +const { kTimeout } = require('internal/timers'); const { kReadBytesOrError, streamBaseState, @@ -75,7 +77,6 @@ const kOnKeylog = Symbol('onkeylog'); const kRequestOptions = Symbol('requestOptions'); const kRequestAsyncResource = Symbol('requestAsyncResource'); const kFreeSocketDataGuard = Symbol('freeSocketDataGuard'); -const kFreeSince = Symbol('freeSince'); // New Agent code. @@ -287,7 +288,6 @@ function Agent(options) { socket.once('error', freeSocketErrorListener); installFreeSocketDataGuard(socket); - socket[kFreeSince] = timersBinding.getLibuvNow(); freeSockets.push(socket); }); @@ -732,12 +732,14 @@ Agent.prototype.destroy = function destroy() { // A free socket is destroyed by its idle timeout timer, but timers cannot run // while the event loop is blocked, so the socket can still be in the pool after // its timeout has passed, when the server may have closed the connection. -// kFreeSince is read from the timers' clock when the socket enters the pool, -// after keepSocketAlive(). The socket's idle timer was last started then or -// earlier (at its last read or write), so this does not expire a socket before -// that timer is due. +// The timer records on the timers' clock when it was last started or refreshed, +// and the timer scheduler truncates delays to whole milliseconds. +// socket.timeout is checked too: a timer that has fired keeps its _idleTimeout +// when the timeout is cleared afterwards. function isFreeSocketExpired(socket, now) { - return socket.timeout > 0 && now - socket[kFreeSince] >= socket.timeout; + const timer = socket[kTimeout]; + return socket.timeout > 0 && timer != null && timer._idleTimeout > 0 && + now - timer._idleStart >= MathTrunc(timer._idleTimeout); } function setRequestSocket(agent, req, socket) { diff --git a/test/parallel/test-http-agent-keepalive-expired-free-socket.js b/test/parallel/test-http-agent-keepalive-expired-free-socket.js index d14ef0f364c6..553054a1ad45 100644 --- a/test/parallel/test-http-agent-keepalive-expired-free-socket.js +++ b/test/parallel/test-http-agent-keepalive-expired-free-socket.js @@ -146,6 +146,81 @@ function testMixedPool(scheduling, next) { })); } +// A pooled socket whose timeout is set again is expired by its restarted timer, +// not by the time it entered the pool. +function testRestartedTimeout(next) { + const server = http.createServer(common.mustCall((req, res) => { + res.end('ok'); + }, 2)); + // No Keep-Alive hint from the server, so the agent's `timeout` alone sets + // the socket's idle timeout. + server.keepAliveTimeout = 0; + + server.listen(0, common.mustCall(() => { + const agent = new http.Agent({ keepAlive: true, timeout: 60_000 }); + const { port } = server.address(); + + fillPool(agent, port, 1, common.mustCall(() => { + const [socket] = Object.values(agent.freeSockets)[0]; + common.sleepSync(300); + // Shorter than the block: only a timer restarted now has not passed it. + socket.setTimeout(200); + const req = http.get({ port, agent }, common.mustCall((res) => { + assert.strictEqual(res.socket, socket); + res.resume(); + res.on('end', common.mustCall(() => { + agent.destroy(); + server.close(next); + })); + })); + assert.strictEqual(req.reusedSocket, true); + })); + })); +} + +// A request timeout that fired while the response waited unread is cleared when +// the socket is kept alive. Its timer then still holds the fired timeout, but +// the socket has no idle timeout, so it is reused. +function testFiredTimeoutCleared(next) { + const server = http.createServer(common.mustCall((req, res) => { + res.end('ok'); + }, 2)); + // No Keep-Alive hint from the server, and the agent has no `timeout`, so the + // pooled socket has no idle timeout. + server.keepAliveTimeout = 0; + + server.listen(0, common.mustCall(() => { + const agent = new http.Agent({ keepAlive: true }); + const { port } = server.address(); + + function reuse(socket) { + assert.strictEqual(socket.timeout, 0); + const req = http.get({ port, agent }, common.mustCall((res) => { + assert.strictEqual(res.socket, socket); + res.resume(); + res.on('end', common.mustCall(() => { + agent.destroy(); + server.close(next); + })); + })); + assert.strictEqual(req.reusedSocket, true); + } + + const first = http.get({ port, agent }, common.mustCall((res) => { + const { socket } = res; + // The response is read only after the request timeout has fired. + first.setTimeout(50, common.mustCall(() => { + setImmediate(common.mustCall(() => { + agent.once('free', common.mustCall(() => { + setImmediate(common.mustCall(() => reuse(socket))); + })); + res.resume(); + })); + })); + })); + })); +} + // A destroyed socket can stay in the free pool until its 'close' event. A // request made before that event must not get it. function testDestroyedSocket(next) { @@ -233,6 +308,8 @@ const cases = [ // The idle timeout of one of two pooled sockets passed during the block. (next) => testMixedPool('lifo', next), (next) => testMixedPool('fifo', next), + testRestartedTimeout, + testFiredTimeoutCleared, testDestroyedSocket, testOptimizedAddRequest, ];