Files
Charles Wiltgen 6586852dc9 feat(axiom-concurrency): document cancellation-shield masking semantics
Adds the synchronous withTaskCancellationShield overload and
hasActiveCancellationShield (static on Task, instance on UnsafeCurrentTask).

A runtime probe on macOS 27 established what the interface does not state:
the shield MASKS cancellation rather than suppressing throws. Inside a
shield, in a cancelled task, Task.isCancelled reads false,
checkCancellation() does not throw, and Task.sleep completes.

The trap follows directly and is now the headline: a
"while !Task.isCancelled" loop inside a shield never exits — the condition
it polls is the one the shield suppresses. An onCancel registered inside a
shield likewise does not fire for an already-pending cancellation.

Also measured: nesting is safe (inner pop leaves the outer installed);
masking is scoped to the region, so a child outliving the shield sees
isCancelled == true — shielding a spawn does not permanently detach it;
and hasActiveCancellationShield is per-task, not per-region, so a
structured child reports false while still observing isCancelled == false.

Closes Axiom-csr.
2026-08-25 05:36:57 -07:00

3.0 KiB
Raw Permalink Blame History

Swift Concurrency Reference

Complete API reference for Swift's concurrency model — actors, Sendable, Task management, async sequences, continuations, and migration patterns from GCD.

When to Use This Reference

Use this reference when:

  • Looking up actor definition syntax or reentrancy rules
  • Implementing Sendable conformance for your types
  • Creating TaskGroups for parallel work
  • Working with AsyncStream or custom AsyncSequence
  • Bridging callback-based APIs with continuations
  • Migrating from DispatchQueue/DispatchGroup to actors/TaskGroup

Example Prompts

  • "How do I create a TaskGroup in Swift?"
  • "What's the AsyncStream continuation API?"
  • "How do I make a custom global actor?"
  • "How do I convert a completion handler to async/await?"
  • "What are the actor reentrancy rules?"
  • "How do I make my class Sendable?"
  • "What's the difference between Task and Task.detached?"
  • "How do I run a block that ignores task cancellation?" (iOS 27)
  • "Why does my loop never exit inside withTaskCancellationShield?" (iOS 27)

What's Covered

  • Actor patterns (definition, nonisolated, reentrancy, global actors, gotcha table)
  • Sendable patterns (struct/enum, @Sendable closures, @unchecked, conditional, gotcha table)
  • Task management (Task, Task.detached, cancellation, withTaskCancellationShield and hasActiveCancellationShield (iOS 27), priority, @TaskLocal, gotcha table)
  • Structured concurrency (async let, TaskGroup, task tree semantics, gotcha table)
  • Async sequences (AsyncStream, continuation API, buffering, custom AsyncSequence, gotcha table)
  • Isolation patterns (@MainActor, nonisolated, #isolation, sending, gotcha table)
  • Continuations (withCheckedContinuation, resume-exactly-once, bridging, gotcha table)
  • Migration patterns (DispatchQueue to actor, DispatchGroup to TaskGroup, callbacks to async, gotcha table)
  • Typed notifications (MainActorMessage / AsyncMessage, iOS 26+ — replacing userInfo casting, observation-token lifetime, per-framework coverage)
  • API quick reference table with Swift version requirements

Documentation Scope

This page documents the axiom-concurrency (swift-concurrency-ref reference) — the API reference Claude uses for Swift concurrency syntax and patterns.

For the progressive journey and decision trees: See Swift Concurrency for when and why to introduce concurrency, plus 11 copy-paste patterns.

  • Swift Concurrency Progressive journey from single-threaded to concurrent code, decision trees, @concurrent and isolated conformances
  • Synchronization Mutex, OSAllocatedUnfairLock, atomic types (when you need locks instead of actors)
  • assumeIsolated Synchronous actor access patterns
  • Concurrency Profiling Instruments workflows for async performance

Resources

WWDC: 2021-10132, 2021-10134, 2022-110350, 2025-268

Skills: axiom-concurrency