go resource_error ai_generated true

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

ID: go/goroutine-leak-time-tick

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.21 active — — —
1.22 active — — —

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 90% fail

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

  2. 95% fail

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