# 致命错误：所有协程都处于睡眠状态 - 死锁！（上下文已取消）

- **ID:** `go/goroutine-context-cancel-not-checked`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

协程在上下文被取消后阻塞在通道操作上，没有检查取消状态。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.7 | active | — | — |
| 1.21 | active | — | — |

## 解决方案

1. **Always include ctx.Done() in select** (95% 成功率)
   ```
   select {
case <-ch:
case <-ctx.Done():
    return ctx.Err()
}
   ```
2. **Use context.WithTimeout to automatically cancel** (90% 成功率)
   ```
   ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
select {
case <-ch:
case <-ctx.Done():
    // timeout
}
   ```

## 无效尝试

- **Ignoring context in select** — Context cancellation doesn't automatically unblock other channels. (90% 失败率)
- **Using time.Sleep to wait for cancellation** — Sleep doesn't handle cancellation; goroutine still blocks. (80% 失败率)
