# asyncio.exceptions.CancelledError: 

- **ID:** `python/asyncio-cancellederror-not-handled`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

A task was cancelled (timeout, shutdown, or parent cancellation) and the CancelledError propagated uncaught, often logged as an unhandled task exception.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.8+ | active | — | — |
| 3.9+ | active | — | — |
| 3.10+ | active | — | — |
| 3.11+ | active | — | — |
| 3.12+ | active | — | — |

## Workarounds

1. **** (95% success)
   ```
   try:
    await long_running()
except asyncio.CancelledError:
    await cleanup()
    raise
   ```
2. **** (90% success)
   ```
   tasks = [asyncio.create_task(w(i)) for i in range(5)]
try:
    await asyncio.gather(*tasks)
except asyncio.CancelledError:
    for t in tasks:
        t.cancel()
    raise
   ```

## Dead Ends

- **** — Swallowing cancellation breaks structured concurrency; the parent expects the task to stop. (85% fail)
- **** — Shield everywhere defeats cancellation and leaves tasks running after shutdown. (80% fail)
