swift-concurrency-doctor
Skill0 starsChanged 57 days agoMITView on GitHub
Operational manual and clinical diagnostic guide for Swift 6 strict concurrency. Use when: resolving Swift 6 concurrency compiler errors or warnings (-strict-concurrency=complete), diagnosing data races, fixing Sendable conformance violations across actor boundaries, addressing @MainActor isolation hops, debugging actor reentrancy state mutations, bridging legacy delegate patterns or callbacks to AsyncStream, or replacing legacy DispatchQueue.main.async with structured tasks.
In this repository11 other files it gives its agents.
- templates/AGENTS.mdAGENTS.md
- appstore-privacy-auditorSkill
- ci-cd-automationSkill
- codebase-bootstrapSkill
- code-hygiene-auditorSkill
- git-atomic-curatorSkill
- iphone-duo-migratorSkill
- swiftui-layout-debuggerSkill
- token-optimizerSkill
- xcode-ci-doctorSkill
- zero-secret-sanitizerSkill
The file
We split this skill into the rules it gives an agent
7 rules. About 0 are common, 1 is distinctive, and 6 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
6
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 24
Invariant 1 (Explicit Isolation): Every mutable variable and reference type must have a single, unambiguous isolation owner: an actor …
- Very specific: line 26
Invariant 3 (Structured Over Unstructured): Prefer scoped, cooperative concurrency (async let, withTaskGroup, withThrowingTaskGroup) over …
- Very specific: line 65
When the Swift 6 compiler emits an error or warning with -strict-concurrency=complete, execute this sequence:
- Very specific: line 268
If the property is immutable and set only once at creation, mark it nonisolated let:
- Very specific: line 367
Always default to Task { } unless you explicitly intend to create a background task that must continue running even if the user navigates …
- Very specific: line 379
When bridging low-level OS APIs that cannot use actor (e.g.
- Distinctive: line 558
When downloading 5,000 files, spawning 5,000 tasks concurrently exhausts memory and file descriptors.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Swift Concurrency Doctor (Swift 6 & Strict Concurrency Manual) 🩺⚡️
The definitive, production-grade guide for AI coding agents tasked with diagnosing, refactoring, and verifying Swift 6 strict concurrency, data-race safety, actor isolation domains, and modern structured concurrency architectures.
1. Executive Summary & Core Philosophy
Swift 6 represents the transition of data-race safety from a dynamic runtime debugging concern to a static compile-time guarantee. For AI coding assistants, this transition introduces specific recurring pitfalls:
- The Traps Naive Agents Fall Into:
- Indiscriminate
@unchecked Sendable: Plastering@unchecked Sendableonto mutable classes to silence compiler warnings, which destroys thread safety guarantees and introduces silent heap corruption at runtime. - Inappropriate Blanket
@MainActor: Marking entire network, storage, or parsing layers with@MainActorto avoid isolation hops, resulting in main-thread stalls and UI stutter. - Mixing Grand Central Dispatch inside Async Contexts: Calling
DispatchQueue.main.asyncorDispatchQueue.global().asyncinsideasyncmethods, causing priority inversion, uncooperative thread creation, and escaping closure errors. - Ignoring Suspension Point Reentrancy: Assuming that state checked before an
awaitcall remains identical after resumption.
- Indiscriminate
- The Doctor's Non-Negotiable Invariants:
- Invariant 1 (Explicit Isolation): Every mutable variable and reference type must have a single, unambiguous isolation owner: an actor instance, a global actor (like
@MainActor), or confinement within an isolated synchronous scope. - Invariant 2 (Verifiable Sendability): Values crossing concurrency boundaries must conform to
Sendable. Value types (struct,enum, primitives) are preferred. Classes must be final and immutable or employ synchronization primitives with explicit justification. - Invariant 3 (Structured Over Unstructured): Prefer scoped, cooperative concurrency (
async let,withTaskGroup,withThrowingTaskGroup) over floating, untrackedTask { }instances. - Invariant 4 (Never Block the Cooperative Pool): The Swift runtime allocates exactly one worker thread per physical CPU core. Never invoke blocking system calls (
sleep,sem_wait,pthread_mutex_lockwith long holds, or synchronous network I/O) on cooperative threads.
- Invariant 1 (Explicit Isolation): Every mutable variable and reference type must have a single, unambiguous isolation owner: an actor instance, a global actor (like
2. Formal Concurrency Mental Model for AI Agents
To analyze any concurrency diagnostic accurately, evaluate the problem across the following four orthogonal axes:
+-------------------------------------------------------------------------------------------------------+
| SWIFT 6 CONCURRENCY TAXONOMY MATRIX |
+-----------------------------+-------------------------------------------------------------------------+
| Axis 1: Isolation Domain | Where does this code execute? |
| | -> Nonisolated: Unbound, runs on generic cooperative pool. |
| | -> Actor Instance: Bound to an actor's serial mailbox executor. |
| | -> Global Actor: Bound to a shared singleton executor (@MainActor). |
+-----------------------------+-------------------------------------------------------------------------+
| Axis 2: Boundary Crossing | How does data move between domains? |
| | -> Pass-by-value (Structs/Enums): Deep copied across threads. |
| | -> Actors: Passed by reference, access synchronized via await. |
| | -> Sendable Closures (@Sendable): Only capture Sendable values. |
| | -> Non-Sendable Types: Restricted to origin isolation domain. |
+-----------------------------+-------------------------------------------------------------------------+
| Axis 3: Execution Topology | What is the task lifecycle? |
| | -> Structured: Child task strictly bounded by parent lifetime. |
| | -> Unstructured: Task { } inherits priority/context, outlives parent. |
| | -> Detached: Task.detached { } inherits zero context or priority. |
+-----------------------------+-------------------------------------------------------------------------+
| Axis 4: Temporal Invariants | How does state evolve across suspension? |
| | -> Synchronous block: Atomic, uninterrupted execution. |
| | -> Suspension (await): Thread yielded; actor state can mutate. |
+-----------------------------+-------------------------------------------------------------------------+3. Systematic 5-Phase Diagnostic Protocol
When the Swift 6 compiler emits an error or warning with -strict-concurrency=complete, execute this sequence:
[Phase 1: Diagnostic Parsing]
│
▼ Extract: Source file, line, symbol, origin domain, destination domain.
│
[Phase 2: Entity Classification]
│
▼ Determine: Is the subject a Value Type, Reference Type, Closure, or Protocol?
│
[Phase 3: Domain Alignment]
│
▼ Evaluate: Should the caller hop to the callee's domain, or should callee be nonisolated?
│
[Phase 4: Architectural Remediation]
│
▼ Apply: Immutable Value conversion, Actor encapsulation, or AsyncStream bridging.
│
[Phase 5: Local & CI Verification]
│
▼ Verify: Compile with `swift build -Xswiftc -strict-concurrency=complete -Xswiftc -warnings-as-errors`.4. Comprehensive Error Catalog & Surgical Fixes
Error 01: Mutation of Captured Var in Concurrently-Executing Code
- Compiler Diagnostic:
error: mutation of captured var 'total' in concurrently-executing code - Root Cause:
A local
var total = 0is modified from within an asynchronous closure or task group. Swift prohibits shared mutable state across concurrent closures to prevent data races. - Incorrect Attempt:
// WRONG: Wrapping in an unchecked reference box without locks final class UnsafeBox: @unchecked Sendable { var value = 0 } let box = UnsafeBox() await withTaskGroup(of: Void.self) { group in for _ in 0..<10 { group.addTask { box.value += 1 } } } - Doctor's Solution (Structured Reductive Concurrency):
// CORRECT: Child tasks return values; parent task aggregates results let total = await withTaskGroup(of: Int.self, returning: Int.self) { group in for _ in 0..<10 { group.addTask { // Perform isolated calculation return 1 } } var sum = 0 for await value in group { sum += value } return sum }
Error 02: Capture of Non-Sendable Type in @Sendable Closure
- Compiler Diagnostic:
error: capture of 'client' with non-sendable type 'HTTPClient' in a@Sendableclosure - Root Cause:
A closure passed to an asynchronous API or
Task.initimplicitly requires@Sendable. If it captures a reference type withoutSendableconformance, a potential race condition exists. - Doctor's Solution:
- Extract immutable data before spawning the task:
// If only configuration or endpoints are needed: let endpointURL = client.configuration.baseURL Task { [endpointURL] in let request = URLRequest(url: endpointURL) // Execute request with thread-safe parameters }- Or convert
HTTPClientinto anactor:
public actor HTTPClient { private let session: URLSession public init(session: URLSession = .shared) { self.session = session } public func get(url: URL) async throws -> Data { let (data, _) = try await session.data(from: url) return data } }
Error 03: Main Actor-Isolated Property Mutated from Background Context
- Compiler Diagnostic:
error: main actor-isolated property 'viewState' cannot be mutated from a non-isolated context - Root Cause:
A background parsing or networking task attempts to update a UI-bound
@Publishedor@Observableproperty directly. - Doctor's Solution:
// CORRECT: Hop to MainActor context await MainActor.run { self.viewState = .loaded(items) } // PREFERRED: Isolate presentation methods directly @MainActor func updateViewState(_ state: ViewState) { self.viewState = state }
Error 04: Actor Reentrancy State Corruption (The Silent Bug)
- Problem Statement:
Actors guarantee mutual exclusion, but not transactional continuity across
awaitsuspension points. When an actor callsawait, another task can enter the actor and mutate its state before the first task resumes. - Vulnerable Code:
actor BankAccount { private var balance: Decimal = 100 func withdraw(amount: Decimal) async throws { guard balance >= amount else { throw BankError.insufficientFunds } // Suspension point! Another task calls withdraw() right here! let approved = await fraudDetectionService.verify(amount: amount) guard approved else { throw BankError.fraudDetected } // DANGER: balance might have been depleted during suspension! balance -= amount } } - Doctor's Solution (Re-verification after Suspension):
actor BankAccount { private var balance: Decimal = 100 func withdraw(amount: Decimal) async throws { // Pre-check guard balance >= amount else { throw BankError.insufficientFunds } let approved = await fraudDetectionService.verify(amount: amount) guard approved else { throw BankError.fraudDetected } // Re-validate invariant immediately after resumption guard balance >= amount else { throw BankError.insufficientFunds } balance -= amount } }
Error 05: Bridging Legacy Delegates to Modern AsyncStream
- Problem Statement:
Older Apple SDKs (such as
CoreLocation,AVFoundation, orExternalAccessory) communicate via delegate protocols. Naive agents bridge them with global mutable variables or semaphores. - Doctor's Solution (AsyncStream Factory Pattern):
import CoreLocation import Foundation public final class LocationStreamProvider: NSObject, CLLocationManagerDelegate, @unchecked Sendable { private let locationManager = CLLocationManager() private var continuation: AsyncStream<CLLocation>.Continuation? public override init() { super.init() locationManager.delegate = self } public func streamLocations() -> AsyncStream<CLLocation> { AsyncStream { continuation in self.continuation = continuation self.locationManager.startUpdatingLocation() continuation.onTermination = { [weak self] _ in self?.locationManager.stopUpdatingLocation() } } } public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { for location in locations { continuation?.yield(location) } } public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { // Finish stream gracefully on error or cancellation continuation?.finish() } }
Error 06: Actor-Isolated Property Referenced Synchronously from Outside
- Compiler Diagnostic:
error: actor-isolated property 'cachedItem' can not be referenced from a non-isolated context - Root Cause:
Directly accessing an actor property (e.g.
let item = myActor.cachedItem) without prefixing withawait. - Doctor's Solution:
- Access the property asynchronously:
let item = await myActor.cachedItem. - If the property is immutable and set only once at creation, mark it
nonisolated let:
public actor ImageCache { public nonisolated let storageDirectory: URL // Safe for synchronous reads private var memoryCache: [String: Data] = [:] // Isolated, requires await public init(storageDirectory: URL) { self.storageDirectory = storageDirectory } public func fetch(key: String) -> Data? { return memoryCache[key] } } - Access the property asynchronously:
Error 07: Satisfying Nonisolated Protocol Requirements in an Actor
- Compiler Diagnostic:
error: actor-isolated instance method 'description' cannot be used to satisfy nonisolated protocol requirement - Root Cause:
An actor conforms to a synchronous protocol (like
CustomStringConvertible,Identifiable, orHashable). Because protocol methods are synchronous, they cannot invoke the actor's asynchronous mailbox. - Doctor's Solution:
Mark the protocol implementation method
nonisolated. Inside that method, you can only accessnonisolatedproperties:public actor DeviceManager: CustomStringConvertible, Identifiable { public nonisolated let id: UUID public nonisolated let deviceName: String private var connectionCount: Int = 0 public init(id: UUID = UUID(), deviceName: String) { self.id = id self.deviceName = deviceName } public nonisolated var description: String { return "DeviceManager(name: \(deviceName), id: \(id))" } }
Error 08: Global Variables in Swift 6
- Compiler Diagnostic:
error: global variable 'sharedDatabase' cannot be mutated from concurrently-executing code; it must be isolated to a global actor or marked 'nonisolated(unsafe)' - Root Cause: Top-level variables or static mutable properties without actor isolation represent unsynchronized shared memory.
- Doctor's Solution:
// OPTION A: Make immutable if constant public enum Configuration { public static let apiBaseURL = URL(string: "https://api.example.com")! } // OPTION B: Isolate to MainActor if UI-bound @MainActor public final class AppState { public static var activeTheme: String = "dark" } // OPTION C: Encapsulate in an Actor if shared mutable service public actor SharedDatabaseManager { public static let shared = SharedDatabaseManager() private var connectionPool: [String: Any] = [:] }
Error 09: Continuation Resumed Twice or Never Resumed
- Runtime Failure:
SWIFT TASK CONTINUATION MISUSE: tried to resume continuation twice! - Root Cause:
In callback-based APIs, a code path triggers both success and failure blocks, or a branch exits without calling
continuation.resume. - Doctor's Solution (Single-Exit Guard Pattern):
func fetchLegacyData(from url: URL) async throws -> Data { try await withCheckedThrowingContinuation { continuation in LegacyDownloader.download(url) { data, error in if let error = error { continuation.resume(throwing: error) } else if let data = data { continuation.resume(returning: data) } else { continuation.resume(throwing: URLError(.badServerResponse)) } } } }
Error 10: Task.detached Ignoring Cancellation & Priority Inversion
- Problem Statement:
Task.detachedbreaks the structural link to the parent task. It does not inherit task-local storage, cancellation tokens, or QoS priority. - Doctor's Solution:
Always default to
Task { }unless you explicitly intend to create a background task that must continue running even if the user navigates away or cancels the view:// GOOD: Structured child task inheriting cancellation async let processedImage = imageProcessor.render(image) async let metadata = metadataExtractor.extract(image) let (img, meta) = try await (processedImage, metadata)
5. Production Reference Architecture: ThreadSafeBox Primitive
When bridging low-level OS APIs that cannot use actor (e.g. real-time audio threads or high-frequency telemetry), use this verified os_unfair_lock implementation:
import os.lock
import Foundation
/// A high-performance, thread-safe value container utilizing os_unfair_lock.
/// Conforms to @unchecked Sendable with formal mutual exclusion guarantees.
public final class ThreadSafeBox<Value>: @unchecked Sendable {
private var unfairLock = os_unfair_lock()
private var internalValue: Value
public init(_ initialValue: Value) {
self.internalValue = initialValue
}
/// Reads the protected value with lock protection.
public func read() -> Value {
os_unfair_lock_lock(&unfairLock)
defer { os_unfair_lock_unlock(&unfairLock) }
return internalValue
}
/// Mutates the protected value with lock protection.
public func write(_ newValue: Value) {
os_unfair_lock_lock(&unfairLock)
defer { os_unfair_lock_unlock(&unfairLock) }
internalValue = newValue
}
/// Executes an in-place transformation within a single critical section.
public func mutate<R>(_ block: (inout Value) -> R) -> R {
os_unfair_lock_lock(&unfairLock)
defer { os_unfair_lock_unlock(&unfairLock) }
return block(&internalValue)
}
}6. Complete Anti-Patterns Compendium (15 Fatal Mistakes)
| # | Anti-Pattern | Fatal Consequence | Doctor's Verified Fix |
| 1 | Sprinkling @unchecked Sendable on mutable classes | Silences compiler; causes random heap corruption and crashes in production. | Convert to an actor, an immutable struct, or use ThreadSafeBox. |
| 2 | DispatchQueue.main.async inside async methods | Causes uncooperative thread context switching, race hazards, and priority inversion. | Use await MainActor.run { ... } or isolate the function with @MainActor. |
| 3 | Neglecting Actor Reentrancy | Variables validated before an await can mutate before the task resumes. | Re-validate state invariants immediately after every await statement. |
| 4 | Overusing Task.detached | Detached tasks ignore parent cancellation; continue burning CPU and battery in the background. | Prefer async let, TaskGroup, or scoped Task { }. |
| 5 | Passing NSManagedObject across actors | CoreData crashes violently with thread-confinement violations. | Pass NSManagedObjectID across boundaries or map to an immutable Sendable struct. |
| 6 | Resuming a Continuation more than once | Process immediately crashes with SWIFT TASK CONTINUATION MISUSE. | Ensure every execution path hits exactly one resume call using defer or early guard returns. |
| 7 | Ignoring Task.isCancelled in loops | Long-running tasks consume device memory and network bandwidth even after cancellation. | Periodically call try Task.checkCancellation() inside loops. |
| 8 | Placing @MainActor on parsing or heavy computation | Freezes the user interface by executing JSON decoding or hashing on the main thread. | Confine computational engines to background actors or nonisolated async functions. |
| 9 | Retaining self strongly in infinite async loops | ViewModels never deallocate; causes severe memory leaks and background battery drain. | Use structured lifecycle hooks like SwiftUI's .task modifier which auto-cancels on dismiss. |
| 10 | Calling Thread.sleep or sleep() on cooperative threads | Starves the cooperative thread pool (1 thread per core); locks up the entire app. | Always use try await Task.sleep(for:). |
| 11 | Abusing nonisolated(unsafe) | Completely disables compile-time safety checks with zero synchronization. | Use OS-level locks, actors, or thread-safe atomic wrappers. |
| 12 | Expecting synchronous reads of actor properties | Non-isolated callers must asynchronously await even simple reads. | Mark immutable constants nonisolated let. |
| 13 | Passing UIKit/AppKit views across actors | UI controls are strictly confined to the main thread. Access from other threads causes aborts. | Pass plain data or view models, never instances of UIView or NSView. |
| 14 | Unbounded concurrent child task spawning | Spawning 100,000 tasks concurrently exhausts socket pools and device RAM. | Implement a sliding-window batching mechanism inside TaskGroup. |
| 15 | Bridging async to sync with DispatchSemaphore.wait() | Deadlocks the cooperative thread pool immediately. | Maintain the async pipeline all the way to the UI layer or app entry point. |
7. Exhaustive Case Studies Library
Case Study 01: High-Throughput In-Memory Caching Engine
public actor ImageMemoryCache {
private struct CacheEntry: Sendable {
let data: Data
let accessTime: Date
let cost: Int
}
private var entries: [String: CacheEntry] = [:]
private var totalCost: Int = 0
public nonisolated let maxCost: Int
public init(maxCost: Int = 50 * 1024 * 1024) { // 50MB
self.maxCost = maxCost
}
public func store(data: Data, for key: String) {
let cost = data.count
guard cost <= maxCost else { return }
while totalCost + cost > maxCost, !entries.isEmpty {
evictOldest()
}
entries[key] = CacheEntry(data: data, accessTime: Date(), cost: cost)
totalCost += cost
}
public func retrieve(for key: String) -> Data? {
guard let entry = entries[key] else { return nil }
entries[key] = CacheEntry(data: entry.data, accessTime: Date(), cost: entry.cost)
return entry.data
}
private func evictOldest() {
guard let oldestKey = entries.min(by: { $0.value.accessTime < $1.value.accessTime })?.key else { return }
if let removed = entries.removeValue(forKey: oldestKey) {
totalCost -= removed.cost
}
}
}8. Verification & Test Suite Integration
To verify async code deterministically without flaky sleep delays:
import XCTest
@testable import MyApp
final class ConcurrencyTests: XCTestCase {
func testAsyncExecutionDeterminism() async throws {
let cache = ImageMemoryCache(maxCost: 1024)
let sampleData = Data([0x01, 0x02, 0x03])
await cache.store(data: sampleData, for: "key1")
let retrieved = await cache.retrieve(for: "key1")
XCTAssertEqual(retrieved, sampleData)
}
}Case Study 02: Resilient Network Engine with Cancellation & Timeout Guards
import Foundation
public actor NetworkEngine {
private let session: URLSession
public init(session: URLSession = .shared) {
self.session = session
}
public func fetchWithTimeout(url: URL, timeoutSeconds: Double = 15.0) async throws -> Data {
try await withThrowingTaskGroup(of: Data.self) { group in
// Child Task 1: Execute actual network request
group.addTask {
try Task.checkCancellation()
let (data, response) = try await self.session.data(from: url)
guard let httpResponse = response as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) else {
throw URLError(.badServerResponse)
}
return data
}
// Child Task 2: Timeout timer
group.addTask {
try await Task.sleep(for: .seconds(timeoutSeconds))
throw URLError(.timedOut)
}
// Wait for whichever finishes first
guard let firstResult = try await group.next() else {
throw URLError(.unknown)
}
// Immediately cancel the remaining task (e.g. timeout timer or network call)
group.cancelAll()
return firstResult
}
}
}Case Study 03: Sliding-Window TaskGroup Rate Limiter
When downloading 5,000 files, spawning 5,000 tasks concurrently exhausts memory and file descriptors. This sliding-window rate limiter guarantees a maximum concurrency cap:
import Foundation
public struct BatchDownloader: Sendable {
public static func downloadAll(urls: [URL], maxConcurrent: Int = 6) async throws -> [URL: Data] {
return try await withThrowingTaskGroup(of: (URL, Data).self) { group in
var results: [URL: Data] = [:]
var urlIterator = urls.makeIterator()
// Prime the pool with maxConcurrent initial tasks
for _ in 0..<maxConcurrent {
if let url = urlIterator.next() {
group.addTask {
let (data, _) = try await URLSession.shared.data(from: url)
return (url, data)
}
}
}
// As each task finishes, spawn the next one from the iterator
for try await (url, data) in group {
results[url] = data
if let nextURL = urlIterator.next() {
group.addTask {
let (nextData, _) = try await URLSession.shared.data(from: nextURL)
return (nextURL, nextData)
}
}
}
return results
}
}
}Case Study 04: SwiftData ModelContext Isolation Across Background Actors
ModelContainer is Sendable, but ModelContext is NOT thread-safe across isolation boundaries. Here is the canonical Swift 6 pattern:
import SwiftData
import Foundation
@Model
public final class CachedItemRecord {
public var id: String
public var payload: Data
public var createdAt: Date
public init(id: String, payload: Data, createdAt: Date = Date()) {
self.id = id
self.payload = payload
self.createdAt = createdAt
}
}
public actor BackgroundDataSynchronizer {
private let container: ModelContainer
public init(container: ModelContainer) {
self.container = container
}
public func performBackgroundSync(items: [(id: String, payload: Data)]) async throws {
// Instantiate dedicated isolated context bound to this actor's execution domain
let isolatedContext = ModelContext(container)
isolatedContext.autosaveEnabled = false
for item in items {
let record = CachedItemRecord(id: item.id, payload: item.payload)
isolatedContext.insert(record)
}
try isolatedContext.save()
}
}Case Study 05: Modernizing from ObservableObject to Swift 6 @Observable
The Legacy Pattern (Swift 5 / iOS 14-16)
// Old: Requires explicit @MainActor and Combine @Published
@MainActor
final class LegacyViewModel: ObservableObject {
@Published var items: [String] = []
@Published var isLoading: Bool = false
func load() {
isLoading = true
DispatchQueue.global().async {
let fetched = ["A", "B", "C"]
DispatchQueue.main.async {
self.items = fetched
self.isLoading = false
}
}
}
}The Modern Swift 6 Pattern (iOS 17+ / macOS 14+)
import Observation
import SwiftUI
@Observable
@MainActor
public final class ModernViewModel {
public var items: [String] = []
public var isLoading: Bool = false
public init() {}
public func load() async {
isLoading = true
defer { isLoading = false }
// Execute background work off MainActor
let fetched = await Task.detached(priority: .userInitiated) {
// Expensive computation or decoding
return ["A", "B", "C"]
}.value
// Return to @MainActor domain automatically
self.items = fetched
}
}9. Comprehensive Swift Concurrency Terminology Reference
- Actor: A reference type that protects its mutable state through data isolation, ensuring only one task can execute its code at any given time.
- Global Actor: A singleton actor (such as
@MainActor) that provides global mutual exclusion across disparate types and functions. - MainActor: The global actor representing the system's main thread and user interface runloop.
- Sendable: A marker protocol indicating that a type's values can safely be transferred across concurrency boundaries without risk of data races.
- @Sendable Closure: A closure that only captures
Sendabletypes and does not mutate captured local state. - Isolated Parameter: A method parameter annotated with
isolatedthat temporarily brings the method into the isolation domain of that actor instance. - Nonisolated: Code within an actor that is explicitly decoupled from the actor's mailbox executor, allowing synchronous calls from outside.
- nonisolated(unsafe): An escape hatch attribute in Swift 6 disabling compile-time data race checks for a specific variable or property. Must be used with extreme caution.
- @preconcurrency import: A compiler directive that suppresses strict concurrency diagnostics for imported modules that have not yet adopted Swift 6.
- Structured Concurrency: Concurrency where child tasks are lexically scoped within parent scopes (
async let,TaskGroup), inheriting cancellation and priority. - Unstructured Concurrency: Concurrency spawned via
Task.init, inheriting task context but having independent lifetimes. - Detached Concurrency: Concurrency created via
Task.detached, completely severed from parent context, priority, and task locals. - Task Cancellation: A cooperative signaling mechanism where parent tasks notify child tasks to cease execution via
isCancelled. - Cooperative Thread Pool: The fixed-size thread pool used by the Swift runtime, containing exactly one thread per CPU core.
- Actor Reentrancy: The behavior where an actor yields execution at suspension points (
await), permitting other tasks to interleave execution before the initial task resumes.
10. Verification Script: Strict Concurrency Audit
AI agents should run this automated verification script in target Swift packages before concluding any concurrency refactor:
#!/usr/bin/env bash
set -euo pipefail
echo "======================================================="
echo " SWIFT 6 STRICT CONCURRENCY AUDIT SUITE "
echo "======================================================="
# 1. Compiler version verification
SWIFT_VERSION=$(swift --version | head -n 1)
echo "Target Compiler: $SWIFT_VERSION"
# 2. Strict concurrency build with complete diagnostics
echo "Building package with complete concurrency checking..."
swift build \
-Xswiftc -strict-concurrency=complete \
-Xswiftc -warnings-as-errors
# 3. Parallelized test execution
echo "Executing test suite across concurrent workers..."
swift test --parallel
echo "======================================================="
echo "✅ CONCURRENCY AUDIT PASSED: ZERO DATA RACES DETECTED"
echo "======================================================="Case Study 06: CoreBluetooth CBCentralManager Delegate to AsyncStream
Bridging CoreBluetooth to modern Swift 6 concurrency requires strict thread safety and lifecycle handling:
import CoreBluetooth
import Foundation
public final class BluetoothScannerBridge: NSObject, CBCentralManagerDelegate, @unchecked Sendable {
public struct DiscoveredPeripheral: Sendable, Identifiable {
public let id: UUID
public let name: String?
public let rssi: Int
}
private var centralManager: CBCentralManager?
private var continuation: AsyncStream<DiscoveredPeripheral>.Continuation?
public override init() {
super.init()
}
public func startScanning() -> AsyncStream<DiscoveredPeripheral> {
AsyncStream { continuation in
self.continuation = continuation
self.centralManager = CBCentralManager(delegate: self, queue: nil)
continuation.onTermination = { [weak self] _ in
self?.centralManager?.stopScan()
}
}
}
public func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == .poweredOn {
central.scanForPeripherals(withServices: nil, options: nil)
}
}
public func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
let item = DiscoveredPeripheral(
id: peripheral.identifier,
name: peripheral.name,
rssi: RSSI.intValue
)
continuation?.yield(item)
}
}Case Study 07: Audio Buffer Circular Queue with os_unfair_lock
Real-time audio rendering callbacks (CoreAudio / AVAudioEngine) run on high-priority kernel threads where allocating memory or awaiting an actor is forbidden:
import os.lock
import Foundation
public final class RealtimeAudioRingBuffer: @unchecked Sendable {
private var lock = os_unfair_lock()
private var buffer: [Float]
private var writeIndex: Int = 0
private var readIndex: Int = 0
private let capacity: Int
public init(capacity: Int = 8192) {
self.capacity = capacity
self.buffer = [Float](repeating: 0, count: capacity)
}
public func write(samples: UnsafePointer<Float>, count: Int) {
os_unfair_lock_lock(&lock)
defer { os_unfair_lock_unlock(&lock) }
for i in 0..<count {
buffer[writeIndex] = samples[i]
writeIndex = (writeIndex + 1) % capacity
}
}
public func read(into destination: UnsafeMutablePointer<Float>, count: Int) {
os_unfair_lock_lock(&lock)
defer { os_unfair_lock_unlock(&lock) }
for i in 0..<count {
destination[i] = buffer[readIndex]
readIndex = (readIndex + 1) % capacity
}
}
}Case Study 08: Swift 6 Concurrency in Unit Testing with Swift Testing
Apple's modern Testing framework (Swift 6) natively integrates with async/await and actor isolation:
import Testing
import Foundation
@Suite("Account Service Concurrency Suite")
struct AccountServiceTests {
@Test("Verify isolated balance mutation under concurrent transfers")
func concurrentTransfers() async throws {
let account = BankAccount()
await withTaskGroup(of: Void.self) { group in
for _ in 0..<20 {
group.addTask {
_ = try? await account.withdraw(amount: 5)
}
}
}
// Assert final state deterministically without sleep
#expect(account != nil)
}
@Test("Verify task cancellation propagation")
func cancellationCheck() async throws {
let task = Task {
try await Task.sleep(for: .seconds(10))
return "Completed"
}
task.cancel()
await #expect(throws: CancellationError.self) {
try await task.value
}
}
}Case Study 09: Dynamic Isolation Hop Utility
A reusable helper for transferring closures safely onto the MainActor from background contexts:
public enum ConcurrencyBridge {
@MainActor
public static func onMain<T: Sendable>(_ block: @MainActor () throws -> T) rethrows -> T {
try block()
}
public static func runOnMainAsync<T: Sendable>(_ block: @escaping @MainActor () async throws -> T) async throws -> T {
try await MainActor.run {
try await block()
}
}
}Case Study 10: Memory-Bounded Async Task Processor
Preventing out-of-memory crashes when processing streaming inputs:
public actor BoundedTaskProcessor<Item: Sendable, Output: Sendable> {
private let limit: Int
private var inFlight: Int = 0
private let transform: @Sendable (Item) async throws -> Output
public init(limit: Int = 4, transform: @escaping @Sendable (Item) async throws -> Output) {
self.limit = limit
self.transform = transform
}
public func processBatch(items: [Item]) async throws -> [Output] {
try await withThrowingTaskGroup(of: Output.self) { group in
var outputs: [Output] = []
var iterator = items.makeIterator()
for _ in 0..<limit {
if let next = iterator.next() {
group.addTask { try await self.transform(next) }
}
}
for try await output in group {
outputs.append(output)
if let next = iterator.next() {
group.addTask { try await self.transform(next) }
}
}
return outputs
}
}
}Case Study 11: Actor Isolation with SwiftUI Property Wrappers
Understanding how @State, @Binding, and @StateObject interact with Swift 6 actors:
import SwiftUI
public struct ConcurrentDataFeedView: View {
@State private var feedItems: [String] = []
@State private var isLoading: Bool = false
public init() {}
public var body: some View {
List(feedItems, id: \.self) { item in
Text(item)
}
.overlay {
if isLoading {
ProgressView()
}
}
.task {
// .task automatically runs on @MainActor, and auto-cancels on view dismissal!
isLoading = true
defer { isLoading = false }
do {
let items = try await fetchRemoteFeed()
feedItems = items
} catch {
feedItems = []
}
}
}
private func fetchRemoteFeed() async throws -> [String] {
// Runs cooperatively
return ["Update 1", "Update 2", "Update 3"]
}
}Case Study 12: High-Frequency Telemetry Batching Actor
Batching rapid sensor or analytics events to avoid overloading network or disk storage:
import Foundation
public actor TelemetryBatcher {
private var eventBuffer: [String] = []
private var flushTask: Task<Void, Never>?
private let maxBatchSize: Int
private let flushInterval: Duration
public init(maxBatchSize: Int = 50, flushInterval: Duration = .seconds(5)) {
self.maxBatchSize = maxBatchSize
self.flushInterval = flushInterval
}
public func logEvent(_ event: String) {
eventBuffer.append(event)
if eventBuffer.count >= maxBatchSize {
flush()
} else if flushTask == nil {
scheduleFlush()
}
}
private func scheduleFlush() {
flushTask = Task { [weak self] in
try? await Task.sleep(for: self?.flushInterval ?? .seconds(5))
await self?.flush()
}
}
private func flush() {
flushTask?.cancel()
flushTask = nil
guard !eventBuffer.isEmpty else { return }
let batch = eventBuffer
eventBuffer.removeAll()
Task.detached(priority: .utility) {
// Transmit batch payload to analytics backend
_ = batch
}
}
}Case Study 13: Protocol Concurrency Requirements and Associated Types
How to define strictly typed, race-free protocols in modular Swift packages:
import Foundation
public protocol AsyncRepository: Sendable {
associatedtype Entity: Identifiable & Sendable
associatedtype ID: Sendable
func find(by id: ID) async throws -> Entity?
func save(_ entity: Entity) async throws
func delete(by id: ID) async throws
}
public actor InMemoryRepository<T: Identifiable & Sendable>: AsyncRepository where T.ID: Sendable {
public typealias Entity = T
public typealias ID = T.ID
private var storage: [ID: Entity] = [:]
public init() {}
public func find(by id: ID) async throws -> Entity? {
return storage[id]
}
public func save(_ entity: Entity) async throws {
storage[entity.id] = entity
}
public func delete(by id: ID) async throws {
storage.removeValue(forKey: id)
}
}11. Diagnostic Decision Tree for AI Agents
[Swift 6 Compilation Error Detected]
│
▼
Does error mention 'Sendable' or 'Actor isolation'?
├── YES ───────────────────┐
│ │
▼ ▼
Is it about a Property? Is it about a Closure?
├── YES ──┐ ├── YES ──┐
│ │ │ │
▼ ▼ ▼ ▼
MainActor? Actor? Captures? Escaping?
│ │ │ │
▼ ▼ ▼ ▼
Add await Mark Use [let] Mark
MainActor. nonisolated capture @Sendable
run { } let list12. Conclusion & Verification Summary
By applying the formal invariants in this manual:
- Zero Data Races: Concurrency guarantees are verified at compile time.
- Zero Runtime Deadlocks: The cooperative pool remains unblocked.
- Seamless CI: Builds succeed cleanly under
macos-14,macos-15, and Linux runners. - Predictable Architecture: Code remains maintainable, isolated, and responsive under high user load.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

