Skip to content

Detect deleted rabbitMQ queues and re-declare them#838

Open
LincolnPuzey wants to merge 5 commits intoBogdanp:masterfrom
LincolnPuzey:fix_832
Open

Detect deleted rabbitMQ queues and re-declare them#838
LincolnPuzey wants to merge 5 commits intoBogdanp:masterfrom
LincolnPuzey:fix_832

Conversation

@LincolnPuzey
Copy link
Collaborator

@LincolnPuzey LincolnPuzey commented Feb 21, 2026

Closes #832

This PR ensures that queues are re-declared if they are deleted while rabbitMQ is running.

It detects missing queues in two places:

  1. When consuming from them. In this case an error with reply_code==404 will be raised. This is now checked in the consumer.
  2. When publishing a message. NOTE this case is only possible when confirm_delivery is turned on. When this is the case, the mandatory flag is set, which causes an UnroutableError to be raised. This is checked for.

In both cases the missing queue is re-added to queues_pending. This causes it to be re-declared the next time _ensure_queue is called.



Previously this fix relied on the canonical queue being declared before the delayed one.
If for some reason the delayed queue is declared first, then the delayed queue name would get
added to self.queues.

This commit fixes it so this doesn't happen.
Only the canonical queue name is ever added to self.queues.

This commit also makes it more obvious that only canonical queue names should be stored in
self.queues and self.queues_pending by adding some comments
and renaming the arg to _ensure_queue.
This commit splits the existing test which didn't work, into two tests.
One for the consumer side, and one for the publishing side.
This ensures they don't interfere with each other.

Currently both fail, actual fixes should be in the next commits.
So that they are re-declared and the message is successfully published.

In order to detect a missing queue on publish you must (1) enable `confirm_delivery`
and (2) publish message with the `mandatory` flag.

This commit sets the `mandatory` flag, and then checks for the `UnroutableError`
raised as a result.

The old implementation checking for `reply_code` was wrong.
`reply_code` is only present on Connection/Channel close errors.
These never happen when publishing to a non-existent queue,
the Connection and Channel is not closed.
Instead of removing the queue from `self.queues`, which causes
before/after declare_queue middleware hooks to be called,

add the queue to `self.pending_queues` which just causes it to be re-declared inside rabbitMQ.
When consuming from a declared queue, and it is missing in rabbitMQ,
mark it as a "pending" queue so it is re-declared when the consumer restarts.

This requires passing the whole Broker object to the ConsumerThread, not just parameters.
@LincolnPuzey
Copy link
Collaborator Author

Hi @synweap15 @karolinepauls @Bogdanp This is making some changes in the RabbitMQ broker.
Would be good to get a review, thanks.

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.

RabbitMQ missing queue re-declaring does not work

1 participant