go
resource_error
ai_generated
true
goroutine 泄漏:N 个 goroutine 阻塞在 chan receive(由 goleak 检测)
goroutine leak: N goroutines blocked on chan receive (detected by goleak)
ID: go/goroutine-leak-n-goroutines-blocked-on-chan-receive-detect
80%修复率
87%置信度
0证据数
2024-11-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.21 | active | — | — | — |
| 1.23 | active | — | — | — |
根因分析
goroutine 永远阻塞在 channel 接收上,因为发送方已退出或 channel 从未关闭。
English
Goroutines block forever on a channel receive because the sender exited or the channel is never closed.
解决方案
-
92% 成功率
Pass a context.Context and select on ctx.Done(): select { case v := <-ch: handle(v); case <-ctx.Done(): return }. -
90% 成功率
Ensure the sender closes the channel when finished: defer close(ch) in the producer, so receivers unblock with the zero value.
无效尝试
常见但无效的做法:
-
80% 失败
Buffering delays the block but does not prevent it once the buffer fills; the leak returns under load.
-
95% 失败
Blocked goroutines are reachable and not garbage collected; GC cannot reclaim them.