go resource_error ai_generated true

runtime: goroutine 数量无限增长(通过 runtime.NumGoroutine() 观测)

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

ID: go/goroutine-leak-pprof

其他格式: JSON · Markdown 中文 · English
80%修复率
87%置信度
0证据数
2024-06-11首次发现

版本兼容性

版本状态引入弃用备注
1.18 active — — —
1.22 active — — —

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 95% 失败

    GC does not kill blocked goroutines.

  2. 85% 失败

    Delays OOM but goroutines still accumulate.