From b0bd1e64ce50f4942ddcbbc1e3c2cf581d02580f Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Fri, 9 Oct 2026 09:54:11 +0200 Subject: [PATCH 1/3] fix(pool): preserve queued request async context --- lib/dispatcher/pool-base.js | 20 +++++++++++++++++-- test/pool-async-context.js | 39 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 test/pool-async-context.js diff --git a/lib/dispatcher/pool-base.js b/lib/dispatcher/pool-base.js index 4ab941568ad..612cfc5d511 100644 --- a/lib/dispatcher/pool-base.js +++ b/lib/dispatcher/pool-base.js @@ -1,5 +1,6 @@ 'use strict' +const { AsyncResource } = require('node:async_hooks') const DispatcherBase = require('./dispatcher-base') const FixedQueue = require('./fixed-queue') const { kConnected, kSize, kRunning, kPending, kQueued, kBusy, kFree, kUrl, kClose, kDestroy, kDispatch, kDispatchFailed } = require('../core/symbols') @@ -41,6 +42,21 @@ function needsDrain (dispatcher, dispatched) { return !dispatched && dispatcher[kDispatchFailed] !== true } +// Requests that wait in the Pool queue are dispatched later from whichever +// callback drains the queue. Capture the caller's async context so child +// dispatchers still run in it. +class QueuedRequest extends AsyncResource { + constructor (opts, handler) { + super('UNDICI_POOL_QUEUED_REQUEST') + this.opts = opts + this.handler = handler + } + + dispatch (dispatcher) { + return this.runInAsyncScope(dispatcher.dispatch, dispatcher, this.opts, this.handler) + } +} + /** * Closes every live client, including detached clients that are still * finishing requests of their own. @@ -95,7 +111,7 @@ class PoolBase extends DispatcherBase { break } pool[kQueued]-- - needDrain = needsDrain(client, client.dispatch(item.opts, item.handler)) + needDrain = needsDrain(client, item.dispatch(client)) } client[kNeedDrain] = needDrain @@ -221,7 +237,7 @@ class PoolBase extends DispatcherBase { if (!dispatcher) { this[kNeedDrain] = true - this[kQueue].push({ opts, handler }) + this[kQueue].push(new QueuedRequest(opts, handler)) this[kQueued]++ } else if (needsDrain(dispatcher, dispatcher.dispatch(opts, handler))) { dispatcher[kNeedDrain] = true diff --git a/test/pool-async-context.js b/test/pool-async-context.js new file mode 100644 index 00000000000..b2f8ceac769 --- /dev/null +++ b/test/pool-async-context.js @@ -0,0 +1,39 @@ +'use strict' + +const assert = require('node:assert') +const { AsyncLocalStorage } = require('node:async_hooks') +const { once } = require('node:events') +const { createServer } = require('node:http') +const { test } = require('node:test') +const { Client, Pool } = require('..') + +// https://github.com/nodejs/undici/issues/5981 +test('requests queued in a Pool keep their async context', async (t) => { + const storage = new AsyncLocalStorage() + const contexts = {} + + class ContextClient extends Client { + dispatch (opts, handler) { + contexts[opts.path] = storage.getStore() + return super.dispatch(opts, handler) + } + } + + const server = createServer((req, res) => res.end('ok')) + server.listen(0) + await once(server, 'listening') + t.after(() => server.close()) + + const pool = new Pool(`http://localhost:${server.address().port}`, { + connections: 1, + factory: (origin, opts) => new ContextClient(origin, opts) + }) + t.after(() => pool.close()) + + await Promise.all(['a', 'b', 'c'].map((name) => storage.run(name, async () => { + const { body } = await pool.request({ path: `/${name}`, method: 'GET' }) + await body.dump() + }))) + + assert.deepStrictEqual(contexts, { '/a': 'a', '/b': 'b', '/c': 'c' }) +}) From b0d32a8b7752fc4213a7852850ad62f5433e5cd6 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Fri, 9 Oct 2026 10:27:39 +0200 Subject: [PATCH 2/3] Revert "fix(pool): preserve queued request async context" This reverts commit b0bd1e64ce50f4942ddcbbc1e3c2cf581d02580f. --- lib/dispatcher/pool-base.js | 20 ++----------------- test/pool-async-context.js | 39 ------------------------------------- 2 files changed, 2 insertions(+), 57 deletions(-) delete mode 100644 test/pool-async-context.js diff --git a/lib/dispatcher/pool-base.js b/lib/dispatcher/pool-base.js index 612cfc5d511..4ab941568ad 100644 --- a/lib/dispatcher/pool-base.js +++ b/lib/dispatcher/pool-base.js @@ -1,6 +1,5 @@ 'use strict' -const { AsyncResource } = require('node:async_hooks') const DispatcherBase = require('./dispatcher-base') const FixedQueue = require('./fixed-queue') const { kConnected, kSize, kRunning, kPending, kQueued, kBusy, kFree, kUrl, kClose, kDestroy, kDispatch, kDispatchFailed } = require('../core/symbols') @@ -42,21 +41,6 @@ function needsDrain (dispatcher, dispatched) { return !dispatched && dispatcher[kDispatchFailed] !== true } -// Requests that wait in the Pool queue are dispatched later from whichever -// callback drains the queue. Capture the caller's async context so child -// dispatchers still run in it. -class QueuedRequest extends AsyncResource { - constructor (opts, handler) { - super('UNDICI_POOL_QUEUED_REQUEST') - this.opts = opts - this.handler = handler - } - - dispatch (dispatcher) { - return this.runInAsyncScope(dispatcher.dispatch, dispatcher, this.opts, this.handler) - } -} - /** * Closes every live client, including detached clients that are still * finishing requests of their own. @@ -111,7 +95,7 @@ class PoolBase extends DispatcherBase { break } pool[kQueued]-- - needDrain = needsDrain(client, item.dispatch(client)) + needDrain = needsDrain(client, client.dispatch(item.opts, item.handler)) } client[kNeedDrain] = needDrain @@ -237,7 +221,7 @@ class PoolBase extends DispatcherBase { if (!dispatcher) { this[kNeedDrain] = true - this[kQueue].push(new QueuedRequest(opts, handler)) + this[kQueue].push({ opts, handler }) this[kQueued]++ } else if (needsDrain(dispatcher, dispatcher.dispatch(opts, handler))) { dispatcher[kNeedDrain] = true diff --git a/test/pool-async-context.js b/test/pool-async-context.js deleted file mode 100644 index b2f8ceac769..00000000000 --- a/test/pool-async-context.js +++ /dev/null @@ -1,39 +0,0 @@ -'use strict' - -const assert = require('node:assert') -const { AsyncLocalStorage } = require('node:async_hooks') -const { once } = require('node:events') -const { createServer } = require('node:http') -const { test } = require('node:test') -const { Client, Pool } = require('..') - -// https://github.com/nodejs/undici/issues/5981 -test('requests queued in a Pool keep their async context', async (t) => { - const storage = new AsyncLocalStorage() - const contexts = {} - - class ContextClient extends Client { - dispatch (opts, handler) { - contexts[opts.path] = storage.getStore() - return super.dispatch(opts, handler) - } - } - - const server = createServer((req, res) => res.end('ok')) - server.listen(0) - await once(server, 'listening') - t.after(() => server.close()) - - const pool = new Pool(`http://localhost:${server.address().port}`, { - connections: 1, - factory: (origin, opts) => new ContextClient(origin, opts) - }) - t.after(() => pool.close()) - - await Promise.all(['a', 'b', 'c'].map((name) => storage.run(name, async () => { - const { body } = await pool.request({ path: `/${name}`, method: 'GET' }) - await body.dump() - }))) - - assert.deepStrictEqual(contexts, { '/a': 'a', '/b': 'b', '/c': 'c' }) -}) From 8e187cc1ebc5ca52b8f5bcb6101929e7f2e842ea Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Fri, 9 Oct 2026 10:27:53 +0200 Subject: [PATCH 3/3] docs: document unsupported async context --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 00000000000..6205632c2f7 --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# @nxtedition/undici + +Internal HTTP/1.1 client for Node.js 26. + +## Async context + +Async context propagation is not supported. This package deliberately does not +use `node:async_hooks`, create `AsyncResource` instances, or preserve +`AsyncLocalStorage` stores across internal queueing, dispatch, and callback +boundaries. + +Do not rely on transport callbacks running in the context that submitted a +request. Applications that need request-scoped state must carry it explicitly +outside the transport.