Repository navigation
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-1-delivery-cancel-outside-lock #87 +/- ##
============================================================================
Coverage 100.00% 100.00%
============================================================================
Files 12 12
Lines 1298 1510 +212
============================================================================
+ Hits 1298 1510 +212
🚀 New features to boost your workflow:
|
`cancelTasks()` invoked every registered cancel closure while holding the queue's lock. A cancellation handler that called `cancelTasks()` on the same queue re-entered `DispatchQueue.sync` and trapped. Collect the cancel closures under the lock and invoke them after releasing it. 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-2-cancel-tasks-outside-lock
branch
from
November 15, 2026 15:45
2425565 to
bc2f49d
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
CancellableQueue.cancelTasks()invoked every registered cancel closure while holding the queue's lock. A cancellation handler that calledcancelTasks()on the same queue re-enteredDispatchQueue.syncand trapped with SIGILL.This change collects the cancel closures under the lock and invokes them after releasing it. The doc comment now notes the cost of not cancelling under the lock: when another
cancelTasks()call is in progress, a call can return before that call finishes cancelling the tasks it found. Re-cancelling every task from every call would close that window, but under stress it slows down quadratically.Tests
cancelTasks_{fifoQueue,actorQueue}_canBeCalledFromCancellationHandlercrash 10 of 10 times on the parent and pass here. The task waits insuspendUntilCancelled()from Cancel queued tasks outside of Delivery's lock #86, and the test cancels it explicitly at the end, so a regression that keepscancelTasks()from reaching it fails the test rather than leaving the task running.cancelTasks_*_doesNotCancelFutureTaskstests are rewritten without timing: each waits for the task to end, then callscancelTasks()once more, which at this commit returns only once the queue has processed every earlier registration, and checks that the task was not cancelled. Replace DispatchQueue-backed locks with Mutex #89 drops that second call once registration is synchronous.Task(on:)initializers of each queue:…doesNotCancelCompletedNonthrowingTask(4),cancelTasks_fifoQueue_doesNotCancelCompletedThrowingTaskIsolatedToActor(a throwing initializer whose operation returns) and…ThatThrew(4): a task that has finished, by returning or throwing, is not cancelled by a latercancelTasks();…cancelsCurrentlyExecutingThrowingTask(4): a running throwing task is cancelled.cancelTasks_actorQueue_cancelsCurrentlyExecutingAndPendingTasksnow runs on aDispatchQueueActor, an actor with a dispatch-queue executor of its own, so it cannot occupy a cooperative thread that the test needs, which could hang on a single CPU. Both spinner tests spin onwhile !Task.isCancelled {}, cancel the spinner explicitly at the end and await every task, so every line runs in every passing run.DispatchQueueExecutorimplementsenqueue(_ job: UnownedJob)with an explicitasUnownedSerialExecutor(), becausemain's deployment targets predateExecutorJob. Raise minimum Swift, Xcode, and platform versions #88 switches it toExecutorJobwhen it raises the minimums.Series
PR 3 of 10. It is stacked on #86, but it is independent of it and could be retargeted to #95. It changes no API, so it can ship in a 1.x patch release together with #86.
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. No line inSourcesorTestsreads zero in the CI run or in 4 more runs of the test binary (2 pinned).cancelsCurrentlyExecutingThrowingTaskgap test fails 10 of 10 runs (3 pinned to one CPU) when its initializer skips registering the task's cancellation. 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:
cancelTasks()must collect the cancel closures in a single critical section, and the reservation, registration and removal of a task must each be a single critical section. No test can force a thread switch inside one.DispatchQueue.sync, so thecanBeCalledFromCancellationHandlertests would crash. From Replace DispatchQueue-backed locks with Mutex #89 on, the lock is aMutex, and re-entering it would deadlock instead: the tests would hang, and CI would report the job stopping after 15 minutes (50 for the simulator jobs).DispatchQueueExecutor'senqueue(_: UnownedJob)and a user-definedunownedExecutorat the macOS 10.15 deployment target were not built under Xcode. CI is the first build there.🤖 Generated with Claude Code
https://claude.ai/code/session_01HLuVQLsTNvRnyh155N25UM