ZombieMutex

30 results · ↑ ↓ navigate · Enter open · Esc close

All snippets

Swift / Reference example

Observe cancellation inside a Swift task

Stop a loop cooperatively; cancellation is not remote transaction rollback.

Swift

func transform(_ items: [String]) async throws -> [String] {
    var output: [String] = []
    output.reserveCapacity(items.count)
    for item in items {
        try Task.checkCancellation()
        output.append(item.trimmingCharacters(in: .whitespacesAndNewlines))
    }
    return output
}

What matters

Cancellation only stops work that cooperates with it. Check around CPU-intensive loops and avoid treating a cancelled network request as an undone server mutation.

Test these cases

  • Cancelled before the loop
  • Cancelled partway through
  • Caller propagates CancellationError

Sources and limits

This is a reference snippet, not a substitute for reviewing the full vendor API contract, concurrency rules or production threat model. Do not paste secrets into sample code.