Repository navigation
[patch] Return from Cancel() before the cancelled run unwinds, so cancelling under a lock can't deadlock - #111
Merged
Conversation
…so Cancel() returns before the unwind The reader's cancellation TaskCompletionSource was completed from inside CancellationTokenSource.Cancel() without RunContinuationsAsynchronously, so the whole unwind ran on the cancelling thread before Cancel() returned: the read drain, the process-tree kill, and the caller's own catch block. A caller that cancelled while holding a lock its cleanup also takes deadlocked. Fixes #103 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Bm33TiYrofqKs4oGc3UGUU
|
This was referenced Oct 6, 2026
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 #103
Problem
AsyncProcessStreamReader.Startcreated its cancellation signal asnew TaskCompletionSource<bool>(). The token registration completes it from insideCancellationTokenSource.Cancel(), so every continuation ran synchronously on the cancelling thread: the read drain,RunAsync's catch with the process-tree kill, and then the caller's ownawait/catch. All of that happened beforeCancel()returned. A caller that cancelled while holding a lock its cleanup also takes deadlocked, andCancelAfterran user code on the timer thread.Change
TaskCreationOptions.RunContinuationsAsynchronously, with a comment explaining why.Tests
Two new tests in
RunCommandTests, following the issue's acceptance criteria:CancelShouldNotRunTheCallersContinuationInline: the caller'scatch (OperationCanceledException)records whether it ran whileCancel()was still on the stack.CancelWhileHoldingALockTheCallersCleanupNeedsShouldNotDeadlock: the cancelling thread holds aSemaphoreSlimthat the caller's catch takes synchronously. The wait in the catch is bounded at 5 s, so the bug shows up as a failure and doesn't hang the run.Checked locally (Linux, net10.0):
Cancel()was blocked behind the cleanup.🤖 Generated with Claude Code
https://claude.ai/code/session_01Bm33TiYrofqKs4oGc3UGUU
Generated by Claude Code