go resource_error ai_generated true

goroutine leak: runtime.NumGoroutine() grows unbounded; pprof shows goroutines blocked in chan send

ID: go/goroutine-leak-runtime-numgoroutine-grows-unbounded-ppro

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-08-27First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.19 active — — —
1.21 active — — —
1.23 active — — —

Root Cause

Producer goroutines block forever on an unbuffered channel because the consumer returned early (error/timeout) and nobody drains the channel.

generic

中文

生产者 goroutine 因消费者提前返回(错误/超时)而永久阻塞在无缓冲 channel 的发送上,且无人再消费该 channel。

Workarounds

  1. 95% success
    go func() {
        defer close(out)
        for _, x := range items {
            select {
            case out <- x:
            case <-ctx.Done():
                return
            }
        }
    }()
  2. 88% success
    defer func() {
        for range out {}
    }()

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Delays the leak; once the buffer fills under sustained load the goroutines block again.

  2. 95% fail

    GC does not collect blocked goroutines; they remain referenced by the scheduler.