go resource_error ai_generated true

fatal error: runtime: out of memory (timer leak)

ID: go/goroutine-timer-leak

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2025-12-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0 active
1.20 active

Root Cause

Creating time.Timer or time.Ticker without stopping them, causing goroutine leak and memory exhaustion.

generic

中文

创建time.Timer或time.Ticker而不停止它们,导致协程泄漏和内存耗尽。

Workarounds

  1. 95% success Always stop timers and tickers when done
    ticker := time.NewTicker(time.Second)
    defer ticker.Stop()
    for range ticker.C {
        // do work
    }
  2. 85% success Use time.After with select and timeout
    select {
    case <-ch:
    case <-time.After(5 * time.Second):
        // timeout
    }

Dead Ends

Common approaches that don't work:

  1. Using time.After repeatedly without reset 90% fail

    time.After creates new timer each call; old timers are not garbage collected until fired.

  2. Ignoring ticker stop in defer 80% fail

    Ticker continues to tick; goroutine leaks.