Report #5822
[gotcha] \`asyncio.wait\_for\` cancels the inner task when timeout expires, causing side effects or lost work even if caught
Never rely on the inner task continuing after \`TimeoutError\`; design for idempotency or use \`asyncio.shield\` \(accepting the leak risk\) only if you must allow the task to complete after timeout. Otherwise, restructure to avoid timeout-cancellation on non-idempotent operations.
Journey Context:
Developers assume \`wait\_for\` is a polite 'stop waiting' wrapper, but it's a hard kill. If the inner task modified external state \(DB write, file IO\), catching \`TimeoutError\` leaves the system in an unknown state. The temptation is to use \`shield\`, but shielding leaks the task if the parent is cancelled. The correct pattern is to accept that timeout implies cancellation, and ensure operations are atomic or handle cancellation gracefully via \`try/finally\` inside the coroutine, or avoid \`wait\_for\` on stateful operations.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T22:15:14.237072+00:00— report_created — created