go resource_error ai_generated true

runtime: goroutine count grows unbounded (observed via runtime.NumGoroutine())

ID: go/goroutine-leak-pprof

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.22 active — — —

Root Cause

Goroutines block forever on channel send/receive or wait on a context that is never cancelled, so they never exit.

generic

中文

goroutine 永远阻塞在 channel 发送/接收上,或等待一个永不取消的 context,因此永不退出。

Workarounds

  1. 90% success
    go func(ctx context.Context) {
        select {
        case <-ctx.Done():
            return
        case work <- job:
        }
    }(ctx)
  2. 88% success
    import _ "net/http/pprof"
    // go tool pprof http://localhost:6060/debug/pprof/goroutine

Dead Ends

Common approaches that don't work:

  1. 95% fail

    GC does not kill blocked goroutines.

  2. 85% fail

    Delays OOM but goroutines still accumulate.