Skip to content

Detect deleted rabbitMQ queues and re-declare them - #838

Merged
LincolnPuzey merged 5 commits into
Bogdanp:masterfrom
LincolnPuzey:fix_832
Apr 8, 2026
Merged

LincolnPuzey merged 5 commits into
Bogdanp:masterfrom
LincolnPuzey:fix_832

Conversation

@LincolnPuzey

@LincolnPuzey LincolnPuzey commented Feb 21, 2026 •

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
Collaborator Author

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

@themavik themavik left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Threading broker into _RabbitmqConsumer and pushing 404/UnroutableError back through queues_pending covers re-declare on both consume and publish when confirm_delivery is on. nit: new test comment says conform_delivery; should read confirm_delivery.

@LincolnPuzey
LincolnPuzey merged commit 0c1b8dd into Bogdanp:master Apr 8, 2026
15 checks passed
@LincolnPuzey
LincolnPuzey deleted the fix_832 branch April 8, 2026 13:36
@LincolnPuzey LincolnPuzey added this to the v2.2.0 milestone Apr 8, 2026
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

2 participants