go resource_error ai_generated true

错误:运行时检测到 goroutine 泄漏:time.Tick 从未停止

error: runtime: goroutine leak detected: time.Tick never stopped

ID: go/goroutine-leak-time-tick

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

版本兼容性

版本状态引入弃用备注
1.18 active — — —
1.21 active — — —
1.22 active — — —

根因分析

time.Tick 返回的通道永远不会被垃圾回收;每次调用都会为进程生命周期泄漏一个 goroutine。

English

time.Tick returns a channel that is never garbage collected; each call leaks a goroutine for the process lifetime.

generic

解决方案

  1. 95% 成功率
    t := time.NewTicker(time.Second)
    defer t.Stop()
    for range t.C { /* ... */ }
  2. 90% 成功率
    timer := time.AfterFunc(time.Second, func() { /* ... */ })
    defer timer.Stop()

无效尝试

常见但无效的做法:

  1. 90% 失败

    Each new ticker adds a goroutine; long-running services leak unboundedly.

  2. 95% 失败

    time.Tick's underlying ticker is intentionally retained by the runtime.