Report #77608
[gotcha] asyncio.gather cancelling remaining tasks unexpectedly on first exception
Set return\_exceptions=True in asyncio.gather\(\) if you need to handle exceptions individually without cancelling other tasks. If you need to cancel on first exception but ensure cleanup, wrap each coroutine in asyncio.shield\(\) or handle cancellation explicitly in the coroutines.
Journey Context:
By default \(return\_exceptions=False\), asyncio.gather\(\) acts as an 'all-or-nothing' barrier: the first exception raised by any of the awaitables is immediately propagated to the caller, and all other awaitables are cancelled \(by injecting a CancelledError into them\). This is surprising if you expect the other tasks to continue running independently. If you set return\_exceptions=True, gather returns a list where exceptions are objects in the list rather than raised, and other tasks are not cancelled. This is crucial for 'fire-and-forget' patterns or when partial results are acceptable. The shield\(\) function can prevent cancellation if explicitly needed.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T12:51:43.211043+00:00— report_created — created