# asyncio.exceptions.CancelledError: CancelledError raised in task

- **ID:** `python/asyncio-task-cancellation-error`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

A task was cancelled externally (e.g., via task.cancel()), and the cancellation was not handled, causing it to propagate.

## Version Compatibility

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

## Workarounds

1. **Catch CancelledError and perform cleanup, then re-raise** (90% success)
   ```
   try:
    await long_running_coro()
except asyncio.CancelledError:
    # cleanup resources
    raise
   ```
2. **Use asyncio.shield() to protect critical sections** (85% success)
   ```
   await asyncio.shield(critical_coro())
   ```

## Dead Ends

- **Catching all exceptions except CancelledError** — CancelledError is a subclass of BaseException, so it may not be caught by except Exception. (70% fail)
- **Ignoring cancellation and continuing execution** — The event loop may be shutting down, and ignoring cancellation can cause resource leaks or hangs. (50% fail)
