# asyncio.exceptions.CancelledError: 

- **ID:** `python/asyncio-cancellederror-not-caught`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

任务被取消（通过 task.cancel() 或超时），而协程未传播或处理 CancelledError；在 Python 3.8+ 中它继承自 BaseException 而非 Exception。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |
| 3.10 | active | — | — |
| 3.11 | active | — | — |
| 3.12 | active | — | — |

## 解决方案

1. **** (92% 成功率)
   ```
   try:
    await work()
except asyncio.CancelledError:
    await cleanup()
    raise  # always re-raise
   ```
2. **** (85% 成功率)
   ```
   try:
    await asyncio.shield(flush_buffer())
except asyncio.CancelledError:
    raise
   ```

## 无效尝试

- **** — CancelledError is a BaseException in 3.8+, so except Exception does not catch it. (90% 失败率)
- **** — Prevents proper cancellation propagation, causing hangs on shutdown and resource leaks. (80% 失败率)
