Part 3, Swift concurrency, pages 65 to 66
How does task cancellation work?
Must-know Β· Very common Β· Senior Β· Technical screen
Short answer
Cancellation is cooperative. The runtime marks a task cancelled, but your code must notice, by checking Task.isCancelled or calling try Task.checkCancellation() in long loops, and you let CancellationError propagate.
Senior answer
Marking a task cancelled does not stop it; it sets a flag and cancels child tasks. Well-behaved async APIs check the flag at suspension points, which is why structured tasks feel like they βjust cancel,β but your own loops must check explicitly. For bridged work, withTaskCancellationHandler lets you tear down a resource the moment cancellation arrives. The common bug is swallowing CancellationError in a generic catch, which hides the cancellation from the caller.
Why they ask
To confirm you know cancellation is not automatic for your own code.
Common trap
Assuming cancel() halts work, or catching and discarding CancellationError.
Code
for item in items {
try Task.checkCancellation() // stop promptly if cancelled
await process(item)
}Live interview wording
βCancellation is cooperative: the runtime flags the task, but my loops have to check isCancelled or call checkCancellation, and I let CancellationError propagate rather than swallowing it.β
Weak answer
βI call cancel() and the task stops.β
Interviewer push
βIt kept running after cancel(). Why?β
Recovery line
βCancellation is cooperative. cancel() sets a flag; my code has to check Task.isCancelled or call checkCancellation in the loop. A long loop that never checks runs to completion.β
What earns the point
Knowing cancellation is cooperative and your code must check it.


