fix(microservices): route message handler rejections to handleError - #17785
Merged
kamilmysliwiec merged 1 commit intoSep 17, 2026
Merged
Conversation
follow-up to nestjs#17737, which routed the tcp and rmq handleMessage rejections to handleError bcs an unhandled promise rejection terminates the process the mqtt, nats and redis servers hand the same async handleMessage to their client libraries with no catch, so the identical rejections still escape: mqtt.js and node-redis get the handler through an EventEmitter listener and nats.js calls the subscription callback without awaiting it, so a malformed packet that throws in the deserializer, or a handler that rejects, kills the whole process the three handlers now route rejections through handleError like tcp and rmq already do, so a bad packet logs an error instead of crashing the server
Member
|
lgtm |
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?
#17737fixed the unhandled rejection on tcp and rmq, the same shape is still open on the other three transports:mqttClient.on('message', this.getMessageHandler(mqttClient).bind(this)), an EventEmitter listener, the returned promise is never observedclient.subscribe(channel, { callback: ... }), nats-core callssub.callback(null, msg)without awaiting itsubClient.on('message' | 'pmessage', this.getMessageHandler(pubClient).bind(this)), same EventEmitter storyhandleMessagerejects on paths that get hit in normal operation: the deserializer await runs with nothing around it, and the handler await sits insideonProcessingStartHookwhose default is(id, ctx, done) => done(), so a rejection propagates straight out of the basehandleEventtoo, meaning one malformed packet from the wire, or an event handler that rejects, is enough to kill the whole server processIssue Number: N/A
Steps to reproduce
git checkout master -- packages/microservices/server/server-mqtt.ts packages/microservices/server/server-nats.ts packages/microservices/server/server-redis.tsandnpx vitest run packages/microservices/test/server/server-mqtt.spec.ts packages/microservices/test/server/server-nats.spec.ts packages/microservices/test/server/server-redis.spec.ts -t "leaving them unhandled"handleMessagerejection tohandleError, like the tcp and rmq handlers already do since fix: absolute-form middleware bypass (fastify) and unserializable pattern crash (microservices) #17737What is the new behavior?
the mqtt, nats and redis message handlers route rejections through
handleErrorexactly like tcp and rmq do since #17737, so a bad packet logs an error instead of crashing the processDoes this PR introduce a breaking change?
Other information
same bug class as #17737, just the transports it left out, kafka is deliberately not touched bcs kafkajs reports handler failures itself, same reason #17737 skipped it, successful messages behave exactly as before, only the paths that used to crash the process now log an error