go runtime_error ai_generated true

恐慌:时间:对已触发或已停止的计时器调用Reset

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

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

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

版本兼容性

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

根因分析

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

English

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

解决方案

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

无效尝试

常见但无效的做法:

  1. Using recover() to ignore panic 90% 失败

    Does not fix timer state issue.

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

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