Skip to content

fix: correct disposal ordering to prevent ObjectDisposedException during shutdown#89

Merged
niemyjski merged 4 commits into
mainfrom
fix/dispose-cts-ordering
Jun 9, 2026
Merged

fix: correct disposal ordering to prevent ObjectDisposedException during shutdown#89
niemyjski merged 4 commits into
mainfrom
fix/dispose-cts-ordering

Conversation

@niemyjski

Copy link
Copy Markdown
Member

Summary

  • Reorder AzureServiceBusQueue.Dispose() and DisposeAsync() to call SignalDispose() first (cancels the CTS token so background tasks observe shutdown), dispose resources, then call base.Dispose() last (which disposes the timer and CTS)
  • Add idempotency guard with diagnostic trace logging on re-entry for both sync and async paths

This prevents a potential ObjectDisposedException if the CancellationTokenSource is disposed while background tasks are still checking the token -- the same class of bug fixed in FoundatioFx/Foundatio.Redis#165.

Related

Test plan

  • Builds cleanly with dotnet build -p:ReferenceFoundatioSource=true
  • Existing queue tests continue to pass

niemyjski added 4 commits June 9, 2026 11:25
…ing shutdown

Call SignalDispose() before disposing resources to cancel background tasks
before the CancellationTokenSource is disposed. Move base.Dispose() to the
end to ensure the CTS stays alive while resources are being cleaned up.

Fixes same class of bug as FoundatioFx/Foundatio.Redis#165.
Collapse separate IsDisposed check + SignalDispose() call into single
atomic operation that eliminates the TOCTOU gap.
@niemyjski niemyjski merged commit b52320b into main Jun 9, 2026
3 of 4 checks passed
@niemyjski niemyjski deleted the fix/dispose-cts-ordering branch June 9, 2026 20:26
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