Fix silent consumer death after broker events and connection recovery#82
Merged
blckmn merged 1 commit intoblckmn:masterfrom Apr 1, 2026
Merged
Fix silent consumer death after broker events and connection recovery#82blckmn merged 1 commit intoblckmn:masterfrom
blckmn merged 1 commit intoblckmn:masterfrom
Conversation
- Add ConsumerCancelled and Shutdown event handlers on AsyncEventingBasicConsumer so broker-initiated cancellations (HA failover, queue policy, memory alarm) are detected instead of silently dropping the consumer - Subscribe to IAutorecoveringConnection.RecoverySucceeded to invalidate the stale channel reference after automatic connection recovery, and re-register the consumer via a new OnRecovered() virtual method - Replace non-thread-safe _timer.Enabled check in RestartIn() with Interlocked.CompareExchange to prevent race conditions where concurrent error threads either both enter or both skip the restart path - Guard BasicNack in the RestartConnection error path with a try/catch and channel.IsClosed check to prevent an exception cascade that silently kills the restart timer - Add error handling in TimerActivation() for the NackOnException path so a closed channel during batch nack falls back to a full restart instead of throwing - Clean up connection event handler subscriptions in ClearConnection() and dispose the timer in Cleanup() to prevent resource leaks - Add BasicRabbitService(config, logger) constructor overload for connection-level logging without breaking existing subclasses using the single-arg constructor
blckmn
approved these changes
Apr 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes an issue where consumers silently stop receiving messages from RabbitMQ after a random period (hours to days), requiring a service restart to recover.
Root cause appears to be a combination of bugs in the consumer lifecycle:
ConsumerCancelled/Shutdownhandlers - when the broker cancels a consumer (HA failover, queue policy change, memory alarm), the consumer goes deaf with zero errors loggedAutomaticRecoveryEnabledrecovers the connection, but the cached_channelfield still points to the old dead channel. NoRecoverySucceededhandler existed to invalidate itRestartIn()- the_timer.Enabledguard was not atomic, allowing concurrent threads to either both enter or both skip the restart pathBasicNackafter a channel close threw an exception that was caught by the outer handler, which calledRestartIn()again, but the timer was already running so the second call was a no-op. The consumer never restarted_stoppingguard on event handlers -ConsumerCancelled/Shutdownevents also fire during intentionalStop()/Dispose(), which would schedule an unwanted restart. A volatile flag distinguishes intentional teardown from broker-initiated disconnectsAll fixes are internal to
BasicRabbitServiceandQueueService. No changes to public interfaces, method signatures, configuration models, or enum values.Test plan
Consumer cancelled by broker on queue MyQueue, tags: Subscriber. Scheduling restart.Consumer started on queue MyQueue