Skip to content

Survive continuations posted after NetMQPoller disposal#11

Merged
follesoe merged 2 commits into
devfrom
fix/poller-disposed-synccontext
Jun 10, 2026
Merged

Survive continuations posted after NetMQPoller disposal#11
follesoe merged 2 commits into
devfrom
fix/poller-disposed-synccontext

Conversation

@follesoe

Copy link
Copy Markdown
Member

Fixes #9

Problem

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 — 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 between Stop() and Dispose() were discarded with the task queue, leaving their awaiters hanging forever.

Changes

  • NetMQSynchronizationContext.Post: falls back to ThreadPool.QueueUserWorkItem when the poller is disposed, including when disposal races the Task.Start call (the faulted task's exception is observed so it cannot surface as UnobservedTaskException).
  • NetMQSynchronizationContext.Send: executes the callback inline on the calling thread in the same situations.
  • NetMQPoller.Dispose: captures tasks still in m_tasksQueue (by enumerating the backing queue — TryDequeue depends 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 of Dispose().

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 NetMQPollerTest covering 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

follesoe and others added 2 commits June 10, 2026 12:15
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>
@follesoe follesoe requested a review from Copilot June 10, 2026 10:32
@follesoe follesoe merged commit 165d5d3 into dev Jun 10, 2026
3 checks passed
@follesoe follesoe deleted the fix/poller-disposed-synccontext branch June 10, 2026 10:36

Copilot AI 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.

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.Post falls back to ThreadPool when the poller is disposed (including dispose races during Task.Start), avoiding uncatchable exceptions in the await continuation path.
  • NetMQSynchronizationContext.Send falls back to inline execution on the calling thread in the same disposed/race scenarios.
  • NetMQPoller.Dispose snapshots 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.

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.

2 participants