go resource_error ai_generated true

goroutine leak: goroutine blocked on channel send

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-09-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.21 active — — —

Root Cause

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

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 70% fail

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

  2. 90% fail

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