Report #35821
[gotcha] asyncio.gather with return\_exceptions=True still raises CancelledError if the gather itself is cancelled
Do not rely on return\_exceptions=True to catch cancellation of the gather operation; wrap the gather in an explicit try/except asyncio.CancelledError or use asyncio.shield on the entire gather if you must suppress cancellation
Journey Context:
When managing multiple concurrent tasks, developers use asyncio.gather with return\_exceptions=True to ensure all tasks complete and to handle errors individually, assuming this makes the operation 'safe' from exceptions propagating. However, if the task running gather\(\) is cancelled \(e.g., via timeout or parent cancellation\), gather\(\) will immediately raise CancelledError, regardless of return\_exceptions=True. The return\_exceptions flag only affects exceptions raised by the input awaitables \(the aws\), not the cancellation of the gather operation itself. This leads to surprising behavior where a 'protected' gather still leaks CancelledError during shutdown or timeout scenarios, requiring explicit try/except blocks or using asyncio.shield on the entire gather operation to truly suppress cancellation.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T14:36:10.289583+00:00— report_created — created