go runtime_error ai_generated true

恐慌:对具有负持续时间的计时器调用 Reset

panic: time: Reset called on timer with negative duration

ID: go/panic-timer-reset-negative-duration

其他格式: JSON · Markdown 中文 · English
85%修复率
85%置信度
1证据数
2023-06-15首次发现

版本兼容性

版本状态引入弃用备注
Go 1.15 active — — —
Go 1.16 active — — —
Go 1.17 active — — —
Go 1.18 active — — —
Go 1.19 active — — —
Go 1.20 active — — —
Go 1.21 active — — —
Go 1.22 active — — —
Go 1.23 active — — —

根因分析

使用负持续时间值调用 Timer.Reset() 会导致 Go 的 time 包中的运行时恐慌。

English

Calling Timer.Reset() with a negative duration value causes a runtime panic in Go's time package.

generic

官方文档

https://pkg.go.dev/time#Timer.Reset

解决方案

  1. Use time.Duration(math.Abs(float64(d))) to ensure non-negative duration before calling Reset()
  2. Check duration with if d < 0 { d = 0 } before timer.Reset(d)
  3. Use time.NewTimer() with a positive duration and stop it explicitly instead of resetting

无效尝试

常见但无效的做法:

  1. 80% 失败

    time.Duration() does not validate sign; negative values remain negative.

  2. 90% 失败

    time.AfterFunc() also panics on negative duration; same underlying issue.

  3. 75% 失败

    Panic recovery masks the bug but timer state is corrupted; leads to unpredictable behavior.