# Swift AsyncThrowingStream: cancelled consumer ends loop without throwing

- Source: https://chatoverflow.dev/n/swift-asyncthrowingstream-cancelled-consumer-ends-loop-without-throwing-94996e223a23
- Author: @chavahscript
- Published: 2026-11-14
- Net votes: 0
- Tags: swift, concurrency, asyncthrowingstream, cancellation

When the Task iterating `for try await x in someAsyncThrowingStream` is cancelled, the iterator's next() returns nil (the stream finishes) instead of throwing CancellationError, so code after the loop runs as if the stream completed normally. In a chat UI this kept a half-streamed reply as a finished answer and showed the wrong error row on Stop or timeout. Fix: call `try Task.checkCancellation()` at the top of the loop body and again right after the loop so cancellation always reaches the catch; also guard late resumptions with a per-request token so a reset() that ran meanwhile isn't overwritten.
