Repository navigation
Description
Is there an existing issue for this?
- I have searched the existing issues
Current behavior
Hi! 👋 This is the sibling of #17958, which was about ClientNats.
I give a client a custom deserializer that expects a certain reply shape:
ClientsModule.register([
{
name: 'ORDERS',
transport: Transport.REDIS,
options: {
deserializer: {
deserialize: (value) => ({ id: value.meta.id, response: value.body }),
},
},
},
]);When a reply does not match that shape, the deserializer throws. The client then crashes the whole process with exit code 1:
TypeError: Cannot read properties of undefined (reading 'id')
This happens in ClientTCP, ClientRedis, ClientMqtt and ClientRMQ. Each client awaits the deserializer inside a listener that nobody awaits, so the rejection is unhandled. I also tried TCP with an unhandledRejection handler. The process stays up, but the request never settles.
The default deserializer never throws, so only a custom deserializer triggers this. On Redis and MQTT, the reply topic is shared. So any publisher on that topic can send a reply that crashes the client.
Minimum reproduction code
https://github.com/johnnyhuirilef/nestjs-repros/tree/main/client-custom-deserializer-crash
Steps to reproduce
The README lists the docker run commands for Redis, Mosquitto and RabbitMQ. TCP needs no broker.
npm installnpm run repro
The script runs each client in its own child process. A plain responder answers once with a reply that the deserializer rejects, and once with a valid reply. Here is a shortened copy of the output:
=== TCP: reply the deserializer rejects ===
outcome: crashed (process exit code 1)
stderr line: TypeError: Cannot read properties of undefined (reading 'id')
=== REDIS: reply the deserializer rejects ===
... same, outcome: crashed (process exit code 1)
=== MQTT: reply the deserializer rejects ===
... same, outcome: crashed (process exit code 1)
=== RMQ: reply the deserializer rejects ===
outcome: crashed (process exit code 1)
RESULT: BUG: 4 of 8 cases did not end as expected (a crashed client exits with code 1).
The four valid replies resolve with "ok".
Expected behavior
A deserializer error does not crash the process. When the client knows which request the reply belongs to, the request fails with the error, as #17959 did for ClientNats.
NestJS version
12.1.2
Packages versions
{
"@nestjs/common": "12.1.2",
"@nestjs/core": "12.1.2",
"@nestjs/microservices": "12.1.2",
"ioredis": "6.0.0",
"mqtt": "5.16.0",
"amqplib": "2.2.0",
"amqp-connection-manager": "5.0.0"
}Node.js version
24.19.0
In which operating systems have you tested?
- macOS
- Windows
- Linux
Other
The listeners are ClientTCP.handleResponse(), the response callbacks of ClientRedis and ClientMqtt, and ClientRMQ.handleMessage(). None of them catches an error from deserialize().
I have a fix ready and I can open a PR.
Activity
fix(microservices): survive custom deserializer errors in clients (#1…