go resource_error ai_generated true

goroutine 泄漏:goroutine 阻塞在通道接收

goroutine leak: goroutine blocked on channel receive

ID: go/goroutine-leak-blocked-on-channel-receive

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

版本兼容性

版本状态引入弃用备注
1.18 active — — —
1.21 active — — —

根因分析

一个 goroutine 等待从通道接收数据,但该通道从未被发送或关闭,通常是因为发送方退出时未关闭通道。

English

A goroutine waits to receive from a channel that is never sent to or closed, often due to a sender exiting without closing.

generic

解决方案

  1. 90% 成功率
    Use select with context:
    select {
    case v := <-ch:
        _ = v
    case <-ctx.Done():
        return
    }
  2. 92% 成功率
    Ensure the sender closes the channel when done. Use defer close(ch) in the sender goroutine.

无效尝试

常见但无效的做法:

  1. 60% 失败

    time.After allocates a timer each call and can leak timers; also doesn't fix the root cause.

  2. 80% 失败

    Closing from receiver can cause panic if sender later sends; violates channel ownership.