# asyncio.exceptions.CancelledError: CancelledError not caught in gather

- **ID:** `python/asyncio-gather-exception-handling`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

When using asyncio.gather() with return_exceptions=False (default), an exception in one task cancels others, and CancelledError may propagate unexpectedly.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |

## Workarounds

1. **Use return_exceptions=True to handle exceptions gracefully** (90% success)
   ```
   results = await asyncio.gather(task1, task2, return_exceptions=True)
for r in results:
    if isinstance(r, Exception):
        # handle
   ```
2. **Catch CancelledError explicitly in each coroutine** (85% success)
   ```
   async def safe_coro():
    try:
        await something()
    except asyncio.CancelledError:
        # cleanup and re-raise if needed
        raise
   ```

## Dead Ends

- **Wrapping each task in try-except without re-raising** — If the task is cancelled externally, CancelledError may still be raised and not caught by a general except. (50% fail)
- **Using asyncio.shield() on all tasks** — shield() prevents cancellation but may cause tasks to run indefinitely if not managed. (40% fail)
