How it works
AbortControllerModule registers one middleware, AbortControllerMiddleware. For every matching request it:
- creates a new native
AbortController; - stores it on the Node request under the symbol keys
ABORT_CONTROLLERandABORT_SIGNAL(plus the deprecatedreq.abortController/req.abortSignalproperties); - listens to the response:
finish: the response was fully sent. The request is marked finished and the signal will never abort.closebeforefinish: the connection went away early. The signal aborts withnew AbortError('Client disconnected', 'client-disconnected').
- starts a timer if
timeout > 0. When it fires, the signal aborts withnew AbortError('Request timed out after <timeout>ms', 'timeout'). The timer is cleared onfinishandclose.
@NestAbortSignal() then reads req[ABORT_SIGNAL] (on Fastify, req.raw[ABORT_SIGNAL]) and hands it to your handler.
request ──► middleware: new AbortController, start timer │ ├─ handler runs, passes signal to fetch / db / loops │ ├─ response finish ──► done, timer cleared, signal stays clean ├─ connection close early ──► abort('client-disconnected') └─ timer fires ──► abort('timeout')Why the response, not the request
Section titled “Why the response, not the request”Earlier versions listened to the request stream. A request with a body (POST, PUT, PATCH) emits close as soon as
the body has been read, so those signals aborted while the client was still waiting. Since 1.3 the middleware watches
the response, which only closes early when the client really leaves. The end-to-end tests cover POST requests on
both Express and Fastify.
What aborting does and does not do
Section titled “What aborting does and does not do”Aborting sets signal.aborted, fills signal.reason and fires the abort event. Everything that listens stops:
fetch rejects, Node APIs reject, throwIfAborted() throws. Nothing else happens. The response is not ended and
the handler is not interrupted between two awaits that ignore the signal. Pass the signal down; see
Services and external calls.
Routes
Section titled “Routes”forRoot() applies the middleware to '*'. forRoutes() takes a path or a list of paths in NestJS 11 syntax
('/api/*path'). Routes outside that list get no signal: @NestAbortSignal() returns undefined there.
See Configuration.