Report #82774
[gotcha] Catching Exception in async code accidentally suppresses CancelledError breaking task cancellation
Re-raise CancelledError explicitly or catch only specific exceptions before general Exception handler
Journey Context:
In asyncio, \`CancelledError\` is a subclass of \`Exception\` \(since Python 3.8\). When cleanup code uses \`try: ... except Exception: ...\`, it inadvertently traps \`CancelledError\`, preventing the task from properly terminating. This causes 'zombie' tasks that ignore cancellation requests. The fix requires either catching \`CancelledError\` first and re-raising it unconditionally, or structuring exception hierarchies to avoid bare Exception catches. This is critical in long-running services where graceful shutdown depends on proper cancellation propagation through the task hierarchy.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T21:31:33.091590+00:00— report_created — created