Menu

Keep Concurrent AI Requests Separate in NestJS with AsyncLocalStorage
📰

Keep Concurrent AI Requests Separate in NestJS with AsyncLocalStorage

DEV Community: node·Raju Dandigam·about 9 hours ago
#144UjWKo
#devtonodejs#devto#dev#request#work
Long article (1048 words) — no AI summary yet.
Reading 0:00
15s threshold

Two users start agent work at the same time. One closes the connection. The other request continues. In the logs, which operation owns each tool call, and which cancellation signal should each task observe?

A singleton service with a mutable currentOperationId cannot answer reliably under concurrency. The second request can overwrite the first request's identity while both are awaiting work.

The companion NestJS example gives each HTTP request its own asynchronous context and cancellation signal. Its test sends two real local HTTP requests, cancels one, and verifies that the other completes with its original identity. No model provider is needed to reproduce the ownership problem.

Store request ownership in the asynchronous chain

Nest documents AsyncLocalStorage as a way to retain context across asynchronous work.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.