go resource_error ai_generated true

goroutine leak: goroutine blocked forever on <-ch with no sender

ID: go/goroutine-leak-blocked-recv

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-07-03First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0+ active

Root Cause

A receive on a channel that will never be sent to or closed leaves the goroutine parked permanently.

generic

中文

对一个永远不会被发送或关闭的 channel 执行接收操作,使 goroutine 永久挂起。

Workarounds

  1. 94% success
    select {
    case v := <-ch:
        handle(v)
    case <-ctx.Done():
        return
    }
  2. 88% success
    defer close(ch)

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Ignoring the timeout branch means the goroutine still leaks if it loops; also time.After leaks timers.

  2. 70% fail

    Closing from the receiver does not unblock the receive; the receive already returned zero value and panics on later sends.