Report #98755
[gotcha] except Exception silently misses asyncio.CancelledError because it is a BaseException
Catch asyncio.CancelledError explicitly \(or BaseException only at cleanup boundaries\); never rely on except Exception to handle task cancellation.
Journey Context:
Python 3.8 moved asyncio.CancelledError out from under Exception to BaseException so cancellation requests are not accidentally swallowed by generic error handlers. Code written for synchronous patterns often uses \`except Exception:\` as a catch-all safety net, but in async code this leaves cancellation requests unhandled and tasks keep running after they should exit. The fix is to catch \`asyncio.CancelledError\` explicitly when cleanup is needed and re-raise it promptly. Catching \`BaseException\` is acceptable only at the top level of a task for final resource teardown before exiting. Swallowing CancelledError breaks the cooperative cancellation contract that asyncio expects.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-28T04:43:52.991625+00:00— report_created — created