go resource_error ai_generated true

runtime: 检测到 goroutine 泄漏:N 个 goroutine 阻塞在通道发送上

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

ID: go/goroutine-leak-http-server

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

版本兼容性

版本状态引入弃用备注
1.20 active
1.22 active
1.23 active

根因分析

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

English

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

解决方案

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

无效尝试

常见但无效的做法:

  1. 75% 失败

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

  2. 90% 失败

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