go resource_error ai_generated true

goroutine 泄漏:goroutine 永久阻塞在 <-ch 上且无发送方

goroutine leak: goroutine blocked forever on <-ch with no sender

ID: go/goroutine-leak-blocked-recv

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

版本兼容性

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

根因分析

对一个永远不会被发送或关闭的 channel 执行接收操作,使 goroutine 永久挂起。

English

A receive on a channel that will never be sent to or closed leaves the goroutine parked permanently.

generic

解决方案

  1. 94% 成功率
    select {
    case v := <-ch:
        handle(v)
    case <-ctx.Done():
        return
    }
  2. 88% 成功率
    defer close(ch)

无效尝试

常见但无效的做法:

  1. 60% 失败

    Ignoring the timeout branch means the goroutine still leaks if it loops; also time.After leaks timers.

  2. 70% 失败

    Closing from the receiver does not unblock the receive; the receive already returned zero value and panics on later sends.