# asyncio.exceptions.CancelledError: Task was cancelled

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

## Root Cause

A coroutine that is cancelled while awaiting an operation does not handle CancelledError, causing the task to terminate abruptly and potentially leaving resources unclean.

## Version Compatibility

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

## Workarounds

1. **** (95% success)
   ```
   try:
    await long_operation()
except asyncio.CancelledError:
    # cleanup resources
    raise  # re-raise to acknowledge cancellation
   ```
2. **** (80% success)
   ```
   result = await asyncio.shield(critical_operation())
   ```

## Dead Ends

- **** — Ignoring cancellation can violate asyncio's cooperative cancellation model and lead to tasks that never truly cancel. (70% fail)
- **** — Finally runs but the exception propagates; if not caught, it may still terminate the task. (40% fail)
