go resource_error ai_generated true

goroutine 泄漏: N 个 goroutine 阻塞在 chan send(通过 pprof 检测)

goroutine leak: N goroutines blocked in chan send (detected via pprof)

ID: go/goroutine-leak-blocked-send

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

版本兼容性

版本状态引入弃用备注
1.0+ active

根因分析

启动的 goroutine 向无缓冲或已满的 channel 发送数据,但接收方已停止读取,导致它们永久阻塞并不断累积。

English

Goroutines were spawned to send on an unbuffered or full channel whose receiver stopped reading, so they block forever and accumulate.

generic

解决方案

  1. 95% 成功率
    select {
    case ch <- v:
    case <-ctx.Done():
        return
    }
  2. 90% 成功率
    jobs := make(chan Job, 128)
    for i := 0; i < runtime.NumCPU(); i++ {
        go func() { for j := range jobs { process(j) } }()
    }

无效尝试

常见但无效的做法:

  1. 95% 失败

    Blocked goroutines are not CPU-bound; more CPUs do not unblock them and memory keeps growing.

  2. 90% 失败

    GC does not collect goroutines blocked on channels; they remain reachable from the scheduler.