Skip to content

Document task-local, priority, and cancellation behavior - #93

Merged
dfed merged 1 commit into
claude/2.0-7-fifo-queue-turnsfrom
claude/2.0-8-document-behavior
Nov 17, 2026
Merged

dfed merged 1 commit into
claude/2.0-7-fifo-queue-turnsfrom
claude/2.0-8-document-behavior

Conversation

@dfed

@dfed dfed commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Doc comments: every Task(on:) doc comment now describes what the initializer does:
    • the operation's isolation and ordering;
    • that the task inherits the task-local values and priority of the code that enqueues it, and that awaiting it escalates it;
    • that cancelling a waiting task doesn't remove it from the queue: its operation still runs, after every task enqueued before it has finished (FIFOQueue) or has finished or suspended (ActorQueue).
  • Other public doc comments: rewords the doc comments of the three queue types and their initializers, adoptExecutionContext(of:), and cancelTasks() in the same plain terms as the README. Each comment uses one set of words throughout: "tasks enqueued before it", "finished", and "the code that enqueues it".
  • Floor: documents that a FIFOQueue's priority is a floor.
  • API: adds the priority: parameter that the two CancellableQueue<FIFOQueue> initializers without isolatedTo: were missing. Like every CancellableQueue initializer since Cancel tasks that are still being enqueued when cancelTasks() is called #90, they create their task through trackTask(performing:createdBy:).
  • README sample: fixes the CancellableQueue sample, which did not compile. It used try in a non-throwing task and called an async method without await. All five README @Test samples now compile, and all but actorTaskOrdering, which shows the problem the queues solve, pass.
  • Stale references: removes references to enqueue, enqueueAndWait, and the queue's own task, none of which exist anymore.
  • README sections: adds a "Task-local values, priority, and cancellation" section. What changed since 1.x and what to do about it will go in the 2.0.0 release notes instead of the README.
    • An operation enqueued without a priority now runs at the priority of the code that enqueues it rather than the queue's. FIFOQueue(priority: .medium) restores the old floor; ActorQueue callers should pass priority: when enqueuing important work from low-priority code.
    • The queues' name: initializers are removed.

README

  • Rewrites the README in the voice and vocabulary of the Swift guide's Concurrency chapter.
  • Opens with the problem in one sentence, then "Choosing a queue": one bullet per queue, and how Task(on:) compares with Task.init. It explains operation in one line where the word first appears.
  • Gives each queue its own section, with the existing example and an "In the code above" note.
  • Describes behavior in the guide's terms: tasks, actors, suspension points, task priority, task-local values, and cooperative cancellation.
  • Drops terms the library made up, such as nonisolated and isolated contexts, adopted execution contexts, and "FIFO order".
  • Fixes the claim that MainActor.queue runs tasks in FIFO order. It is an ActorQueue, so the next task can start once a task suspends.
  • States that cancellation is cooperative: a cancelled task keeps its place, and its operation still runs and decides how to respond.
  • Replaces the dead #ID645 and #ID639 guide links.
  • Keeps the Installation section's wording, matching the owner's other packages, with the version raised to 2.0.0. Package.swift already states the Swift and platform requirements.
  • Shows how to wait for an operation's result, with one inline line in the ActorQueue section.
  • Apart from the CancellableQueue sample fix above, keeps the examples' code and changes only comments.
  • Every Swift example (the five tests and the CancellableQueue(underlyingQueue: FIFOQueue()) snippet) compiles and runs with Swift 6.4.2 against this commit, with stand-ins for UIImage, performWork(), and the two placeholder function bodies. actorTaskOrdering still fails often, as it should. The inline await Task(on: queue) { myself in myself.count }.value line compiles and returns the count inside the ActorQueue example's Counter, from both an isolated and a nonisolated method.

Tests

task_fifoQueue_withPriority_executesAtThatPriority checks, for the throwing and non-throwing initializers, that the new priority: parameter sets the priority the task runs at. It enqueues a .low task from a .high task and reads the priority the task reports through a stream, without awaiting the task, so awaiting cannot escalate it.

Series

PR 9 of 10. It is stacked on #92.

Validation

Each commit in the series was validated on its own, on Linux with Swift 6.4.2, from a git archive copy:

  • It builds with -warnings-as-errors, and swiftformat --lint is clean.
  • The full suite passed 3 runs unpinned and 1 with the test runner pinned to one CPU.
  • The Linux job's coverage steps, run from the commit's own ci.yml under bash -e, pass, including the atomic-counter check. No line in Sources or Tests reads zero in the CI run.
  • This round changed no test here. Earlier rounds checked the race-guarding tests against mutants of the code they guard: each fails 10 of 10 runs against its mutant and passes on the real code.
  • At the top of the series, 3 rounds of 10 concurrent full suites pinned to one CPU pass, and ThreadSanitizer reports no races in 3 runs. TSan on Linux flags Synchronization.Mutex even in trivially correct code, so the TSan runs replace Mutex with 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:

  • Every queue initializer awaits turn.begin(). This PR rewrites the doc comments around every Task(on:) initializer; each must still await its turn before running its operation. Only main's ordering loops cover a missing await turn.begin().
  • Darwin. Nothing was run on Apple platforms. CI is the first run there.

🤖 Generated with Claude Code

https://claude.ai/code/session_01HLuVQLsTNvRnyh155N25UM

@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (a442493) to head (ab0abd7).

Additional details and impacted files

Impacted file tree graph

@@                       Coverage Diff                       @@
##           claude/2.0-7-fifo-queue-turns       #93   +/-   ##
===============================================================
  Coverage                         100.00%   100.00%           
===============================================================
  Files                                 12        12           
  Lines                               1865      1876   +11     
===============================================================
+ Hits                                1865      1876   +11     
Files with missing lines Coverage Δ
Sources/AsyncQueue/ActorQueue.swift 100.00% <ø> (ø)
Sources/AsyncQueue/CancellableQueue.swift 100.00% <100.00%> (ø)
Sources/AsyncQueue/FIFOQueue.swift 100.00% <ø> (ø)
Tests/AsyncQueueTests/CancellableQueueTests.swift 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dfed
dfed force-pushed the claude/2.0-8-document-behavior branch 2 times, most recently from 0740990 to 405ad63 Compare November 14, 2026 07:30
@dfed
dfed force-pushed the claude/2.0-8-document-behavior branch 2 times, most recently from fb7247d to 559a989 Compare November 16, 2026 05:13
@dfed
dfed force-pushed the claude/2.0-8-document-behavior branch from 559a989 to 02bd142 Compare November 16, 2026 11:40

dfed commented Nov 16, 2026

Copy link
Copy Markdown
Owner Author

Build Xcode (iOS,watchOS) failed on 02bd142 before any test ran. The build succeeded, and then xcodebuild couldn't reach the test runner because the simulator had shut down:

xctest encountered an error (Failed to establish communication with the test runner. (Underlying Error: Error retrieving daemon unix domain socket from simulator, failed after 30 attempts.; getenv error: … Code=405 "Unable to getenv(“TESTMANAGERD_SIM_SOCK”) while not booting or booted.  Current state: Shutdown"

The log has no test output at all, so the code on this branch had no part in it. It's a simulator failure on the xcode-27 runner, which is heavily shared right now because the whole stack was pushed at once. I'll re-run the failed job once the run finishes.


Generated by Claude Code

@dfed
dfed added this pull request to stack #103 November 16, 2026 21:42

dfed commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner Author

Correction: the CodeQL Swift failure I reported here is fixed by 9a40bac on #88, which builds without SwiftPM's sandbox under CodeQL, and this PR now sits on top of it. I was wrong that it needed a decision. See the corrected comment on #88.


Generated by Claude Code

- Rewrite the `Task(on:)` documentation to describe what each initializer
  actually does: the operation's isolation, its ordering, that it
  inherits the caller's task-local values and priority, that awaiting it
  escalates its priority, and that cancelling a pending task does not
  remove it from its queue.
- Document that a `FIFOQueue`'s priority is a floor.
- Add the `priority:` parameter that the two `CancellableQueue<FIFOQueue>`
  initializers without `isolatedTo:` were missing.
- Fix the README's `CancellableQueue` sample, which did not compile: it
  used `try` in a nonthrowing task and called an async method without
  `await`.
- Remove references to `enqueue` and `enqueueAndWait`, and to the queue's
  own task, none of which exist anymore.

Rewrite the README so that it is easy to follow, in the voice and
vocabulary of the Concurrency chapter of The Swift Programming Language.
It now opens with a short guide to choosing a queue, gives each queue
its own section around the existing examples, and describes behavior in
the guide's terms (tasks, actors, suspension points, task priority,
task-local values, and cooperative cancellation) instead of terms the
library made up, such as nonisolated and isolated contexts, adopted
execution contexts, and FIFO order. It corrects the claim that
`MainActor.queue` runs tasks in FIFO order: it is an `ActorQueue`, so
the next task can start once a task suspends. It also points the guide
links at sections that still exist. The installation section keeps its
wording, with the version raised to 2.0.0; what changed in 2.0 and what
to do belongs in the release notes. Apart from the
`CancellableQueue` fix, the examples' code is unchanged except for
comments. Describe the queues in the same plain terms in the public doc
comments.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HLuVQLsTNvRnyh155N25UM
@dfed
dfed force-pushed the claude/2.0-8-document-behavior branch from 02bd142 to ab0abd7 Compare November 16, 2026 23:40
@dfed
dfed merged commit d81a060 into main Nov 17, 2026
12 of 22 checks passed
@dfed
dfed deleted the claude/2.0-8-document-behavior branch November 17, 2026 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants