Skip to content

Subscribe the queues again after a reconnect - #97

Merged
nikosd23 merged 2 commits into
masterfrom
fix/resubscribe-queues-after-reconnect
Nov 11, 2026
Merged

nikosd23 merged 2 commits into
masterfrom
fix/resubscribe-queues-after-reconnect

Conversation

@nikosd23

@nikosd23 nikosd23 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Problem

After a reconnect, rabbit-queue opens a new consume channel but the queues keep the old one, and their consumers die with it. Nothing subscribes them again unless the app creates its queues on connected. An app that doesn't (for example one built on a wrapper that only logs connected) reconnects, reports healthy, and has 0 consumers until it is restarted.

To reproduce: subscribe a queue, run rabbitmqctl close_all_connections, wait for connected, then run rabbitmqctl list_consumers. The queue has no consumer, and a published message is never handled.

Change

  • openConnections now rebinds every queue in this.queues to the new consume channel before it emits connected: declares the queue again, adds its exchange bindings again (now tracked on Queue), and subscribes its handler with the queue's own prefetch. The same Queue objects are reused, so references such as BaseQueueHandler.queue stay valid.
  • A queue that fails to subscribe is retried by the existing resubscribe loop. If the connection drops again mid-rebind, connected isn't emitted.
  • createQueue with a handler cancels an existing consumer for the same queue on the same channel first. Apps that follow the README and create their queues on connected replace the rebound consumer instead of adding a second one.
  • New option resubscribeOnReconnect (default true). With false, a reconnect leaves the queues alone, as in 5.12.0.
  • README: options block, connected example, changelog entry (5.13.x).

Behaviour change

Subscribing again on reconnect is now the default. The README described it as the app's job before. I kept that pattern safe (no duplicate consumers) and added the opt-out.

Testing

Against a local RabbitMQ 3.13.6: full suite passes (159 tests). 9 new reconnect tests cover both connections or only the consume connection dropping, repeated reconnects ending with 1 consumer, the queue's own prefetch, an unsubscribed queue staying unsubscribed, exclusive-queue bindings, consumers being back when connected fires, the retry path, and the opt-out. Without the ts/ changes, 7 of them fail.

Not run: npm run test-docker (RabbitMQ 4.x).

To do after merge

  • Version bump and release (the README assumes 5.13.x).

@nikosd23 nikosd23 changed the title Subscribe the queues again after a reconnect (PROD-93697) Subscribe the queues again after a reconnect Nov 11, 2026
nikosd23 and others added 2 commits November 11, 2026 09:52
A reconnect opens a new consume channel, but the queues kept the old one and
their consumers died with it. Apps that don't create their queues again on
'connected' kept running with no consumers and reported healthy.

openConnections now rebinds every queue to the new channel before emitting
'connected': declares it again, adds its exchange bindings again and subscribes
its handler with the queue's own prefetch. A queue that fails to subscribe is
retried by the resubscribe loop. createQueue with a handler cancels an
existing consumer on the same channel, so apps that do create their queues
again on 'connected' don't end up with two consumers.

Co-Authored-By: Claude <noreply@anthropic.com>
…esubscribe

On by default. With false, a reconnect leaves the queues alone and the app
creates them again on 'connected', as before.

Co-Authored-By: Claude <noreply@anthropic.com>
@nikosd23
nikosd23 force-pushed the fix/resubscribe-queues-after-reconnect branch from 4f7bbf3 to 6303b58 Compare November 11, 2026 07:52
@nikosd23
nikosd23 merged commit 15e236d into master Nov 11, 2026
12 of 13 checks passed
@nikosd23
nikosd23 deleted the fix/resubscribe-queues-after-reconnect branch November 11, 2026 09:42
@nikosd23 nikosd23 mentioned this pull request Nov 11, 2026
1 task done
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