go
resource_error
ai_generated
true
警告:检测到通道泄漏(chan int,goroutine 3)
warning: channel leak detected (chan int, goroutine 3)
ID: go/channel-leak-without-close
80%修复率
80%置信度
0证据数
2024-08-02首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.21 | active | — | — | — |
根因分析
通道从未关闭,且没有 goroutine 发送或接收,导致其永远被持有。
English
A channel is never closed and no goroutine is sending or receiving, causing it to be held forever.
解决方案
-
90% 成功率
ch := make(chan int) // Ensure it's closed eventually defer close(ch) // or use a worker that exits
-
85% 成功率
ctx, cancel := context.WithCancel(context.Background()) ch := make(chan int) go func() { select { case <-ctx.Done(): close(ch) case v := <-ch: // process } }() cancel()
无效尝试
常见但无效的做法:
-
100% 失败
Channels are not garbage collected if referenced; GC doesn't force close.
-
90% 失败
Nil channel blocks forever; doesn't release resources.
-
80% 失败
Leaks accumulate, leading to memory exhaustion.