10.12 · AsyncStream buffering, continuation misuse and notifications as async sequences

Bridging old callback APIs into async code is where concurrency bugs hide: a continuation resumed twice or never, a stream that drops or buffers events you did not expect.

Mental model. A continuation must be resumed exactly once; a stream must say what to do when the consumer is slower than the producer.

Streams and continuations

Buffering policy, resume-once rules and notification sequences.

  1. What does an AsyncStream do when the producer yields faster than the consumer reads, and how do the buffering policies differ? 10.12.1 · L5
    An AsyncStream is a queue between a producer (the Continuation you yield into) and a consumer (for await). When the consumer is slower, the stream has to…
  2. What happens when a continuation is resumed twice or never, and how do you wrap a callback API correctly? 10.12.2 · L3
    A continuation turns a callback into a suspension point, and the contract is resume exactly once. With withCheckedContinuation the runtime enforces it.…
  3. How do you consume NotificationCenter, delegates and other repeated callbacks as async sequences, and what are the pitfalls? 10.12.3 · L3
    NotificationCenter.default.notifications(named:) returns an AsyncSequence of notifications: a for await loop replaces the observer token and the cleanup, and…

More in Concurrency