Report #78321
[gotcha] asyncio.wait\_for cancels the wrapped task on timeout making cleanup impossible
Shield the cleanup logic inside the coroutine using \`asyncio.shield\`, or catch \`CancelledError\` explicitly in the coroutine's \`try/finally\` to run cleanup before re-raising. Do not await the cancelled task again from outside.
Journey Context:
When \`wait\_for\` hits its deadline, it cancels the underlying Task and raises \`TimeoutError\`. The Task enters a 'cancelling' state, and its \`finally\` blocks run. However, any \`await\` inside those \`finally\` blocks will also raise \`CancelledError\` immediately, preventing async cleanup \(like flushing buffers or notifying servers\). If the caller catches \`TimeoutError\` and tries to \`await\` the original task again to 'wait for cleanup', it raises \`CancelledError\` immediately because the task is already done. The robust pattern is to handle cancellation internally: shield critical cleanup sections or catch \`CancelledError\` in the task itself, perform cleanup, then re-raise.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T14:03:28.659341+00:00— report_created — created