go resource_error ai_generated true

goroutine leak: N goroutines blocked on chan receive (detected by goleak)

ID: go/goroutine-leak-n-goroutines-blocked-on-chan-receive-detect

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-11-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.21 active — — —
1.23 active — — —

Root Cause

Goroutines block forever on a channel receive because the sender exited or the channel is never closed.

generic

中文

goroutine 永远阻塞在 channel 接收上,因为发送方已退出或 channel 从未关闭。

Workarounds

  1. 92% success
    Pass a context.Context and select on ctx.Done(): select { case v := <-ch: handle(v); case <-ctx.Done(): return }.
  2. 90% success
    Ensure the sender closes the channel when finished: defer close(ch) in the producer, so receivers unblock with the zero value.

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Buffering delays the block but does not prevent it once the buffer fills; the leak returns under load.

  2. 95% fail

    Blocked goroutines are reachable and not garbage collected; GC cannot reclaim them.