go resource_error ai_generated true

warning: channel leak detected (chan int, goroutine 3)

ID: go/channel-leak-without-close

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.21 active

Root Cause

A channel is never closed and no goroutine is sending or receiving, causing it to be held forever.

generic

中文

通道从未关闭,且没有 goroutine 发送或接收,导致其永远被持有。

Workarounds

  1. 90% success
    ch := make(chan int)
    // Ensure it's closed eventually
    defer close(ch)
    // or use a worker that exits
  2. 85% success
    ctx, cancel := context.WithCancel(context.Background())
    ch := make(chan int)
    go func() {
        select {
        case <-ctx.Done():
            close(ch)
        case v := <-ch:
            // process
        }
    }()
    cancel()

Dead Ends

Common approaches that don't work:

  1. 100% fail

    Channels are not garbage collected if referenced; GC doesn't force close.

  2. 90% fail

    Nil channel blocks forever; doesn't release resources.

  3. 80% fail

    Leaks accumulate, leading to memory exhaustion.