go
resource_error
ai_generated
true
warning: channel leak detected (chan int, goroutine 3)
ID: go/channel-leak-without-close
80%Fix Rate
80%Confidence
0Evidence
2024-08-02First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 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
-
90% success
ch := make(chan int) // Ensure it's closed eventually defer close(ch) // or use a worker that exits
-
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:
-
100% fail
Channels are not garbage collected if referenced; GC doesn't force close.
-
90% fail
Nil channel blocks forever; doesn't release resources.
-
80% fail
Leaks accumulate, leading to memory exhaustion.