Skip to content

Fix NullReferenceException in rate-limited listener pause/resume#2254

Merged
jeremydmiller merged 1 commit intomainfrom
fix/rate-limit-null-guard
Mar 2, 2026
Merged

Fix NullReferenceException in rate-limited listener pause/resume#2254
jeremydmiller merged 1 commit intomainfrom
fix/rate-limit-null-guard

Conversation

@jeremydmiller
Copy link
Member

Summary

  • Guard PauseListenerContinuation.ExecuteAsync against null Envelope and null Listener, falling back to Envelope.Destination when the listener reference is unavailable
  • Apply defensive copy pattern in ListeningAgent.StopAndDrainAsync to capture Listener/_receiver into locals before nulling the fields, eliminating a TOCTOU race where concurrent callers could see null between check and use
  • Apply the same defensive copy in RabbitMqListener.StopAsync for _consumer and Channel
  • Catch ObjectDisposedException in listener disposal for rapid pause/stop cycles

Addresses the bug described in #2242: when multiple messages exceed a rate limit simultaneously, the first triggers PauseListenerContinuation which stops the listener (setting Listener = null), and subsequent messages hit a NullReferenceException on lifecycle.Envelope!.Listener!.Address.

Test plan

  • Unit tests for PauseListenerContinuation with null envelope and null listener
  • RabbitMQ E2E test sending 10 messages with a 1-permit/5s rate limit, verifying no NullReferenceException during concurrent pause/resume cycles
  • All existing CoreTests pass

🤖 Generated with Claude Code

…imiting

Guard against null Envelope and null Listener in PauseListenerContinuation
to prevent NRE when multiple rate-limited messages trigger concurrent
pause/resume cycles. Apply defensive copy pattern in ListeningAgent
StopAndDrainAsync and RabbitMqListener StopAsync to eliminate TOCTOU
race conditions where Listener/Channel could become null between check
and use.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

1 participant