Skip to content

fix(message-bus): swallow the CANCELLED error when unsubscribing from Rusi - #60

Merged
fraliv13 merged 1 commit into
mainfrom
fix/rusi-unsubscribe-cancelled
Nov 12, 2026
Merged

fraliv13 merged 1 commit into
mainfrom
fix/rusi-unsubscribe-cancelled

Conversation

@fraliv13

Copy link
Copy Markdown
Contributor

Problem

With the Rusi transport, every sendCommandAndReceiveEvent crashes the process once the reply event has been received:

ERROR: Received: uncaughtException
  "message": "1 CANCELLED: Cancelled on client"
  at Object.onReceiveStatus (@grpc/grpc-js/build/src/client.js:421:73)
  ...
  at async Object.sendCommandAndReceiveEvent (@totalsoft/message-bus/dist/src/messageBus.js:86:31)
Messaging Host is shutting down...
  1. The reply event arrives and the promise resolves, so the finally block calls subscription.unsubscribe().
  2. unsubscribe removes the stream's 'error' / 'end' listeners and then calls call.cancel().
  3. After a client-side cancel, grpc-js always emits 'error' with status 1 CANCELLED (client.js → onReceiveStatus).
  4. Nothing is listening for 'error' any more, so Node throws. graceful-shutdown treats the resulting uncaughtException as unrecoverable and shuts the host down.

This started with #51, which began removing the 'error' listener before cancel(). Before #51, the listener stayed on the stream and just logged the error.

Fix

In unsubscribe, attach a no-op 'error' listener before calling cancel(). The CANCELLED error is swallowed and isn't passed on to the subscription's own error listeners. Everything else from #51 is unchanged.

Tests

  • New test: a stream that emits CANCELLED on cancel(), as grpc-js does. It failed before the fix with Unhandled error ... CANCELLED and passes now.
  • All 42 message-bus tests pass, and tsc --noEmit is clean.

Includes a patch changeset for @totalsoft/message-bus.

Not in this PR

After unsubscribe cancels the stream, the 'data' handler still tries to ack the reply on it, so the sidecar logs "message was not ACK". That's harmless for a short-lived RPC subscription and is left as is.

🤖 Generated with Claude Code

… Rusi

grpc-js emits 'error' (1 CANCELLED: Cancelled on client) on a duplex
stream after call.cancel(). Since #51, unsubscribe removed the stream's
'error' listener before cancelling, so the event had no listener and
became an uncaughtException, which shuts down the messaging host after
every sendCommandAndReceiveEvent.

Attach a no-op 'error' listener before cancelling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fraliv13
fraliv13 merged commit c4b183c into main Nov 12, 2026
1 check passed
@fraliv13
fraliv13 deleted the fix/rusi-unsubscribe-cancelled branch November 12, 2026 14:13
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