go resource_error ai_generated true

goroutine 泄漏:goroutine 阻塞在通道发送

goroutine leak: goroutine blocked on channel send

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

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

版本兼容性

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

根因分析

一个 goroutine 向无人接收的通道发送数据,通常是因为接收方提前退出,或通道无缓冲且没有活跃的接收者。

English

A goroutine sends to a channel that no one receives, often because the receiver exited early or the channel is unbuffered and no reader is active.

generic

解决方案

  1. 92% 成功率
    Use select with context cancellation:
    select {
    case ch <- val:
    case <-ctx.Done():
        return
    }
  2. 88% 成功率
    Ensure the receiver always drains the channel or closes it to unblock senders. Use a done channel to signal shutdown.

无效尝试

常见但无效的做法:

  1. 70% 失败

    Only delays the leak; if buffer fills, goroutine still blocks.

  2. 90% 失败

    Leaks accumulate over time, causing memory growth and eventual OOM.