# goroutine 泄漏：runtime.NumGoroutine() 无限增长；pprof 显示 goroutine 阻塞在 chan send

- **ID:** `go/goroutine-leak-runtime-numgoroutine-grows-unbounded-ppro`
- **领域:** go
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

生产者 goroutine 因消费者提前返回（错误/超时）而永久阻塞在无缓冲 channel 的发送上，且无人再消费该 channel。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   go func() {
    defer close(out)
    for _, x := range items {
        select {
        case out <- x:
        case <-ctx.Done():
            return
        }
    }
}()
   ```
2. **** (88% 成功率)
   ```
   defer func() {
    for range out {}
}()
   ```

## 无效尝试

- **** — Delays the leak; once the buffer fills under sustained load the goroutines block again. (80% 失败率)
- **** — GC does not collect blocked goroutines; they remain referenced by the scheduler. (95% 失败率)
