go runtime_error ai_generated true

panic: time: Reset called on timer that has already fired or been stopped

ID: go/goroutine-timer-reset-after-expiry

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2024-12-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active
1.19 active
1.20 active
1.21 active
1.22 active

Root Cause

Calling Reset() on a time.Timer that has already expired or been stopped, which is not allowed in Go 1.18+ without proper draining.

generic

中文

对已过期或已停止的time.Timer调用Reset(),在Go 1.18+中不允许,除非正确排空通道。

Workarounds

  1. 95% success Drain timer channel before Reset
    if !t.Stop() { <-t.C }; t.Reset(duration)
  2. 90% success Use time.NewTimer and avoid Reset
    t := time.NewTimer(duration); // use once and recreate if needed

Dead Ends

Common approaches that don't work:

  1. Using recover() to ignore panic 90% fail

    Does not fix timer state issue.

  2. Calling Stop() before Reset() without draining 85% fail

    Stop() returns false if already fired, but Reset still panics if channel not drained.