go resource_error ai_generated true

goroutine 泄漏:N 个 goroutine 阻塞在通道发送 (runtime.gopark)

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

ID: go/goroutine-leak-blocked-channel

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

版本兼容性

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

根因分析

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

English

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

解决方案

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

无效尝试

常见但无效的做法:

  1. 95% 失败

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

  2. 90% 失败

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