# fatal error: 所有 goroutine 都休眠 - 死锁！（阻塞在 chan receive）

- **ID:** `go/fatal-error-all-goroutines-are-asleep-deadlock-blocked-`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

main 从某个 channel 接收，但没有任何 goroutine 会向其发送，例如生产者 goroutine 因错误提前退出，或使用了 nil channel。

## 版本兼容性

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

## 解决方案

1. **** (94% 成功率)
   ```
   ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
select {
case v := <-ch:
    use(v)
case <-ctx.Done():
    return ctx.Err()
}
   ```
2. **** (92% 成功率)
   ```
   go func() {
    defer close(ch)
    for _, x := range items {
        ch <- x
    }
}()
for v := range ch { use(v) }
   ```

## 无效尝试

- **** — Buffer only helps if a sender exists; with no sender, receive still blocks forever. (85% 失败率)
- **** — An empty select blocks forever too, converting deadlock into a silent hang. (90% 失败率)
