Skip to content

fix(core): stop the Observable producer when the client disconnects - #17826

Merged
kamilmysliwiec merged 3 commits into
nestjs:masterfrom
xia-chao:fix/observable-disconnect
Sep 22, 2026
Merged

kamilmysliwiec merged 3 commits into
nestjs:masterfrom
xia-chao:fix/observable-disconnect

Conversation

@xia-chao

@xia-chao xia-chao commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

A route that returns an Observable keeps producing after the client closes the connection. transformToResult resolves it with lastValueFrom and never watches the connection, so timers and in flight work keep going until the Observable finishes on its own. If it never finishes, it never stops.

The SSE path already handles this, it unsubscribes the producer when the client goes away. A plain GET route does not, so this is the same fix for that other path.

Issue Number: N/A

What is the new behavior?

transformToResult now receives the response and unsubscribes the producer when the connection closes, using the same socket the SSE path already watches. If the client is already gone, nothing is written back. While the client stays connected, nothing changes.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

I reproduced it with a small app whose route returns interval(40). The connection was cut at 2 ticks, and 220ms later the counter was at 7. With this change it stays at 2, on both Express and Fastify.

To run the tests: npx vitest run packages/core/test/router/router-response-controller.spec.ts

A route handler returning a long-lived Observable kept producing after the
client was gone. `RouterResponseController.transformToResult()` resolved
handler Observables with `lastValueFrom()` and never tied the subscription to
the connection, while `sse()` already unsubscribed its producer on `'close'`.

`transformToResult()` now accepts the response, unsubscribes the producer when
the connection closes (same `socket ?? raw` disconnect source `sse()` uses,
so Express and Fastify both work), settles quietly instead of leaking
`EmptyError` into the exception filters, and reports through
`isResponseClosed()` so no response is written to a socket that is already
gone. `redirect()` and `render()` pass their response through as well.

Behaviour while the client stays connected is unchanged.
@xia-chao

Copy link
Copy Markdown
Contributor Author

Left the never-completing case alone, it has no single value to send back.
@micalevisk You can review it

Only tear the response pipeline down on client disconnect when the route
handler itself returned (or resolved to) an Observable. Interceptors always
wrap the result in an Observable, so the handler reports its own return
value from where it runs. Handlers that return anything else keep the
previous behavior with or without interceptors: interceptor work runs to
completion and late errors still reach the exception filters.

Watch the request socket like sse() does, and leave
RouterResponseController, redirect() and render() unchanged.
@kamilmysliwiec
kamilmysliwiec merged commit 6b282d3 into nestjs:master Sep 22, 2026
4 checks passed
kamilmysliwiec added a commit that referenced this pull request Oct 16, 2026
ServerTCP subscribed to the Observable a handler returned and never
unsubscribed. After the peer disconnected, a stream that does not
complete on its own (an interval, a Subject) kept running and producing
values for nobody, holding on to the request and its socket, and it
kept the process alive after close().

Each accepted socket now has a notifier for its "close" and "error"
events, replayed for handlers that settle later, and the response
stream ends on it once the handler itself returned (or resolved to) an
Observable, the policy of #17826 for HTTP. Value and Promise handlers,
and their interceptors, still run to completion. As interceptors and
pre-request hooks wrap the result, RpcContextCreator reports the
handler's own Observable by the context it was called with.

Discovered by Anthropic's OSS Scanner, as bug ANT-2026-QHTH4HKE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants