Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])`

Expand Down
37 changes: 31 additions & 6 deletions lib/internal/http/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'use strict';

const {
MathTrunc,
NumberIsFinite,
NumberParseInt,
ObjectKeys,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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) {
Comment thread
dennev marked this conversation as resolved.
Resolved
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;
Expand Down
Loading