go resource_error ai_generated true

runtime: goroutine leak detected: N goroutines blocked on chan send

ID: go/goroutine-leak-http-server

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.20 active
1.22 active
1.23 active

Root Cause

Handler spawns goroutines writing to a channel that no one drains after client disconnects, or waits on a channel that is never closed. Goroutines accumulate over time.

generic

中文

处理程序启动的 goroutine 向无人消费的通道写入,或在永不关闭的通道上等待,客户端断开后 goroutine 持续累积。

Workarounds

  1. 92% success
    go func() {
        select {
        case ch <- result:
        case <-ctx.Done():
            return
        }
    }()
  2. 85% success
    results := make(chan Result, len(tasks))

Dead Ends

Common approaches that don't work:

  1. 75% fail

    Leaked goroutines still hold stacks and channel references; memory grows regardless of scheduler tuning.

  2. 90% fail

    GC does not collect goroutines that are still runnable or blocked on a channel.