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
29 changes: 29 additions & 0 deletions lib/dispatcher/pool-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const kOnConnectionError = Symbol('onConnectionError')
const kOnClientBusy = Symbol('on client busy')
const kOnClientDrain = Symbol('on client drain')
const kDrainQueue = Symbol('drain queue')
const kFailQueue = Symbol('fail queue')
const kGetDispatcher = Symbol('get dispatcher')
const kHasDispatcher = Symbol('has dispatcher')
const kAddClient = Symbol('add client')
Expand Down Expand Up @@ -199,6 +200,33 @@ class PoolBase extends DispatcherBase {
}
}

// Fail every request still waiting in the pool queue with a single observed
// error. Used when waiting can no longer produce a different outcome, e.g.
// the protocol probe's connection attempt failed: retrying the backlog one
// probe at a time would make each request wait for a full connect timeout of
// its own before it is even attempted.
// https://github.com/nodejs/undici/issues/6238
[kFailQueue] (err) {
// Only settle what is already waiting: an error handler may dispatch a new
// request synchronously, and that request must not inherit this error.
let remaining = this[kQueued]
while (remaining-- > 0) {
const item = this[kShiftQueue]()
if (item == null) {
break
}

const { handler } = item
item.opts = null
item.handler = null
item.runInAsyncScope(handler.onResponseError, handler, null, err)
}

if (this[kClosedResolve] && this[kQueued] === 0) {
closeClients(this).then(this[kClosedResolve])
}
}

[kOnConnect] = (origin, targets) => {
this.emit('connect', origin, [this, ...targets])
};
Expand Down Expand Up @@ -396,6 +424,7 @@ module.exports = {
kRemoveClient,
kRetireClient,
kDrainQueue,
kFailQueue,
kOnClientBusy,
kOnClientDrain,
kGetDispatcher,
Expand Down
15 changes: 14 additions & 1 deletion lib/dispatcher/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
kNeedDrain,
kAddClient,
kDrainQueue,
kFailQueue,
kOnClientBusy,
kOnClientDrain,
kGetDispatcher,
Expand Down Expand Up @@ -146,8 +147,9 @@ class Pool extends PoolBase {
}
})

this.on('connectionError', (origin, targets) => {
this.on('connectionError', (origin, targets, err) => {
let resumeQueued = false
let failedProbe = false

// If a connection error occurs, we remove the client from the pool,
// and emit a connectionError event. They will not be re-used.
Expand All @@ -156,6 +158,7 @@ class Pool extends PoolBase {
if (target === this[kProtocolProbe]) {
this[kProtocolProbe] = null
resumeQueued = true
failedProbe = true
}

// Retire (close) the client rather than only dropping it from
Expand All @@ -166,6 +169,16 @@ class Pool extends PoolBase {
}
}

if (failedProbe) {
// The queued requests are only waiting for this probe. Handing them to
// a fresh probe one at a time would restart the connect timeout for
// each of them, so a stalled origin rejects them one timeout after
// another instead of all at once.
// https://github.com/nodejs/undici/issues/6238
this[kFailQueue](err)
return
}

// The retired client no longer drains the pool queue, so hand any
// queued requests to another (possibly new) client.
if (resumeQueued || this[kQueued] > 0) {
Expand Down
46 changes: 46 additions & 0 deletions test/issue-6238.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict'

const assert = require('node:assert')
const { once } = require('node:events')
const net = require('node:net')
const { test } = require('node:test')

const { Agent, fetch } = require('..')

// https://github.com/nodejs/undici/issues/6238
test('concurrent requests to a stalled TLS origin time out together', { timeout: 60000 }, async (t) => {
// Accepts the TCP connection, but never answers the TLS handshake. The
// protocol is therefore never learned, and the pool keeps every concurrent
// request queued behind its protocol probe.
const sockets = []
const server = net.createServer((socket) => { sockets.push(socket) })
server.listen(0, '127.0.0.1')
await once(server, 'listening')

const connectTimeout = 2000
const dispatcher = new Agent({ connect: { timeout: connectTimeout } })

t.after(async () => {
for (const socket of sockets) {
socket.destroy()
}
server.close()
await dispatcher.destroy()
})

const url = `https://127.0.0.1:${server.address().port}/`
const started = performance.now()

const elapsed = await Promise.all([1, 2, 3, 4].map(async () => {
await assert.rejects(fetch(url, { dispatcher }), (error) => error.cause?.code === 'UND_ERR_CONNECT_TIMEOUT')

return performance.now() - started
}))

// Every request has a connect timeout of its own, so they must all reject
// after about one of them rather than one timeout after another.
assert.ok(
Math.max(...elapsed) < 2 * connectTimeout,
`the requests timed out one after another: ${elapsed.map((ms) => `${(ms / 1000).toFixed(1)}s`).join(', ')}`
)
})
Loading