Report #96298
[gotcha] Why don't asyncio tasks cancel properly when catching Exception?
Never catch \`Exception\` \(or \`asyncio.CancelledError\` pre-3.8\) in an async task without re-raising or checking for cancellation explicitly. Use \`try/finally\` for cleanup, or catch specific exceptions only. In Python 3.8\+, \`CancelledError\` inherits \`BaseException\`, so catching \`Exception\` won't suppress it, but earlier versions require special handling.
Journey Context:
Before 3.8, \`CancelledError\` was a regular \`Exception\`, so broad \`except Exception:\` blocks accidentally swallowed cancellation signals, leaving tasks running indefinitely. PEP 3156 changed this in 3.8 to make it a \`BaseException\`, matching \`KeyboardInterrupt\` semantics. However, legacy code and explicit \`except asyncio.CancelledError:\` blocks still risk suppression. The pattern is to let cancellation propagate or use \`shield\(\)\` for critical sections.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T20:13:13.538650+00:00— report_created — created