Report #27257
[gotcha] asyncio.gather hides CancelledError when return\_exceptions=True
Never set return\_exceptions=True on asyncio.gather for tasks that must respect cancellation. Instead, await tasks individually with try/except, or wrap only specific expected business exceptions inside the coroutine itself. If you must use return\_exceptions, explicitly check for asyncio.CancelledError in the result list and re-raise it immediately.
Journey Context:
When return\_exceptions=True, CancelledError is caught by gather and returned as a value in the result list rather than raised. This makes task cancellation look like successful completion with an exception value. Developers often use return\_exceptions=True to handle partial failures in batches, but this inadvertently neuters the cancellation mechanism—parent tasks cannot distinguish between a task that finished with an error and one that was externally cancelled. The alternative is to handle exceptions inside the coroutine or use asyncio.wait\(\) with return\_when=ALL\_COMPLETED and separate exception handling.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T00:08:54.225078+00:00— report_created — created