From 757c14f04903b9c75e138ff14688341a38775e2f Mon Sep 17 00:00:00 2001 From: RegretSSS <38377607+RegretSSS@users.noreply.github.com> Date: Sun, 8 Nov 2026 10:37:27 +0800 Subject: [PATCH 1/3] test: issue #6054 reporter reproduction (red on main) --- test/issue-6054-repro.js | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 test/issue-6054-repro.js diff --git a/test/issue-6054-repro.js b/test/issue-6054-repro.js new file mode 100644 index 00000000000..3051bc111a3 --- /dev/null +++ b/test/issue-6054-repro.js @@ -0,0 +1,49 @@ +'use strict' + +// nodejs/undici#6054 —— 报告者复现(已验证在当前 main 上 FAIL:Pool 永久卡死) +const { test } = require('node:test') +const assert = require('node:assert/strict') +const { createServer } = require('node:http') +const { once } = require('node:events') +const { setImmediate: nextTurn } = require('node:timers/promises') +const { Pool } = require('../index.js') + +test('Pool remains usable after a request validation error', { timeout: 5000 }, async (t) => { + let received = 0 + const server = createServer((req, res) => { + received++ + res.end('ok') + }) + server.listen(0, '127.0.0.1') + await once(server, 'listening') + + const pool = new Pool(`http://127.0.0.1:${server.address().port}`, { connections: 1 }) + t.after(async () => { + await pool.destroy() + server.closeAllConnections() + await new Promise(resolve => server.close(resolve)) + }) + + const warm = await pool.request({ path: '/', method: 'GET' }) + assert.equal(await warm.body.text(), 'ok') + await nextTurn() + assert.equal(pool.stats.free, 1) + + await assert.rejects( + pool.request({ path: '/', method: 'GET', headers: { 'x-invalid': '\n' } }), + { code: 'UND_ERR_INVALID_ARG' } + ) + + const next = pool.request({ path: '/', method: 'GET' }) + .then(async ({ body }) => body.text()) + let timer + const outcome = await Promise.race([ + next, + new Promise(resolve => { timer = setTimeout(() => resolve('TIMEOUT'), 500) }) + ]).finally(() => clearTimeout(timer)) + + const { connected, free, running, pending, queued, size } = pool.stats + console.log({ outcome, received, connected, free, running, pending, queued, size }) + assert.equal(outcome, 'ok') + assert.equal(received, 2) +}) From 72e98061e092e116cf01315c9c1838dc87557b71 Mon Sep 17 00:00:00 2001 From: RegretSSS <38377607+RegretSSS@users.noreply.github.com> Date: Sun, 8 Nov 2026 10:51:04 +0800 Subject: [PATCH 2/3] fix: dispatch must not report busy after a synchronous failure DispatcherBase.dispatch catches synchronous request validation errors (e.g. an invalid header throwing UND_ERR_INVALID_ARG from the Request constructor) and, after delivering them through onResponseError, returned false. By convention a false return means the dispatcher has queued the request internally and is saturated: a 'drain' event will be emitted once it has capacity again. But a synchronously rejected request was never queued, consumed no capacity, and can never trigger a drain. PoolBase trusted the false return and permanently marked the idle client as needing drain (kNeedDrain), so with limited connections the client slot was never returned to rotation and every subsequent request waited in the pool queue forever with nothing running and no event left to resume it (nodejs/undici#6054). Return true instead: the dispatcher did not take ownership of the request and remains available. This fixes all three PoolBase call sites (kDispatch, kOnDrain, kDrainQueue) and Agent at once, at the layer that owns the dispatch contract. --- lib/dispatcher/dispatcher-base.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/dispatcher/dispatcher-base.js b/lib/dispatcher/dispatcher-base.js index 97a4db6e321..7261fd17a90 100644 --- a/lib/dispatcher/dispatcher-base.js +++ b/lib/dispatcher/dispatcher-base.js @@ -190,7 +190,12 @@ class DispatcherBase extends Dispatcher { handler.onResponseError(null, err) - return false + // The request failed synchronously: no work was queued on this + // dispatcher, no capacity was consumed and no 'drain' event will + // ever follow. Returning false would make callers (e.g. Pool) + // mark this dispatcher as busy waiting for a drain that never + // comes, permanently taking it out of rotation. + return true } } } From aa31568f0e7a1dedbea9aca1e136cbbc01c33a2f Mon Sep 17 00:00:00 2001 From: RegretSSS <38377607+RegretSSS@users.noreply.github.com> Date: Sun, 8 Nov 2026 10:51:20 +0800 Subject: [PATCH 3/3] test: pool stays usable after a request validation error Regression tests for nodejs/undici#6054, adapted from the reporter's reproduction and following the existing test/pool.js conventions: - a synchronously rejected request (invalid header) must not take the idle client out of rotation: the pool keeps serving requests with connections: 1 and the client slot is not consumed - a queued request that fails validation when the pool drains its queue must not end the drain loop or mark the just-drained client busy, leaving later queued requests stuck Replaces test/issue-6054-repro.js, whose scenario is now covered here. --- test/issue-6054-repro.js | 49 ---------------------- test/pool.js | 88 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 49 deletions(-) delete mode 100644 test/issue-6054-repro.js diff --git a/test/issue-6054-repro.js b/test/issue-6054-repro.js deleted file mode 100644 index 3051bc111a3..00000000000 --- a/test/issue-6054-repro.js +++ /dev/null @@ -1,49 +0,0 @@ -'use strict' - -// nodejs/undici#6054 —— 报告者复现(已验证在当前 main 上 FAIL:Pool 永久卡死) -const { test } = require('node:test') -const assert = require('node:assert/strict') -const { createServer } = require('node:http') -const { once } = require('node:events') -const { setImmediate: nextTurn } = require('node:timers/promises') -const { Pool } = require('../index.js') - -test('Pool remains usable after a request validation error', { timeout: 5000 }, async (t) => { - let received = 0 - const server = createServer((req, res) => { - received++ - res.end('ok') - }) - server.listen(0, '127.0.0.1') - await once(server, 'listening') - - const pool = new Pool(`http://127.0.0.1:${server.address().port}`, { connections: 1 }) - t.after(async () => { - await pool.destroy() - server.closeAllConnections() - await new Promise(resolve => server.close(resolve)) - }) - - const warm = await pool.request({ path: '/', method: 'GET' }) - assert.equal(await warm.body.text(), 'ok') - await nextTurn() - assert.equal(pool.stats.free, 1) - - await assert.rejects( - pool.request({ path: '/', method: 'GET', headers: { 'x-invalid': '\n' } }), - { code: 'UND_ERR_INVALID_ARG' } - ) - - const next = pool.request({ path: '/', method: 'GET' }) - .then(async ({ body }) => body.text()) - let timer - const outcome = await Promise.race([ - next, - new Promise(resolve => { timer = setTimeout(() => resolve('TIMEOUT'), 500) }) - ]).finally(() => clearTimeout(timer)) - - const { connected, free, running, pending, queued, size } = pool.stats - console.log({ outcome, received, connected, free, running, pending, queued, size }) - assert.equal(outcome, 'ok') - assert.equal(received, 2) -}) diff --git a/test/pool.js b/test/pool.js index 3c2c360ea1f..2c0e2723050 100644 --- a/test/pool.js +++ b/test/pool.js @@ -953,6 +953,94 @@ test('pool dispatch error', async (t) => { await t.completed }) +test('pool remains usable after a request validation error', async (t) => { + t = tspl(t, { plan: 5 }) + + const server = createServer({ joinDuplicateHeaders: true }, (req, res) => { + res.end('ok') + }) + after(() => server.close()) + + await new Promise(resolve => server.listen(0, resolve)) + + const pool = new Pool(`http://localhost:${server.address().port}`, { + connections: 1, + pipelining: 1 + }) + after(() => pool.destroy()) + + // Warm the pool so that a single idle client is in rotation. + { + const { statusCode, body } = await pool.request({ path: '/', method: 'GET' }) + t.strictEqual(statusCode, 200) + await body.text() + } + + // A synchronously rejected dispatch must not take the idle client out of + // rotation waiting for a drain event that never comes. + // See https://github.com/nodejs/undici/issues/6054 + await t.rejects( + pool.request({ path: '/', method: 'GET', headers: { 'x-invalid': '\n' } }), + { code: 'UND_ERR_INVALID_ARG' } + ) + + const { statusCode, body } = await Promise.race([ + pool.request({ path: '/', method: 'GET' }), + new Promise((_resolve, reject) => { + setTimeout(() => reject(new Error('pool stalled after a validation error')), 1000) + }) + ]) + t.strictEqual(statusCode, 200) + await body.text() + + // The client slot was never consumed, so it stays available. + t.strictEqual(pool.stats.connected, 1) + t.strictEqual(pool.stats.size, 0) +}) + +test('pool keeps draining the queue when a queued request fails validation', async (t) => { + t = tspl(t, { plan: 2 }) + + const server = createServer({ joinDuplicateHeaders: true }, async (req, res) => { + if (req.url === '/slow') { + await new Promise(resolve => setTimeout(resolve, 20)) + } + res.end('ok') + }) + after(() => server.close()) + + await new Promise(resolve => server.listen(0, resolve)) + + const pool = new Pool(`http://localhost:${server.address().port}`, { + connections: 1, + pipelining: 1 + }) + after(() => pool.destroy()) + + // The first request occupies the only client, the following two are queued + // by the pool. When the client drains, the invalid request must fail + // without ending the drain loop or marking the idle client busy. + const slow = pool.request({ path: '/slow', method: 'GET' }) + const invalid = pool.request({ path: '/', method: 'GET', headers: { 'transfer-encoding': 'fail' } }) + const valid = pool.request({ path: '/', method: 'GET' }) + + { + const { body } = await slow + await body.text() + } + + await t.rejects(invalid, { code: 'UND_ERR_INVALID_ARG' }) + + const { statusCode, body } = await Promise.race([ + valid, + new Promise((_resolve, reject) => { + setTimeout(() => reject(new Error('pool stalled after a queued validation error')), 1000) + }) + ]) + t.strictEqual(statusCode, 200) + await body.text() +}) + test('pool request abort in queue', async (t) => { t = tspl(t, { plan: 3 })