Repository navigation
Cancel queued tasks outside of Delivery's lock - #86
Merged
dfed merged 1 commit intoNov 17, 2026
Merged
Conversation
This was referenced Nov 14, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## claude/2.0-0-atomic-coverage #86 +/- ##
==============================================================
Coverage 100.00% 100.00%
==============================================================
Files 11 12 +1
Lines 1209 1298 +89
==============================================================
+ Hits 1209 1298 +89
🚀 New features to boost your workflow:
|
`Delivery.cancel()` called `Task.cancel()` while holding a lock backed by a single process-wide serial queue. Cancellation handlers run synchronously, so a handler that cancelled any other queued task – on any queue – re-entered `DispatchQueue.sync` and trapped. Forwarding cancellation to an inner task is a common pattern, so ordinary code hit this. Read the task under the lock and cancel it after releasing the lock, and give each `Delivery` its own lock so unrelated tasks no longer contend. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HLuVQLsTNvRnyh155N25UM
dfed
force-pushed
the
claude/2.0-1-delivery-cancel-outside-lock
branch
from
November 15, 2026 15:45
e69a296 to
5fbedc8
Compare
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.
Summary
Delivery.cancel()calledTask.cancel()while holding a lock backed by a single, process-wide serialDispatchQueue. Cancellation handlers run synchronously, so a handler that cancelled another queued task, on any queue, re-enteredDispatchQueue.syncand trapped with SIGILL. Forwarding cancellation to an inner task is a common pattern, so ordinary code hits this:This change reads the task under the lock and cancels it after releasing the lock. It also gives each
Deliveryits own lock.Tests
task_canBeCancelledFromCancellationHandlerOfAnotherQueuedTask(FIFOQueue and ActorQueue) crashes 10 of 10 times against the parent'sDeliveryand passes here. The inner task waits in a newsuspendUntilCancelled()helper (Utilities/TestHelpers.swift), which returns only once the task is cancelled and involves no sleep. If cancellation stops reaching the inner task, the test hangs until the CI job's timeout rather than failing an expectation.mainTask_cancelledWhileMainActorIsBusy_executesOperationCancelled(ActorQueue) andtask_cancelledWhileWaitingForPriorTask_executesOperationCancelled(FIFOQueue) cancel a task before its operation begins and check that the operation then runs cancelled. The ActorQueue test cancels while the test itself occupies the main actor, and the FIFOQueue test while an earlier task waits on a semaphore, so the cancellation always lands first, with no sleep.DeliveryTests.getValue_whenValueIsSentAfterGetValueSuspendsandgetValueThrowing_whenFailureIsSentAfterGetValueSuspendssend the value or failure only oncegetValue()has suspended, so the path that resumes a suspendedgetValue()runs in every run.Series
PR 2 of 10 in a 2.0 series (#95, then #86 through #94, each stacked on the previous one). It is stacked on #95, which adds atomic coverage counters, their check and job timeouts to CI and removes the unused
Expectationhelper, and which is based onmain. This PR changes no API, so it can ship in a 1.x patch release together with #87.Validation
Each commit in the series was validated on its own, on Linux, from a
git archivecopy. For this PR, with Swift 6.2.4 as theswift:6.2job uses:swiftformat --lintis clean. The test target has exactly the nineweak varwarnings it inherits frommain, which Raise minimum Swift, Xcode, and platform versions #88 fixes, and no others.linux-6-2job's coverage steps, run from this commit'sci.ymlunderbash -e, pass, including the atomic-counter check, which fails when the flag is dropped. Across the CI run and 4 more runs of the test binary (2 pinned), the only lines inSourcesorTeststhat read zero are the four spinner lines ofmain'sCancellableQueueTeststhat Update coverage counters atomically and time out test jobs #95 describes; Cancel tasks outside of CancellableQueue's lock #87 rewrites them.task_canBeCancelledFromCancellationHandlerOfAnotherQueuedTasktests crash 10 of 10 runs (3 pinned to one CPU) against the parent'sDelivery, pass 10 of 10 here, and pass--repetitions 100 --repeat-until failunpinned and pinned. Earlier rounds checked the other tests against mutants the same way.Synchronization.Mutexeven in trivially correct code, so the TSan runs replaceMutexwith a pthread-based lock that TSan understands.Nothing was run on Apple platforms. CI is the first run there.
Review checklist
Left for code review because no test can force them:
cancel()must setisCancelledand read the task in a singlewithLock, andexecutemust store the task and readisCancelledin a singlewithLock. Splitting either lets a cancellation slip between the two and be lost. No test can force a thread switch inside that window.task_canBeCancelledFromCancellationHandlerOfAnotherQueuedTaskhangs; CI reports that as the job stopping after 15 minutes (50 for the simulator jobs).🤖 Generated with Claude Code
https://claude.ai/code/session_01HLuVQLsTNvRnyh155N25UM