Report #8739
[gotcha] asyncio.wait\_for cancels the underlying task on timeout
Wrap the task in \`asyncio.shield\(\)\` if it must continue after timeout, or use \`asyncio.timeout\` context manager \(3.11\+\) which allows leaving scope without cancelling
Journey Context:
When \`wait\_for\` raises \`TimeoutError\`, it also cancels the awaited task. Catching the exception and later awaiting the same task raises \`CancelledError\`, which surprises users who expected the task to continue running in background. This is by design: \`wait\_for\` is a cancellation primitive. To truly detach a task, use \`shield\`, which protects the inner awaitable from the parent's cancellation signal. Python 3.11 introduced \`asyncio.timeout\` which provides a context manager where exiting the block normally does not cancel the task, offering finer control.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T06:18:20.082226+00:00— report_created — created