go
resource_error
ai_generated
true
goroutine 泄漏: N 个 goroutine 阻塞在 chan receive(pprof 显示 [chan receive])
goroutine leak: N goroutines blocked in chan receive (pprof shows [chan receive])
ID: go/goroutine-leak-blocked-receive
80%修复率
87%置信度
0证据数
2024-09-03首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0+ | active | — | — | — |
根因分析
启动了从 channel 读取的 worker,但该 channel 从未被关闭也从未写入,导致它们永久阻塞在接收上。
English
Workers were started to read from a channel that was never closed and never written to, so they block on receive forever.
解决方案
-
96% 成功率
go func() { defer close(ch) for _, x := range items { ch <- x } }() for v := range ch { process(v) } -
94% 成功率
for { select { case v, ok := <-ch: if !ok { return } process(v) case <-ctx.Done(): return } }
无效尝试
常见但无效的做法:
-
60% 失败
If the loop uses for v := range ch, time.After cannot be applied; converting to select changes semantics and still leaks if done incorrectly.
-
85% 失败
Masks the leak; goroutine count still grows within the interval and can OOM under bursts.