Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Diagnose and fix Swift Concurrency issues: async/await, actor isolation, Sendable, and Swift 6 migration.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
SKILL.md
1---2name: swift-concurrency3description: Diagnose Swift Concurrency issues, refactor callback-based code to async/await, and guide Swift 6 migration when working with tasks, actors, @MainActor, Sendable, data races, thread safety, or concurrency-related compiler and linter warnings.4---5# Swift Concurrency67## Fast Path89Before proposing a fix:10111. Analyze `Package.swift` or `.pbxproj` to determine Swift language mode, strict concurrency level, default isolation, and upcoming features. Do this always, not only for migration work.122. Capture the exact diagnostic and offending symbol.133. Determine the isolation boundary: `@MainActor`, custom actor, actor instance isolation, or `nonisolated`.144. Confirm whether the code is UI-bound or intended to run off the main actor. When spawning unstructured tasks, inspect the synchronous prefix (everything before the first `await`): start on `@MainActor` only when that prefix truly needs main-actor access; otherwise use `Task { @concurrent in ... }` and hop back with `MainActor.run` only after the suspension. A trivial non-main line (for example, `print`) followed by main-actor work in the same prefix is not a reason to use `@concurrent`. For delayed retries, timers, and backoff tasks, separate the waiting from the UI mutation. The sleep often belongs off the main actor even when the final state update belongs on it.1516Project settings that change concurrency behavior:1718| Setting | SwiftPM (`Package.swift`) | Xcode (`.pbxproj`) |19|---|---|---|20| Language mode | `swiftLanguageVersions` or `-swift-version` (`// swift-tools-version:` is not a reliable proxy) | Swift Language Version |21| Strict concurrency | `.enableExperimentalFeature("StrictConcurrency=targeted")` | `SWIFT_STRICT_CONCURRENCY` |22| Default isolation | `.defaultIsolation(MainActor.self)` | `SWIFT_DEFAULT_ACTOR_ISOLATION` |23| Upcoming features | `.enableUpcomingFeature("NonisolatedNonsendingByDefault")` | `SWIFT_UPCOMING_FEATURE_*` |24| Approachable Concurrency | N/A (use individual upcoming features) | `SWIFT_APPROACHABLE_CONCURRENCY` |2526> **Xcode 26 note**: New projects created in Xcode 26 will often start with `SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor` and `SWIFT_APPROACHABLE_CONCURRENCY = YES` enabled by default. Treat these as likely defaults for newly created projects, not as confirmed settings.2728If any of these are unknown, ask the developer to confirm them before giving migration-sensitive guidance. Do not guess, even for new Xcode 26 projects.2930Guardrails:3132- Do not recommend `@MainActor` as a blanket fix. Justify why the code is truly UI-bound.33- Prefer structured concurrency over unstructured tasks. Use `Task.detached` only with a clear reason.34- If recommending `@preconcurrency`, `@unchecked Sendable`, or `nonisolated(unsafe)`, require a documented safety invariant and a follow-up removal plan.35- Optimize for the smallest safe change. Do not refactor unrelated architecture during migration.36- Course references are for deeper learning only. Use them sparingly and only when they clearly help answer the developer's question.3738## Quick Fix Mode3940Use Quick Fix Mode when all of these are true:4142- The issue is localized to one file or one type.43- The isolation boundary is clear.44- The fix can be explained in 1-2 behavior-preserving steps.4546Skip Quick Fix Mode when any of these are true:4748- Build settings or default isolation are unknown.49- The issue crosses module boundaries or changes public API behavior.50- The likely fix depends on unsafe escape hatches.5152## Common Diagnostics5354| Diagnostic | First check | Smallest safe fix | Escalate to |55|---|---|---|---|56| `Main actor-isolated ... cannot be used from a nonisolated context` | Is this truly UI-bound? | Isolate the caller to `@MainActor` or use `await MainActor.run { ... }` only when main-actor ownership is correct. | `references/actors.md`, `references/threading.md` |57| `Actor-isolated type does not conform to protocol` | Must the requirement run on the actor? | Prefer isolated conformance (e.g., `extension Foo: @MainActor SomeProtocol`); use `nonisolated` only for truly nonisolated requirements. | `references/actors.md` |58| `Sending value of non-Sendable type ... risks causing data races` | What isolation boundary is being crossed? | Keep access inside one actor, or convert the transferred value to an immutable/value type. | `references/sendable.md`, `references/threading.md` |59| `SwiftLint async_without_await` | Is `async` actually required by protocol, override, or `@concurrent`? | Remove `async`, or use a narrow suppression with rationale. Never add fake awaits. | `references/linting.md` |60| `wait(...) is unavailable from asynchronous contexts` | Is this legacy XCTest async waiting? | Replace with `await fulfillment(of:)` or Swift Testing equivalents. | `references/testing.md` |61| Core Data concurrency warnings | Are `NSManagedObject` instances crossing contexts or actors? | Pass `NSManagedObjectID` or map to a Sendable value type. | `references/core-data.md` |62| `Thread.current` unavailable from asynchronous contexts | Are you debugging by thread instead of isolation? | Reason in terms of isolation and use Instruments/debugger instead. | `references/threading.md` |63| SwiftLint concurrency-related warnings | Which specific lint rule triggered? | Use `references/linting.md` for rule intent and preferred fixes; avoid dummy awaits. | `references/linting.md` |64| `... cannot satisfy conformance requirement for a 'Sendable' type parameter` (`SendableMetatype`) | Does the conformance carry global-actor isolation? | Remove actor isolation from the conformance, or avoid passing the metatype across isolation boundaries. See `SendableMetatype` section in `references/actors.md`. | `references/actors.md` |6566## When Quick Fixes Fail67681. Gather project settings if not already confirmed.692. Re-evaluate which isolation boundaries the type crosses.703. Route to the matching reference file for a deeper fix.714. If the fix may change behavior, document the invariant and add verification steps.7273## Smallest Safe Fixes7475Prefer changes that preserve behavior while satisfying data-race safety:7677- **UI-bound state**: isolate the type or member to `@MainActor`.78- **Shared mutable state**: move it behind an `actor`, or use `@MainActor` only if the state is UI-owned.79- **Background work**: when work must hop off caller isolation, use an `async` API marked `@concurrent`; when work can safely inherit caller isolation, use `nonisolated` without `@concurrent`. When spawning a `Task`, match entry isolation to its synchronous prefix. If nothing before the first `await` needs the main actor, use `Task { @concurrent in ... }` and hop back via `await MainActor.run { ... }` for the UI update. If the prefix mixes a trivial non-main statement with main-actor work, keep the inherited `@MainActor` start—splitting the cheap line off-main is not worth an extra hop.80- **Sendability issues**: prefer immutable values and explicit boundaries over `@unchecked Sendable`.8182## Concurrency Tool Selection8384| Need | Tool | Key Guidance |85|---|---|---|86| Single async operation | `async/await` | Default choice for sequential async work |87| Fixed parallel operations | `async let` | Known count at compile time; auto-cancelled on throw |88| Dynamic parallel operations | `withTaskGroup` | Unknown count; structured — cancels children on scope exit |89| Sync → async bridge | `Task { }` | Inherits actor context; use `Task.detached` only with documented reason |90| Shared mutable state | `actor` | Prefer over locks/queues; keep isolated sections small |91| UI-bound state | `@MainActor` | Only for truly UI-related code; justify isolation |9293### Common Scenarios9495**Network request with UI update**96```swift97Task { @concurrent in98let data = try await fetchData()99await MainActor.run { self.updateUI(with: data) }100}101```102103**Processing array items in parallel**104```swift105await withTaskGroup(of: ProcessedItem.self) { group in106for item in items {107group.addTask { await process(item) }108}109for await result in group {110results.append(result)111}112}113```114115116## Task entry isolation117118Match a `Task`'s entry isolation to its synchronous prefix (everything from `{` to the first `await`).119120- If anything in that prefix needs `@MainActor`, keep the inherited `@MainActor` start.121- If nothing in that prefix needs `@MainActor`, prefer `Task { @concurrent in ... }` and hop back only for UI-owned mutation.122123```swift124// ❌ Synchronous prefix is empty; first work hops away125Task {126await hopToOtherIsolationDomain()127}128129// ❌ Synchronous prefix is only `print` (trivial, non-main); first await hops away130Task {131print("Also not main-thread-bound")132await hopToOtherIsolationDomain()133}134135// ✅ Start off the main actor, hop back only for UI work136Task { @concurrent in137await hopToOtherIsolationDomain()138await MainActor.run { updateUI() }139}140141// ✅ Synchronous prefix DOES contain main-actor work — keep inheritance142Task {143print("debug") // trivial, non-main — rides along144self.isLoading = true // needs @MainActor, before any await145await fetchData()146}147```148149## Swift 6 Migration Quick Guide150151Key changes in Swift 6:152- **Strict concurrency checking** enabled by default153- **Complete data-race safety** at compile time154- **Sendable requirements** enforced on boundaries155- **Isolation checking** for all async boundaries156157### Migration Validation Loop158159Apply this cycle for each migration change:1601611. **Build** — Run `swift build` or Xcode build to surface new diagnostics1622. **Fix** — Address one category of error at a time (e.g., all Sendable issues first)1633. **Rebuild** — Confirm the fix compiles cleanly before moving on1644. **Test** — Run the test suite to catch regressions (`swift test` or Cmd+U)1655. **Only proceed** to the next file/module when all diagnostics are resolved166167If a fix introduces new warnings, resolve them before continuing. Never batch multiple unrelated fixes — keep commits small and reviewable.168169For detailed migration steps, see `references/migration.md`.170171## Reference Router172173Open the smallest reference that matches the question:174175- Foundations176- `references/async-await-basics.md` — async/await syntax, execution order, async let, URLSession patterns177- `references/tasks.md` — Task lifecycle, cancellation, priorities, task groups, structured vs unstructured178- `references/actors.md` — Actor isolation, @MainActor, global actors, reentrancy, custom executors, Mutex179- `references/sendable.md` — Sendable conformance, value/reference types, @unchecked, region isolation180- `references/threading.md` — Execution model, suspension points, Swift 6.2 isolation behavior181- Streams182- `references/async-sequences.md` — AsyncSequence, AsyncStream, when to use vs regular async methods183- `references/async-algorithms.md` — Debounce, throttle, merge, combineLatest, channels, timers184- Applied topics185- `references/testing.md` — Swift Testing first, XCTest fallback, leak checks186- `references/performance.md` — Profiling with Instruments, reducing suspension points, execution strategies187- `references/memory-management.md` — Retain cycles in tasks, memory safety patterns188- `references/core-data.md` — NSManagedObject sendability, custom executors, isolation conflicts189- Migration and tooling190- `references/migration.md` — Swift 6 migration strategy, closure-to-async conversion, @preconcurrency, FRP migration191- `references/linting.md` — Concurrency-focused lint rules and SwiftLint `async_without_await`192- Glossary193- `references/glossary.md` — Quick definitions of core concurrency terms194195## Verification Checklist196197When changing concurrency code:1981991. Re-check build settings before interpreting diagnostics.2002. Build and clear one category of errors before moving on. Do not batch unrelated fixes into the same change.2013. Run tests, especially actor-, lifetime-, and cancellation-sensitive tests.2024. Use Instruments for performance claims instead of guessing.2035. Verify deallocation and cancellation behavior for long-lived tasks.2046. Check `Task.isCancelled` in long-running operations.2057. Never use semaphores or ad hoc locking in async contexts when actor isolation or `Mutex` would express ownership more safely.206207---208209**Note**: This skill is based on the comprehensive [Swift Concurrency Course](https://www.swiftconcurrencycourse.com?utm_source=github&utm_medium=agent-skill&utm_campaign=skill-footer) by Antoine van der Lee.210