Skip to content

Commit 5b15842

Browse files
dennevaduh95
authored andcommitted
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: #54293 Assisted-by: a closed-source coding agent Signed-off-by: dennev <dennev13@gmail.com> PR-URL: #67292 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
1 parent aebf092 commit 5b15842

3 files changed

Lines changed: 354 additions & 7 deletions

File tree

‎doc/api/http.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,10 @@ An object which contains arrays of sockets currently awaiting use by
353353
the agent when `keepAlive` is enabled. Do not modify.
354354

355355
Sockets in the `freeSockets` list will be automatically destroyed and
356-
removed from the array on `'timeout'`.
356+
removed from the array on `'timeout'`. When a request would reuse a free
357+
socket whose timeout has passed, for example because the event loop was blocked
358+
and `'timeout'` could not be emitted in time, the agent destroys that socket
359+
instead.
357360

358361
### `agent.getName([options])`
359362

‎lib/internal/http/agent.js‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'use strict';
2323

2424
const {
25+
MathTrunc,
2526
NumberIsFinite,
2627
NumberParseInt,
2728
ObjectKeys,
@@ -56,10 +57,14 @@ const {
5657
const {
5758
onStreamRead,
5859
} = require('internal/stream_base_commons');
60+
const { kTimeout } = require('internal/timers');
5961
const {
6062
kReadBytesOrError,
6163
streamBaseState,
6264
} = internalBinding('stream_wrap');
65+
// Call getLibuvNow() on the binding: its fast API call reads the binding
66+
// from the receiver.
67+
const timersBinding = internalBinding('timers');
6368
const {
6469
validateNumber,
6570
validateOneOf,
@@ -410,15 +415,22 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
410415
while (freeSockets.length && freeSockets[0].destroyed) {
411416
freeSockets.shift();
412417
}
413-
socket = this.scheduling === 'fifo' ?
414-
freeSockets.shift() :
415-
freeSockets.pop();
416-
while (socket && hasPendingRead(socket)) {
417-
debug('PENDING DATA on FREE socket - destroying poisoned socket');
418-
socket.destroy();
418+
const now = timersBinding.getLibuvNow();
419+
while (freeSockets.length) {
419420
socket = this.scheduling === 'fifo' ?
420421
freeSockets.shift() :
421422
freeSockets.pop();
423+
if (socket.destroyed || isFreeSocketExpired(socket, now)) {
424+
debug('free socket destroyed or expired');
425+
socket.destroy();
426+
socket.emit('agentRemove');
427+
} else if (hasPendingRead(socket)) {
428+
debug('PENDING DATA on FREE socket - destroying poisoned socket');
429+
socket.destroy();
430+
} else {
431+
break;
432+
}
433+
socket = undefined;
422434
}
423435
if (!freeSockets.length)
424436
delete this.freeSockets[name];
@@ -717,6 +729,19 @@ Agent.prototype.destroy = function destroy() {
717729
}
718730
};
719731

732+
// A free socket is destroyed by its idle timeout timer, but timers cannot run
733+
// while the event loop is blocked, so the socket can still be in the pool after
734+
// its timeout has passed, when the server may have closed the connection.
735+
// The timer records on the timers' clock when it was last started or refreshed,
736+
// and the timer scheduler truncates delays to whole milliseconds.
737+
// socket.timeout is checked too: a timer that has fired keeps its _idleTimeout
738+
// when the timeout is cleared afterwards.
739+
function isFreeSocketExpired(socket, now) {
740+
const timer = socket[kTimeout];
741+
return socket.timeout > 0 && timer != null && timer._idleTimeout > 0 &&
742+
now - timer._idleStart >= MathTrunc(timer._idleTimeout);
743+
}
744+
720745
function setRequestSocket(agent, req, socket) {
721746
req.onSocket(socket);
722747
const agentTimeout = agent.options.timeout || 0;

0 commit comments

Comments
 (0)