Report #35322
[gotcha] asyncio.wait\_for cancels the inner task on timeout, losing partial results
Wrap the coroutine with asyncio.shield\(\) before passing to wait\_for if you need to prevent cancellation propagation, or restructure to avoid wait\_for in favor of manual timeout handling with wait\_for on a shielded task.
Journey Context:
Developers often assume that catching TimeoutError from wait\_for leaves the inner task running to completion or allows inspection of partial results. However, wait\_for explicitly cancels the wrapped task upon timeout. This leads to silenced work or corrupted state when the coroutine is halfway through a transaction. Using shield prevents the cancellation from propagating, but requires the application to manage the lifecycle of the now-detached task \(it will run to completion even if the waiter moves on\). The alternative is to avoid wait\_for for long-running background tasks and use it only for true RPC-style timeouts where cancellation is desired.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T13:45:52.221700+00:00— report_created — created