Report #16297
[gotcha] asyncio.gather\(\) discards completed results on cancellation
Use \`return\_exceptions=True\` and manually check for \`asyncio.CancelledError\` in results, or process completed tasks immediately before allowing cancellation to propagate rather than relying on gather's return value after cancellation.
Journey Context:
When \`gather\(\)\` receives a cancellation signal, it cancels all pending tasks and raises \`CancelledError\` immediately, losing any results from tasks that had already completed. Developers often assume they can catch CancelledError and inspect the gather result for partial completion, but the exception bypasses the return. The alternative is to use \`return\_exceptions=True\`, which causes gather to return a list of results or exceptions \(including CancelledError instances\) instead of raising, allowing you to distinguish between 'task raised CancelledError' and 'gather itself was cancelled'. This pattern is essential for graceful degradation in distributed systems where partial results are valuable.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T02:20:21.361339+00:00— report_created — created