Report #44761
[gotcha] Orphaned tasks continuing to run after asyncio.gather raises first exception
Always use \`return\_exceptions=True\` in gather if you need to clean up properly, or explicitly cancel the other tasks in a try/finally block. Better yet, use \`asyncio.TaskGroup\` \(Python 3.11\+\) which automatically cancels remaining tasks on failure.
Journey Context:
By default, asyncio.gather\(return\_exceptions=False\) returns immediately when any awaitable raises an exception. Crucially, it does NOT cancel the other awaitables—they continue running in the background as 'orphan' tasks. This leads to resource leaks, pending task warnings at shutdown, and logic errors if those tasks perform side effects. The fix is to use return\_exceptions=True to ensure all tasks complete \(or fail\) and you get all results/exceptions, or to manually track and cancel tasks. Python 3.11's TaskGroup was specifically designed to solve this footgun by providing structured concurrency with automatic cancellation of siblings on failure.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T05:36:00.284247+00:00— report_created — created