diff --git a/doc/api/http.md b/doc/api/http.md index 240f507b5ba4..24418e9cd3ec 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 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 013be05d1f9f..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,10 +57,14 @@ const { const { onStreamRead, } = require('internal/stream_base_commons'); +const { kTimeout } = require('internal/timers'); 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, @@ -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 (socket.destroyed || isFreeSocketExpired(socket, now)) { + debug('free socket destroyed or 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,19 @@ 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. +// 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) { + const timer = socket[kTimeout]; + return socket.timeout > 0 && timer != null && timer._idleTimeout > 0 && + now - timer._idleStart >= MathTrunc(timer._idleTimeout); +} + 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..553054a1ad45 --- /dev/null +++ b/test/parallel/test-http-agent-keepalive-expired-free-socket.js @@ -0,0 +1,319 @@ +// 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); + })); + })); +} + +// 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 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) { + 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) { + 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, + // 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, +]; + +(function run(i) { + if (i < cases.length) cases[i](common.mustCall(() => run(i + 1))); +})(0);