# 致命错误：所有协程都在休眠 - 死锁！（上下文取消未传播）

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

## 根因

父协程取消上下文，但子协程未监听上下文的 Done 通道，导致它们无限阻塞。

## 版本兼容性

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

## 解决方案

1. **Use select to listen on context.Done() in blocking operations.** (95% 成功率)
   ```
   select {
case result := <-ch:
    return result
case <-ctx.Done():
    return ctx.Err()
}
   ```
2. **Pass context to functions and check cancellation periodically.** (90% 成功率)
   ```
   func worker(ctx context.Context) error {
    for {
        select {
        case <-ctx.Done():
            return ctx.Err()
        default:
            // do work
        }
    }
}
   ```

## 无效尝试

- **Only checking context.Err() without using select on Done.** — context.Err() is only set after cancellation; without select, goroutine may not wake up. (80% 失败率)
- **Passing context by value but not using it in select.** — The context is available but not used to unblock the goroutine. (70% 失败率)
