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

- **ID:** `go/panic-timer-reset-negative-duration`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 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 | — | — |

## 解决方案

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
   ```

## 无效尝试

- **** — time.Duration() does not validate sign; negative values remain negative. (80% 失败率)
- **** — time.AfterFunc() also panics on negative duration; same underlying issue. (90% 失败率)
- **** — Panic recovery masks the bug but timer state is corrupted; leads to unpredictable behavior. (75% 失败率)
