Fix channel-open cancellation deadlock in ConnectionService#154
Open
juanpacostaaa wants to merge 3 commits into
Open
Fix channel-open cancellation deadlock in ConnectionService#154juanpacostaaa wants to merge 3 commits into
juanpacostaaa wants to merge 3 commits into
Conversation
…ion after releasing the lock. ConnectionService.HandleMessageAsync was disposing a pending channel's CancellationTokenRegistration while holding lockObject, but the callback registered in OpenChannelAsync also took lockObject, and Dispose() blocks until an in-flight callback completes — so a channel-open cancelling at that moment deadlocked the session and leaked every later OpenChannelAsync thread.
…ancellation deadlock, using a small internal ConnectionService hook reached by reflection (matching how ReconnectTests reaches KeyRotationThreshold) to force the cancel-during-confirmation race.
adamasmar
previously approved these changes
Jul 23, 2026
klvnraju
reviewed
Jul 23, 2026
juanpacostaaa
force-pushed
the
dev/juanpacostaaa/fix-channel-cancellation-deadlock
branch
from
July 23, 2026 22:30
a97004e to
00e876b
Compare
…ook with an explicit synchronization handshake using TaskCompletionSource and CancellationToken LIFO callback ordering. A second callback registered after the internal one fires first (LIFO), and once it completes, Cancel() immediately proceeds to the internal callback on the same thread — which blocks on lockObject.
klvnraju
approved these changes
Jul 23, 2026
Author
|
@microsoft-github-policy-service agree company="Microsoft" |
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.
Problem
ConnectionService.HandleMessageAsyncdisposed a pending channel'sCancellationTokenRegistrationwhile holding
lockObject. The callback registered inOpenChannelAsyncalso takeslockObject,and
Dispose()blocks until an in-flight callback completes. So an open cancelling exactly as itsconfirmation was processed deadlocked the pump thread, hanging every later
OpenChannelAsyncFix
Dispose the registration after releasing the lock. Removing the pending channel under the lock
makes the callback a no-op, so deferring the dispose is safe. Applied to the confirmation and
failure handlers.
Tests
Added
OpenChannelCancelDuringConfirmationDoesNotDeadlockandSessionRemainsUsableAfterOpenCancelRace,which force the race via a small internal hook reached by reflection (like
ReconnectTestsdoes forKeyRotationThreshold).