This commit is contained in:
2026-08-14 16:03:47 +01:00
parent fd53e3f4fb
commit dfdc3b2840
2463 changed files with 186456 additions and 111315 deletions
+52
View File
@@ -203,4 +203,56 @@ module.exports = class Agent extends AgentBase {
return super.addRequest(request, options)
}
// When connect() rejects, agent-base removes only its placeholder socket, so Node never drains this.requests[name] and requests queued past maxSockets hang forever.
// On a failure we dispatch the next queued request ourselves.
// See npm/cli#9386 and TooTallNate/proxy-agents#427.
createSocket (req, options, cb) {
super.createSocket(req, options, (err, socket) => {
if (err) {
this.#drainPendingRequests(req, options)
}
cb(err, socket)
})
}
// Dispatch the next request queued behind maxSockets, reusing the slot the failed connection freed.
#drainPendingRequests (failedReq, options) {
const name = this.getName(options)
const queue = this.requests[name]
if (!queue || queue.length === 0) {
return
}
// Node's removeSocket() picks a queued request without shifting it off, so drop the failed one to avoid dispatching it twice.
const failedIndex = queue.indexOf(failedReq)
if (failedIndex !== -1) {
queue.splice(failedIndex, 1)
}
if (queue.length === 0) {
delete this.requests[name]
return
}
// Safety belt: only dispatch if a socket slot is genuinely free.
const socketCount = this.sockets[name] ? this.sockets[name].length : 0
if (socketCount >= this.maxSockets || this.totalSocketCount >= this.maxTotalSockets) {
return
}
const nextReq = queue.shift()
if (queue.length === 0) {
delete this.requests[name]
}
// All queued requests share this origin, so the failed request's options suit the next one.
// createSocket() recurses here if this connection also fails, draining the whole queue.
this.createSocket(nextReq, options, (err, socket) => {
if (err) {
nextReq.onSocket(null, err)
} else {
nextReq.onSocket(socket)
}
})
}
}
+4
View File
@@ -37,6 +37,10 @@ const normalizeOptions = (opts) => {
// remove timeout since we already used it to set our own idle timeout
delete normalized.timeout
// since opts is often passed when initiating requests, it may contain
// headers, which should not be saved in an agent
delete normalized.headers
return normalized
}