go resource_error ai_generated true

goroutine leak: N goroutines blocked in chan send (runtime.gopark)

ID: go/goroutine-leak-blocked-channel

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.7+ active

Root Cause

Goroutines send to an unbuffered channel whose receiver has exited or never runs, so they block forever. Memory and goroutine count grow unbounded over time.

generic

中文

goroutine 向无缓冲通道发送数据,但接收方已退出或从未运行,导致永久阻塞。内存和 goroutine 数量随时间无限增长。

Workarounds

  1. 95% success
    select {
    case out <- v:
    case <-ctx.Done():
        return ctx.Err()
    }
  2. 82% success
    ch := make(chan int, len(producers))
    // producers can always send without blocking

Dead Ends

Common approaches that don't work:

  1. 95% fail

    GOMAXPROCS controls scheduler parallelism, not goroutine blocking; blocked goroutines remain blocked.

  2. 90% fail

    Blocked goroutines are reachable from the scheduler and are not collected; GC cannot free them.