Repository navigation
Cancel tasks that are still being enqueued when cancelTasks() is called - #90
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## claude/2.0-4-mutex #90 +/- ##
====================================================
Coverage 100.00% 100.00%
====================================================
Files 12 12
Lines 1476 1540 +64
====================================================
+ Hits 1476 1540 +64
🚀 New features to boost your workflow:
|
dfed
force-pushed
the
claude/2.0-5-cancel-enqueuing-tasks
branch
from
November 15, 2026 15:45
9fb84c6 to
83b0b2d
Compare
Owner
Author
|
Correction: the CodeQL Swift failure I reported here is fixed by Generated by Claude Code |
A `CancellableQueue` reserves an identifier for each task, creates the task on the underlying queue, and only then registers the task's cancel closure. `cancelTasks()` cancelled only registered tasks. A task that was reserved but not yet registered when `cancelTasks()` ran was skipped, and nothing cancelled it later unless `cancelTasks()` was called again. Such a task can already be executing, for example when its own operation calls `cancelTasks()`. `cancelTasks()` now marks such reservations, and registering a marked task's cancel closure cancels the task immediately, after releasing the lock. Every `Task(on:)` initializer of `CancellableQueue` now tracks its task through one internal method, `trackTask(performing:createdBy:)`, which reserves the identifier, creates the task, and registers its cancel closure, in that order. Tests call it directly to finish a task, or to call `cancelTasks()`, while the task is being created, which the initializers cannot be made to do on demand. The method is always inlined, so enqueuing a task costs what it did before. 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-5-cancel-enqueuing-tasks
branch
from
November 16, 2026 23:40
83b0b2d to
a47fa99
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
A
CancellableQueuereserves an identifier for each task, creates the task on the underlying queue, and only then registers the task's cancel closure.cancelTasks()cancelled only registered tasks. A task that was reserved but not yet registered whencancelTasks()ran was skipped, and nothing cancelled it later unlesscancelTasks()was called again. Such a task can already be executing, for example when its own operation callscancelTasks().cancelTasks()now marks those reservations. When a marked task registers its cancel closure, it is cancelled immediately, after the lock is released. This bug predates the series; it gets its own PR because it is a separate bug.Every
Task(on:)initializer ofCancellableQueuenow tracks its task through one internal method,trackTask(performing:createdBy:), which reserves the identifier, creates the task, and registers its cancel closure, in that order. Tests call it directly to finish a task, or to callcancelTasks(), while the task is being created, which the initializers cannot be made to do on demand. It is marked@inline(__always): without the attribute, enqueuing on aCancellableQueuemeasured about 11% slower (30 interleaved rounds of 20,000 tasks pinned to one CPU, median 1.113× against the original); with it, the cost is within noise (1.007×, against 0.995× for an A/A run), and heap allocations per task are unchanged.Tests
All of these drive the queue's own methods in a fixed order, so none depends on timing:
cancelTasks_cancelsTaskThatRegistersCancellationAfterwardand…AfterRepeatedCancelTaskscallcancelTasks()between a task's reservation and its registration. They fail 10 of 10 times on the parent.cancelTasks_doesNotCancelTaskThatFinishedBeforeRegisteringCancellationAfterCancelTasksguards the new state against cancelling a task that has already finished.cancelTasks_canBeCalledFromCancellationHandlerOfTaskThatRegistersCancellationAfterwardchecks that the cancellation a registration triggers runs after the lock is released, so its handler can callcancelTasks().cancelTasks_doesNotCancelTrackedTaskThatFinishedWhileBeingCreatedandcancelTasks_calledWhileTrackedTaskIsBeingCreated_cancelsTaskcalltrackTaskwith acreateTaskclosure that finishes the task, or callscancelTasks(), beforetrackTaskregisters it. Both fail 10 of 10 times iftrackTaskreserves only after creating the task, and the second if it never registers the task.Series
PR 6 of 10. It is stacked on #89.
Validation
Each commit in the series was validated on its own, on Linux with Swift 6.4.2, from a
git archivecopy:-warnings-as-errors, andswiftformat --lintis clean.ci.ymlunderbash -e, pass, including the atomic-counter check. No line inSourcesorTestsreads zero in the CI run.trackTasktests fail 10 of 10 runs (3 pinned to one CPU) against atrackTaskthat reserves the identifier only after creating the task, and the second also against one that never registers the task. Both pass 10 of 10 on the real code and pass--repetitions 100 --repeat-until failunpinned and pinned, and still fail 10 of 10 against the first mutant at every later commit in the series.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 registered closures and mark the reservations in onewithLock, andregisterCancellationmust read and update the task's state in one. No test can force a thread switch inside one.@inline(__always)cost was measured on Linux only.🤖 Generated with Claude Code
https://claude.ai/code/session_01HLuVQLsTNvRnyh155N25UM