Report #71581
[gotcha] asyncio.wait\_for timeout leaves task running if task swallows CancelledError
Always re-raise asyncio.CancelledError after cleanup in tasks intended for wait\_for; use asyncio.timeout \(3.11\+\) or shielding with explicit cancellation handling to avoid orphan background work.
Journey Context:
When wait\_for hits its deadline, it sends cancellation to the task. If the task catches CancelledError to perform cleanup but forgets to re-raise, wait\_for raises TimeoutError to its caller, but the task continues executing in the background detached from any awaiter. This creates zombie coroutines that may hold resources or mutate state unexpectedly. Alternatives like shielding prevent the cancellation from reaching the inner task, making the intent explicit. Python 3.11's asyncio.timeout is safer as it manages the cancellation lifecycle explicitly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T02:43:41.813225+00:00— report_created — created