# 错误: 上下文截止时间已过（取消后 goroutine 阻塞在 <-ch）

- **ID:** `go/goroutine-leak-on-ctx-cancel`
- **领域:** go
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

goroutine 等待一个仅在成功时写入的 channel。ctx 取消后发送者提前返回从不发送，接收者永远阻塞。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   Select on ctx.Done() alongside the channel:

select {
case v := <-ch:
    return v, nil
case <-ctx.Done():
    return nil, ctx.Err()
}
   ```
2. **** (90% 成功率)
   ```
   Ensure producers always send or close on exit:

defer close(ch)
for _, item := range items {
    select {
    case ch <- item:
    case <-ctx.Done():
        return
    }
}
   ```

## 无效尝试

- **** — Delays the leak; cancel still leaves the receiver blocked. (70% 失败率)
- **** — default makes the loop spin hot and still does not deliver the value. (65% 失败率)
