Survive continuations posted after NetMQPoller disposal#11
Merged
Conversation
NetMQPoller installs NetMQSynchronizationContext on the poller thread, so awaiting code captures it and may post continuations after the poller is disposed. Post then threw ObjectDisposedException out of Task.Start, which the await machinery rethrows on a thread-pool thread, crashing the process. Post now falls back to the thread pool and Send executes the callback inline when the poller is disposed, including when disposal races the Task.Start call. Dispose additionally executes tasks that were scheduled between Stop() and Dispose() instead of discarding them with the task queue, which left their awaiters hanging forever; they run in order on a thread-pool thread so arbitrary continuations cannot block or deadlock the disposing thread. See #9 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The upstream merge reintroduced net472 into NetMQ.Tests' TargetFrameworks. The build step already pins net10.0, but dotnet test evaluated both frameworks and failed on the never-built net472 assembly, breaking CI on every push since. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Prevents fatal crashes and hung awaiters caused by NetMQSynchronizationContext continuations being posted after a NetMQPoller has stopped/disposed, by providing safe fallback execution paths and ensuring queued tasks aren’t silently dropped during disposal.
Changes:
NetMQSynchronizationContext.Postfalls back toThreadPoolwhen the poller is disposed (including dispose races duringTask.Start), avoiding uncatchable exceptions in the await continuation path.NetMQSynchronizationContext.Sendfalls back to inline execution on the calling thread in the same disposed/race scenarios.NetMQPoller.Disposesnapshots any remaining queued tasks and schedules them to run (in order) on the thread pool instead of discarding them.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/NetMQ/NetMQSynchronizationContext.cs | Adds disposed/race handling to Post/Send to avoid throwing and to ensure continuations still run. |
| src/NetMQ/NetMQPoller.cs | Drains and executes tasks left in m_tasksQueue during disposal so Stop→Dispose doesn’t strand awaiters. |
| src/NetMQ.Tests/NetMQPollerTest.cs | Adds regression tests for Post-after-dispose, Send-after-dispose, and tasks scheduled after Stop. |
| .github/workflows/CI.yml | Ensures CI tests run with net10.0 to match the build configuration. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Fixes #9
Problem
NetMQPollerinstallsNetMQSynchronizationContexton the poller thread, so awaiting code captures it and may post continuations after the poller is disposed.Postthen threwObjectDisposedExceptionout ofTask.Start, which the await machinery rethrows on a thread-pool thread — uncatchable by application code and fatal to the process (Sentry BLUEYE-APP-FH, and BLUEYE-APP-9G as the native SIGABRT on iOS). Separately, tasks scheduled betweenStop()andDispose()were discarded with the task queue, leaving their awaiters hanging forever.Changes
NetMQSynchronizationContext.Post: falls back toThreadPool.QueueUserWorkItemwhen the poller is disposed, including when disposal races theTask.Startcall (the faulted task's exception is observed so it cannot surface asUnobservedTaskException).NetMQSynchronizationContext.Send: executes the callback inline on the calling thread in the same situations.NetMQPoller.Dispose: captures tasks still inm_tasksQueue(by enumerating the backing queue —TryDequeuedepends on pair-socket signalling that is unavailable once the poller thread has exited) and executes them in order on a thread-pool thread, rather than discarding them. They are not run on the disposing thread, so arbitrary continuations cannot block or deadlock the caller ofDispose().Behavioural note: a fallback callback runs without the poller's thread affinity. At that point the poller is already disposed, so the affinity guarantee no longer protects anything — the alternative was a process crash or a hung awaiter, not a working continuation.
Tests
Three new tests in
NetMQPollerTestcovering Post-after-dispose, Send-after-dispose, and task-scheduled-after-Stop. Full Poller category passes (26/26); the library builds clean for net10.0, net10.0-android36.0 and net10.0-ios26.0.Upstreaming
After a BlueyeApp release confirms BLUEYE-APP-FH/9G stop occurring, this is a candidate to file upstream (closest upstream report is zeromq#697, stale-closed without a fix).
🤖 Generated with Claude Code