Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 18 additions & 2 deletions lib/dispatcher/pool-base.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions test/pool-async-context.js
Original file line number Diff line number Diff line change
@@ -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' })
})