swift-concurrency/SKILL.md
Expert guidance on Swift Concurrency best practices, patterns, and implementation. Use when developers mention: (1) Swift Concurrency, async/await, actors, or tasks, (2) modern concurrency patterns or structured concurrency, (3) migrating to Swift 6 or strict concurrency checking, (4) data races or thread safety issues, (5) refactoring closures to async/await, (6) @MainActor, Sendable, or actor isolation, (7) concurrent code architecture or performance, (8) concurrency-related linter warnings, (9) AsyncSequence, AsyncStream, or task groups, (10) nonisolated, @preconcurrency, or @unchecked Sendable.
npx skillsauth add wendylabsinc/claude-skills swift-concurrencyInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
This skill provides expert guidance on Swift Concurrency, covering modern async/await patterns, actors, tasks, Sendable conformance, and migration to Swift 6. Use this skill to help developers write safe, performant concurrent code and navigate the complexities of Swift's structured concurrency model.
@MainActor, custom actor, actor instance isolation, or nonisolated.@MainActor as a blanket fix. Justify why main-actor isolation is correct for the code.Task.detached only with a clear reason.@preconcurrency, @unchecked Sendable, or nonisolated(unsafe), require:
Concurrency behavior depends on build settings. Always try to determine:
@MainActor or nonisolated?)NonisolatedNonsendingByDefault)Package.swift for .defaultIsolation(MainActor.self).Package.swift for .enableUpcomingFeature("NonisolatedNonsendingByDefault")..enableExperimentalFeature("StrictConcurrency=targeted") (or similar).// swift-tools-version: ...project.pbxproj for:
SWIFT_DEFAULT_ACTOR_ISOLATIONSWIFT_STRICT_CONCURRENCYSWIFT_UPCOMING_FEATURE_ (and/or SWIFT_ENABLE_EXPERIMENTAL_FEATURES)If any of these are unknown, ask the developer to confirm them before giving migration-sensitive guidance.
When a developer needs concurrency guidance, follow this decision tree:
Starting fresh with async code?
references/async-await-basics.md for foundational patternsreferences/tasks.md (async let, task groups)Protecting shared mutable state?
references/actors.md (actors, @MainActor)references/sendable.md (Sendable conformance)Managing async operations?
references/tasks.md (Task, child tasks, cancellation)references/async-sequences.md (AsyncSequence, AsyncStream)Performance or debugging issues?
references/performance.md (profiling, suspension points)references/testing.md (XCTest, Swift Testing)references/linting.md for rule intent and preferred fixes; avoid dummy awaits as “fixes”.async_without_await warning
async if not required; if required by protocol/override/@concurrent, prefer narrow suppression over adding fake awaits. See references/linting.md.references/sendable.md and references/threading.md (especially Swift 6.2 behavior changes)@MainActorreferences/actors.md (global actors, nonisolated, isolated parameters) and references/threading.md (default isolation)references/threading.md to avoid thread-centric debugging and rely on isolation + Instrumentsreferences/testing.md (await fulfillment(of:) and Swift Testing patterns)references/core-data.md (DAO/NSManagedObjectID, default isolation conflicts)async/await - Making existing synchronous code asynchronous
// Use for: Single asynchronous operations
func fetchUser() async throws -> User {
try await networkClient.get("/user")
}
async let - Running multiple independent async operations in parallel
// Use for: Fixed number of parallel operations known at compile time
async let user = fetchUser()
async let posts = fetchPosts()
let profile = try await (user, posts)
Task Group - Dynamic parallel operations with structured concurrency
// Use for: Unknown number of parallel operations at compile time
await withTaskGroup(of: Result.self) { group in
for item in items {
group.addTask { await process(item) }
}
}
Actor - Protecting mutable state from data races
// Use for: Shared mutable state accessed from multiple contexts
actor DataCache {
private var cache: [String: Data] = [:]
func get(_ key: String) -> Data? { cache[key] }
}
@MainActor - Ensuring UI updates on main thread
// Use for: View models, UI-related classes
@MainActor
class ViewModel: ObservableObject {
@Published var data: String = ""
}
Scenario: Multiple parallel network requests
async let users = fetchUsers()
async let posts = fetchPosts()
async let comments = fetchComments()
let (u, p, c) = try await (users, posts, comments)
Scenario: Processing array items in parallel
await withTaskGroup(of: ProcessedItem.self) { group in
for item in items {
group.addTask { await process(item) }
}
for await result in group {
results.append(result)
}
}
Key changes in Swift 6:
For detailed migration steps, see references/migration.md.
Load these files as needed for specific topics:
async-await-basics.md - async/await syntax, execution order, async let, URLSession patternstasks.md - Task lifecycle, cancellation, priorities, task groups, structured vs unstructuredthreading.md - Thread/task relationship, suspension points, isolation domains, nonisolatedactors.md - Actor isolation, @MainActor, global actors, reentrancy, custom executors, Mutexsendable.md - Sendable conformance, value/reference types, @unchecked, region isolationasync-sequences.md - AsyncSequence, AsyncStream, when to use vs regular async methodsperformance.md - Profiling with Instruments, reducing suspension points, execution strategiestesting.md - XCTest async patterns, Swift Testing, concurrency testing utilitiesproduction-patterns.md - Advanced patterns: actor state machines, ~Copyable state, structured concurrency, lock-free atomics, service lifecycleWhen managing resources that need cleanup (connections, file handles, etc.), use the withX pattern to ensure proper cleanup through structured concurrency.
Bad - Unstructured cleanup:
func withConnection<R>(
_ body: (Connection) async throws -> R
) async throws -> R {
let connection = Connection()
try await connection.connect()
defer {
Task {
await connection.disconnect() // Unstructured! May not complete
}
}
return try await body(connection)
}
Better - Explicit cleanup in all paths:
func withConnection<R>(
_ body: (Connection) async throws -> R
) async throws -> R {
let connection = Connection()
try await connection.connect()
do {
let result = try await body(connection)
await connection.disconnect()
return result
} catch {
await connection.disconnect()
throw error
}
}
Best - Make the pattern impossible to misuse:
actor Client {
private let connection: Connection // Non-optional, always valid
private init(connection: Connection) {
self.connection = connection
}
static func withConnection<R>(
_ body: (Client) async throws -> R
) async throws -> R {
let connection = try await Connection.establish()
let client = Client(connection: connection)
do {
let result = try await body(client)
await connection.close()
return result
} catch {
await connection.close()
throw error
}
}
// No public connect()/disconnect() methods - can't forget cleanup
}
This pattern:
withConnectionnotConnected error cases entirelyWhen accessing C globals like stdout in Swift 6 strict concurrency mode, use a local nonisolated(unsafe) variable:
@inline(__always)
private func flushStdout() {
nonisolated(unsafe) let out = stdout
fflush(out)
}
The nonisolated(unsafe) annotation tells the compiler to skip concurrency checking for this specific access to the C global.
Use withThrowingDiscardingTaskGroup when you need to spawn multiple tasks but don't need to collect their individual results:
try await withThrowingDiscardingTaskGroup { taskGroup in
taskGroup.addTask {
try await handleClient(client1)
}
taskGroup.addTask {
try await handleClient(client2)
}
}
Check for both cancellation and graceful shutdown in long-running loops:
while !Task.isShuttingDownGracefully && !Task.isCancelled {
try await handle(value)
}
// Use gracefulShutdown() for clean termination
try await gracefulShutdown()
AsyncStream has no backpressure. If the producer yields faster than the consumer processes, values accumulate in an unbounded buffer, leading to memory growth.
Prefer mapping NIOAsyncSequence or other backpressured sequences instead:
// Bad - no backpressure, unbounded buffer
let stream = AsyncStream<Message> { continuation in
Task {
for await rawMessage in nioChannel.inbound {
continuation.yield(Message(rawMessage))
}
continuation.finish()
}
}
// Good - preserves backpressure from NIO
let messages = nioChannel.inbound.map { Message($0) }
for try await message in messages {
process(message)
}
If you must use AsyncStream, use structured concurrency with makeStream():
func run() async {
let (stream, continuation) = AsyncStream<Value>.makeStream()
async let producer: Void = runProducer(continuation: continuation)
for await value in stream {
process(value)
}
await producer
}
private func runProducer(
continuation: sending AsyncStream<Value>.Continuation
) async {
let cont = consume continuation // Transfer ownership
try? await withThrowingDiscardingTaskGroup { group in
group.addTask {
while !Task.isCancelled {
cont.yield(produceValue())
try await Task.sleep(for: .seconds(1))
}
}
}
cont.finish()
}
Key points:
sending parameter transfers ownership into the functionconsume continuation allows safe sharing across task group childrenAsyncStream.Continuation is Sendable - no need for nonisolated(unsafe)Task { } inside AsyncStream.init - it's unstructured concurrencyreferences/testing.md).references/performance.md).with-style APIs.See references/glossary.md for quick definitions of core concurrency terms used across this skill.
development
Autonomous continuous integration loop for the Wendy Cloud repository. Use when asked to: (1) iterate on the cloud stack, (2) find and fix bugs in Wendy Cloud, (3) run the cloud test loop, (4) continuously test the Swift broker, (5) scan for regressions after new features. This skill manages the full local dev stack autonomously including starting Docker, the Swift broker, pki-core, running tests, diagnosing failures, and applying fixes.
development
Expert guidance on Swift best practices, patterns, and implementation. Use when developers mention: (1) Swift configuration or environment variables, (2) swift-log or logging patterns, (3) OpenTelemetry or swift-otel, (4) Swift Testing framework or @Test macro, (5) Foundation avoidance or cross-platform Swift, (6) platform-specific code organization, (7) Span or memory safety patterns, (8) non-copyable types (~Copyable), (9) API design patterns or access modifiers.
tools
Expert guidance on building and deploying apps to WendyOS edge devices. Use when developers mention: (1) Wendy or WendyOS, (2) wendy CLI commands, (3) wendy.json or entitlements, (4) deploying apps to edge devices, (5) remote debugging Swift on ARM64, (6) NVIDIA Jetson or Raspberry Pi apps, (7) cross-compiling Swift for ARM64.
development
Curated Swift package ecosystem for WendyOS and Linux. Use when developers mention: (1) Swift packages for Linux or ARM64/AMD64, (2) choosing a Swift library, (3) Swift Package Index, (4) swiftpackageindex.com, (5) what Swift library to use, (6) Swift on WendyOS dependencies, (7) edge computing Swift libraries.