Repository navigation
fix(core): stop the Observable producer when the client disconnects - #17826
Merged
kamilmysliwiec merged 3 commits intoSep 22, 2026
Merged
Conversation
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.
Contributor
Author
|
Left the never-completing case alone, it has no single value to send back. |
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
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.
1 of 2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
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?
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