Skip to content
Sarj Code Standards

no-gather-first-exception-reraise

Raise every gathered failure instead of only the first.

Why

asyncio.gather(..., return_exceptions=True) waits for every operation and returns their failures as values. Scanning the results and raising the first exception discards every later failure, so concurrent cleanup or fan-out can lose the errors that explain what is still broken.

Fix

Use asyncio.TaskGroup when the first failure should cancel the siblings. When every operation must finish, as in cleanup, collect all failures and raise BaseExceptionGroup or ExceptionGroup so none is lost.

Examples

Before — flagged Only the first cleanup failure is raised
storage/cleanup.py
import asyncio
async def delete_all(paths: list[str]) -> None:
results = await asyncio.gather(
*(delete(path) for path in paths), return_exceptions=True
)
for result in results:
if isinstance(result, BaseException):
raise result
After — preferred Every cleanup failure is raised together
storage/cleanup.py
import asyncio
async def delete_all(paths: list[str]) -> None:
results = await asyncio.gather(
*(delete(path) for path in paths), return_exceptions=True
)
failures = [result for result in results if isinstance(result, Exception)]
if failures:
raise ExceptionGroup("cleanup failed", failures)