Repository navigation
fix(microservices): close the request-response span exactly once - #17794
Merged
kamilmysliwiec merged 1 commit intoSep 17, 2026
Merged
Conversation
follow-up to #17781, which fixed the same leak on the event path on the request-response path the end hook lives in the publisher (`ServerMqtt`/`ServerNats`/`ServerRedis#getPublisher`, the inline publisher in `ServerTCP#handleMessage` and `ServerRMQ#sendMessage`), which `Server#send` invokes once per emitted packet and only after `await handler(...)` resolves, so the span is mismanaged in two ways: - a handler that returns a multi-value observable closes the span once per emitted value, so a stream of three responses closes it three times - a handler that rejects never reaches the publisher at all, so the span stays open forever and an APM exporter accumulates spans it never exports both are fixed the way #17781 fixed the event path: the hook moves out of the publisher and onto the response stream's `finalize` teardown, guarded by the shared `Server#createProcessingEndHookRunner`, and the handler call is wrapped in try/catch so a rejection closes the span before it is rethrown as a side effect the `NO_MESSAGE_HANDLER` path in mqtt, nats and redis no longer fires an end hook: it never ran a start hook, so that call was unpaired (tcp and rmq already did not fire one there) Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
MingLin1995
added a commit
to MingLin1995/nest
that referenced
this pull request
Sep 17, 2026
follow-up to nestjs#17794, which moved the end hook onto the response stream for mqtt, nats, redis, tcp and rmq but left the kafka publisher untouched `ServerKafka` still runs `onProcessingEndHook` from `sendMessage`, which `Server#send` invokes once per emitted packet, so the span is mismanaged in three ways: - a handler returning a multi-value observable closes the span once per published reply - `combineStreamsAndThrowIfRetriable` rejects on a `KafkaRetriableException`, so `this.send` is never reached and the span stays open; kafkajs then redelivers the message, and every retry leaks another span - the `NO_MESSAGE_HANDLER` path publishes without ever running a start hook, so that end hook call is unpaired all three are fixed the way nestjs#17781 and nestjs#17794 did it: the hook moves out of `sendMessage` and onto the reply stream's `finalize` teardown, guarded by the shared `Server#createProcessingEndHookRunner`, and the handler call is wrapped in try/catch so a retriable rejection closes the span before it is rethrown as on the other transports the span now closes once processing has settled rather than after the last reply has been produced
4 of 6 tasks
kamilmysliwiec
added a commit
that referenced
this pull request
Sep 17, 2026
#17797) * fix(microservices): close the kafka request-response span exactly once follow-up to #17794, which moved the end hook onto the response stream for mqtt, nats, redis, tcp and rmq but left the kafka publisher untouched `ServerKafka` still runs `onProcessingEndHook` from `sendMessage`, which `Server#send` invokes once per emitted packet, so the span is mismanaged in three ways: - a handler returning a multi-value observable closes the span once per published reply - `combineStreamsAndThrowIfRetriable` rejects on a `KafkaRetriableException`, so `this.send` is never reached and the span stays open; kafkajs then redelivers the message, and every retry leaks another span - the `NO_MESSAGE_HANDLER` path publishes without ever running a start hook, so that end hook call is unpaired all three are fixed the way #17781 and #17794 did it: the hook moves out of `sendMessage` and onto the reply stream's `finalize` teardown, guarded by the shared `Server#createProcessingEndHookRunner`, and the handler call is wrapped in try/catch so a retriable rejection closes the span before it is rethrown as on the other transports the span now closes once processing has settled rather than after the last reply has been produced * fix(microservices): settle the kafka reply when the stream completes --------- Co-authored-by: Kamil Myśliwiec <mail@kamilmysliwiec.com>
5 of 7 tasks
mdrezuanislamridoy
pushed a commit
to mdrezuanislamridoy/nest-1
that referenced
this pull request
Oct 27, 2026
mdrezuanislamridoy
pushed a commit
to mdrezuanislamridoy/nest-1
that referenced
this pull request
Nov 4, 2026
mdrezuanislamridoy
pushed a commit
to mdrezuanislamridoy/nest-1
that referenced
this pull request
Nov 5, 2026
mdrezuanislamridoy
pushed a commit
to mdrezuanislamridoy/nest-1
that referenced
this pull request
Nov 5, 2026
mdrezuanislamridoy
pushed a commit
to mdrezuanislamridoy/nest-1
that referenced
this pull request
Nov 16, 2026
ttt43ttt
pushed a commit
to NoDevelopers/ts-monorepo
that referenced
this pull request
Nov 22, 2026
| datasource | package | from | to | | ---------- | ------------------------ | ------ | ------ | | npm | @nestjs/common | 12.0.1 | 12.1.0 | | npm | @nestjs/core | 12.0.1 | 12.1.0 | | npm | @nestjs/platform-express | 12.0.1 | 12.1.0 | ## [v12.1.0](https://github.com/nestjs/nest/releases/tag/v12.1.0) ##### v12.1.0 (2026-09-23) ##### Bug fixes - `core` - [#17838](nestjs/nest#17838) fix(core): inherit optional ctor params when the ctor is not redeclared ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - [#17819](nestjs/nest#17819) fix(core): name the module when metadata entries are invalid ([@thekhegay](https://github.com/thekhegay)) - `microservices` - [#17828](nestjs/nest#17828) fix(microservices): let the redis client retry after a failed connect ([@hktitof](https://github.com/hktitof)) - [#17810](nestjs/nest#17810) fix(microservices): fail every pending request when a callback throws ([@hktitof](https://github.com/hktitof)) ##### Enhancements - `core` - [#17846](nestjs/nest#17846) fix(core): do not skip subsequent middleware after a transient one ([@lehafari](https://github.com/lehafari)) - [#17840](nestjs/nest#17840) fix(core): preserve route handler names ([@Grmiade](https://github.com/Grmiade)) - `common`, `core` - [#17713](nestjs/nest#17713) feat(core): add support for array of global prefixes ([@micalevisk](https://github.com/micalevisk)) - [#17834](nestjs/nest#17834) feat(common,core): add built-in, adapter-agnostic cookies ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - `platform-fastify` - [#17824](nestjs/nest#17824) fix(fastify): honor Nest forceCloseConnections in initHttpServer ([@xia-chao](https://github.com/xia-chao)) - `common` - [#17827](nestjs/nest#17827) fix(common): apply the ParseDatePipe default to every missing value ([@xia-chao](https://github.com/xia-chao)) - `platform-fastify`, `testing` - [#17835](nestjs/nest#17835) feat(fastify): add file upload interceptors backed by [@fastify/multipart](https://github.com/fastify/multipart) ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - `common`, `core`, `platform-fastify` - [#17836](nestjs/nest#17836) feat(common,core,fastify): built-in CSRF protection and security headers ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) ##### Committers: 9 - Abdellatif Anaflous ([@hktitof](https://github.com/hktitof)) - Alejandro ([@lehafari](https://github.com/lehafari)) - Chris Z ([@haimingZZ](https://github.com/haimingZZ)) - Johnny J. Huirilef Millán ([@johnnyhuirilef](https://github.com/johnnyhuirilef)) - Jérémy Fauvel ([@Grmiade](https://github.com/Grmiade)) - Kamil Mysliwiec ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - Micael Levi L. Cavalcante ([@micalevisk](https://github.com/micalevisk)) - Roman Khegay ([@thekhegay](https://github.com/thekhegay)) - Xia Chao ([@xia-chao](https://github.com/xia-chao)) ## [v12.0.4](https://github.com/nestjs/nest/releases/tag/v12.0.4) ##### v12.0.4 (2026-09-21) ##### Bug fixes - `microservices` - [#17816](nestjs/nest#17816) fix(microservices): let the kafka client retry after a failed connect ([@Xch4rt](https://github.com/Xch4rt)) - [#17786](nestjs/nest#17786) fix(microservices): fail pending rmq requests on client close ([@johnnyhuirilef](https://github.com/johnnyhuirilef)) - [#17802](nestjs/nest#17802) fix(microservices): fail pending nats requests on client close ([@hktitof](https://github.com/hktitof)) - [#17803](nestjs/nest#17803) fix(microservices): handle missing grpc package namespace gracefully ([@Se3do](https://github.com/Se3do)) - [#17799](nestjs/nest#17799) fix(microservices): share the processing hook envelope across transports ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - [#17797](nestjs/nest#17797) fix(microservices): close the kafka request-response span exactly once ([@MingLin1995](https://github.com/MingLin1995)) - [#17794](nestjs/nest#17794) fix(microservices): close the request-response span exactly once ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - [#17785](nestjs/nest#17785) fix(microservices): route message handler rejections to handleError ([@hktitof](https://github.com/hktitof)) - `core` - [#17821](nestjs/nest#17821) fix(core): hang on sparse factory inject array ([@xia-chao](https://github.com/xia-chao)) ##### Enhancements - `platform-express` - [#17818](nestjs/nest#17818) Fix: merge multer `limits` key-by-key instead of overriding it wholesale ([@mdrezuanislamridoy](https://github.com/mdrezuanislamridoy)) - `common` - [#17780](nestjs/nest#17780) fix(common): mark parse date pipe options as optional ([@Chy-Zaber-Bin-Zahid](https://github.com/Chy-Zaber-Bin-Zahid)) ##### Dependencies - `common` - [#17793](nestjs/nest#17793) fix(deps): update dependency file-type to v22.1.1 ([@renovate\[bot\]](https://github.com/apps/renovate)) - `platform-fastify` - [#17791](nestjs/nest#17791) fix(deps): update dependency fastify to v5.12.5 ([@renovate\[bot\]](https://github.com/apps/renovate)) ##### Committers: 10 - Abdellatif Anaflous ([@hktitof](https://github.com/hktitof)) - Chowdhury Zaber Bin Zahid ([@Chy-Zaber-Bin-Zahid](https://github.com/Chy-Zaber-Bin-Zahid)) - Imad - Prakticode ([@prakticode](https://github.com/prakticode)) - Johnny J. Huirilef Millán ([@johnnyhuirilef](https://github.com/johnnyhuirilef)) - Kamil Mysliwiec ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - MD. Ridoy Babu ([@mdrezuanislamridoy](https://github.com/mdrezuanislamridoy)) - Ming ([@MingLin1995](https://github.com/MingLin1995)) - Mohammed Said ([@Se3do](https://github.com/Se3do)) - Pablo Gutiérrez ([@Xch4rt](https://github.com/Xch4rt)) - Xia Chao ([@xia-chao](https://github.com/xia-chao)) ## [v12.0.3](https://github.com/nestjs/nest/releases/tag/v12.0.3) ##### v12.0.3 (2026-09-15) ##### Bug fixes - `microservices` - [#17766](nestjs/nest#17766) fix(microservices): always run the kafka processing end hook ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - [#17757](nestjs/nest#17757) fix(microservices): fail pending requests on client close ([@hktitof](https://github.com/hktitof)) - [#17746](nestjs/nest#17746) fix(microservices): bound memory held for a TCP peer ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - [#17745](nestjs/nest#17745) fix(microservices): destroy open TCP sockets on shutdown ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - `common` - [#17738](nestjs/nest#17738) fix(common): validate uuid version and variant when no version is set ([@otnc](https://github.com/otnc)) - [#17761](nestjs/nest#17761) fix(common): invoke proto-key stripping hook via this in transform ([@koreahghg](https://github.com/koreahghg)) ##### Dependencies - `platform-express` - [#17762](nestjs/nest#17762) fix(deps): update dependency multer to v2.4.0 ([@renovate\[bot\]](https://github.com/apps/renovate)) - `common` - [#17753](nestjs/nest#17753) fix(deps): update dependency file-type to v22.1.0 ([@renovate\[bot\]](https://github.com/apps/renovate)) ##### Committers: 4 - Abdellatif Anaflous ([@hktitof](https://github.com/hktitof)) - Kamil Mysliwiec ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - otoneko. ([@otnc](https://github.com/otnc)) - 한국 ([@koreahghg](https://github.com/koreahghg)) ## [v12.0.2](https://github.com/nestjs/nest/releases/tag/v12.0.2) ##### v12.0.2 (2026-09-14) ##### Bug fixes - `platform-socket.io`, `platform-ws` - [#17666](nestjs/nest#17666) fix(socket.io): keep the message stream alive when a handler throws ([@hktitof](https://github.com/hktitof)) - `microservices` - [#17728](nestjs/nest#17728) fix(microservices): detach signal listeners of connected microservices ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - [#17671](nestjs/nest#17671) fix(microservices): fail requests whose reply subscription is rejected ([@samuelmbabhazi](https://github.com/samuelmbabhazi)) - [#17670](nestjs/nest#17670) fix(microservices): do not treat foreign JSON as an RPC envelope ([@xia-chao](https://github.com/xia-chao)) - [#17644](nestjs/nest#17644) fix(microservices): don't flatten plain handler results with pre-request hooks ([@iamolegga](https://github.com/iamolegga)) - [#17641](nestjs/nest#17641) fix(microservices): run the mqtt listen callback once ([@johnnyhuirilef](https://github.com/johnnyhuirilef)) - `common` - [#17710](nestjs/nest#17710) fix(common): collapse duplicate internal slashes in `normalizePath` ([@Asadshah7950](https://github.com/Asadshah7950)) - [#17655](nestjs/nest#17655) fix(common): validate UUID v3 variant ([@gwagjiug](https://github.com/gwagjiug)) - [#17702](nestjs/nest#17702) fix(common): treat empty string as array input in ParseArrayPipe ([@xia-chao](https://github.com/xia-chao)) - `core` - [#17667](nestjs/nest#17667) fix(core): discover factory providers with discovery decorators ([@hktitof](https://github.com/hktitof)) - [#17709](nestjs/nest#17709) fix(core): avoid treating error instances with `statusCode` as http errors ([@Asadshah7950](https://github.com/Asadshah7950)) - [#17625](nestjs/nest#17625) fix(core): clean up repeated shutdown signal listeners ([@Raj4478](https://github.com/Raj4478)) - `platform-fastify` - [#17705](nestjs/nest#17705) fix(fastify): derive neutral version when accept header is absent ([@Gaurav1112](https://github.com/Gaurav1112)) - [#17699](nestjs/nest#17699) fix(fastify): send responses for exceptions thrown from middleware ([@micalevisk](https://github.com/micalevisk)) ##### Enhancements - `microservices` - [#17734](nestjs/nest#17734) feat(microservices): add maxConnectionAttempts to ServerMqtt ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) ##### Dependencies - `platform-fastify` - [#17729](nestjs/nest#17729) fix(deps): update dependency fastify to v5.12.4 ([@renovate\[bot\]](https://github.com/apps/renovate)) ##### Committers: 16 - Abdellatif Anaflous ([@hktitof](https://github.com/hktitof)) - Alexey Solovyov ([@lyohaplotinka](https://github.com/lyohaplotinka)) - Asad Ali Shah ([@Asadshah7950](https://github.com/Asadshah7950)) - Gyeonghun Park ([@Gyeonghun-Park](https://github.com/Gyeonghun-Park)) - Johnny J. Huirilef Millán ([@johnnyhuirilef](https://github.com/johnnyhuirilef)) - Kamil Mysliwiec ([@kamilmysliwiec](https://github.com/kamilmysliwiec)) - Kumar Gaurav ([@Gaurav1112](https://github.com/Gaurav1112)) - Micael Levi L. Cavalcante ([@micalevisk](https://github.com/micalevisk)) - Ming ([@MingLin1995](https://github.com/MingLin1995)) - Mohammed Said ([@Se3do](https://github.com/Se3do)) - Rajeshwar Singh ([@Raj4478](https://github.com/Raj4478)) - Xia Chao ([@xia-chao](https://github.com/xia-chao)) - [@iamolegga](https://github.com/iamolegga) - cui fliter ([@cuishuang](https://github.com/cuishuang)) - samuel mbabhazi ([@samuelmbabhazi](https://github.com/samuelmbabhazi)) - 곽지욱 ([@gwagjiug](https://github.com/gwagjiug))
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
PR Type
What is the current behavior?
Issue Number: N/A
Follow-up to #17781, which fixed this leak on the event path. It raised the same
problem on the request-response path; this is that fix.
On the request-response path the end hook lives in the publisher —
ServerMqtt#getPublisher,ServerNats#getPublisher,ServerRedis#getPublisher,the inline publisher in
ServerTCP#handleMessage, andServerRMQ#sendMessage.Server#sendinvokes that publisher once per emitted packet, and only afterawait handler(...)has resolved. Two things go wrong:1. A multi-value response closes the span once per value. A handler
returning an observable of three values publishes three packets, so
onProcessingEndHookruns three times for one request. Measured onmasterwith
ServerTCP:2. A rejecting handler never closes the span at all. The
awaitthrowsbefore the publisher is ever reached, so the span stays open forever:
Blast radius on the second one is narrow, for the reason #17781 documented:
handlers registered through the DI pipeline are wrapped by
RpcProxy, whichturns a rejection into an error observable, so
sendpublishes an{ err }packet and the span closes. What actually rejects is a custom exception filter
that throws and raw handlers registered via
addHandlerby custom transportstrategies. The first bug has no such escape hatch — any
@MessagePatternreturning a multi-value observable hits it.
What is the new behavior?
Both are fixed the way #17781 fixed the event path. The hook moves out of the
publisher and onto the response stream's
finalizeteardown, guarded by theshared
Server#createProcessingEndHookRunneradded in that PR, and the handlercall is wrapped in
try/catchso a rejection closes the span before the erroris rethrown. After the change, all four paths close the span exactly once:
12 specs across the five transports; 10 of them fail on
master. (The two thatpass there are the tcp single-value and stream-error cases, which already fired
once.)
This also lines the transports up with where the hook already fires elsewhere —
Server#handleEventusesfinalize,ServerKafka#sendMessageuses.finally(),and grpc closes in a
finally— so "the span closes when the handler's workfinishes" now holds everywhere.
One deliberate side effect: the
NO_MESSAGE_HANDLERpath in mqtt, nats andredis no longer fires an end hook. It never ran a start hook, so that call was
unpaired; tcp and rmq already did not fire one there.
The
ctx/contextparameters ofServerNats#getPublisher,ServerRedis#getPublisherandServerRMQ#sendMessageare now unused. I left thesignatures alone since they are public.
Does this PR introduce a breaking change?
Timing changes for anyone consuming
onProcessingEndHook: the hook now fireswhen the response stream completes rather than immediately before each write.
That is the same shift #17779 and #17781 made on the other paths.
Other information
Two things I did not change, both worth a separate call:
client waits out its own timeout with nothing on the wire. Publishing an
error packet on that path would be the complete fix; it changes wire
behaviour, so it should be its own PR.
NO_MESSAGE_HANDLERpath be instrumented at all? Right now itsits outside
onProcessingStartHookon every transport. Wrapping it wouldgive exporters a span for "request arrived, no handler", which seems more
useful than the current silence — but that is a design decision, not a bug
fix.
🤖 Generated with Claude Code