go resource_error ai_generated true

goroutine leak: goroutine blocked on channel receive

ID: go/goroutine-leak-blocked-on-channel-receive

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.21 active — — —

Root Cause

A goroutine waits to receive from a channel that is never sent to or closed, often due to a sender exiting without closing.

generic

中文

一个 goroutine 等待从通道接收数据,但该通道从未被发送或关闭,通常是因为发送方退出时未关闭通道。

Workarounds

  1. 90% success
    Use select with context:
    select {
    case v := <-ch:
        _ = v
    case <-ctx.Done():
        return
    }
  2. 92% success
    Ensure the sender closes the channel when done. Use defer close(ch) in the sender goroutine.

Dead Ends

Common approaches that don't work:

  1. 60% fail

    time.After allocates a timer each call and can leak timers; also doesn't fix the root cause.

  2. 80% fail

    Closing from receiver can cause panic if sender later sends; violates channel ownership.