go
resource_error
ai_generated
true
runtime: goroutine 数量无限增长(通过 runtime.NumGoroutine() 观测)
runtime: goroutine count grows unbounded (observed via runtime.NumGoroutine())
ID: go/goroutine-leak-pprof
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.
解决方案
-
90% 成功率
go func(ctx context.Context) { select { case <-ctx.Done(): return case work <- job: } }(ctx) -
88% 成功率
import _ "net/http/pprof" // go tool pprof http://localhost:6060/debug/pprof/goroutine
无效尝试
常见但无效的做法:
-
95% 失败
GC does not kill blocked goroutines.
-
85% 失败
Delays OOM but goroutines still accumulate.