go
resource_error
ai_generated
true
goroutine leak: N goroutines blocked in chan receive (pprof shows [chan receive])
ID: go/goroutine-leak-blocked-receive
80%Fix Rate
87%Confidence
0Evidence
2024-09-03First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.0+ | active | — | — | — |
Root Cause
Workers were started to read from a channel that was never closed and never written to, so they block on receive forever.
generic中文
启动了从 channel 读取的 worker,但该 channel 从未被关闭也从未写入,导致它们永久阻塞在接收上。
Workarounds
-
96% success
go func() { defer close(ch) for _, x := range items { ch <- x } }() for v := range ch { process(v) } -
94% success
for { select { case v, ok := <-ch: if !ok { return } process(v) case <-ctx.Done(): return } }
Dead Ends
Common approaches that don't work:
-
60% fail
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% fail
Masks the leak; goroutine count still grows within the interval and can OOM under bursts.