Cancellation interview problems

Cancellation is where many asyncio answers fall apart, which is why practical rounds test it directly. task.cancel() schedules a CancelledError to be raised inside the task at its next await. The task can run cleanup in finally or except CancelledError, and should then re-raise. asyncio.TaskGroup and asyncio.timeout both rely on this. asyncio.shield() protects an inner operation from the outer cancellation, and Task.cancelling() and uncancel() exist for the rare code that needs to know why it was cancelled.

The most common mistake is swallowing the cancellation. except Exception around an await is safe because CancelledError has subclassed BaseException since Python 3.8, but a bare except: or except BaseException that doesn't re-raise swallows the cancellation and leaves a task that refuses to stop. Others include doing a long await inside finally without a bound and assuming cancel() is synchronous. A strong answer releases locks and semaphores with async with, awaits the cancelled task to confirm it finished, and explains what state partial work leaves behind.

4 open problems

  1. 20Dynamic Request Batcherhard

    Group concurrent inference requests into batches that flush when full or when the oldest request has waited long enough, with cancellation and drain.

  2. 21Async TTL Cache with Request Coalescingmedium

    Build an asyncio cache whose entries expire after a TTL, where concurrent misses for the same key share one fetch that survives caller cancellation.

  3. 22Async Retry with Exponential Backoffmedium

    Retry a failing coroutine with capped exponential backoff, re-raise the last error, and never swallow cancellation.

  4. 25Async Request Deduplicatorhard

    Collapse concurrent fetches for the same key into one shared call, with per-caller timeouts that never cancel the shared work.

7 locked

These open in order of demand. Open one and press I want this one to ask for it.

  1. 10Async Inference with Retries and Timeouts Lockedhard

    Run async inference calls under a concurrency cap with a per-attempt timeout, bounded retries, attempt counts and correct cancellation.

  2. 13Cancellable Inference Manager Lockedmedium

    Build a manager that runs inference requests in the background with a concurrency cap, supports cancelling them, and reports each final result.

  3. 14Fair Async Token Bucket Lockedhard

    Implement an asyncio token bucket rate limiter that serves waiters in strict FIFO order and handles cancelled waiters without leaking tokens.

  4. 17GPU Memory Scheduler Lockedhard

    Admit async jobs onto a GPU with fixed memory in strict FIFO order, so large jobs never starve, and survive cancelled waiters without leaking memory.

  5. 18Streaming Inference Limiter Lockedmedium

    Wrap token-streaming backends in an async generator that caps concurrent streams, starts lazily, and frees its slot on every exit path.

  6. 41Atomic Async Download Lockedhard

    Save an async byte stream to disk atomically, enforcing an expected size and cleaning up on errors, callback failures and cancellation.

  7. 43Merge Async Streams Lockedhard

    Merge several async generators into one stream that yields items as soon as any source produces them, with bounded buffering and clean shutdown.