Files
Ron Evans e569dcfe38 runtime: fix ticker not stopping when Stop races with its callback (#5487)
* runtime: fix ticker not stopping when Stop races with its callback

On the threads and cores schedulers, timer callbacks run concurrently
with user goroutines.

If Stop (or Reset) was called in the window after the node was popped
but before its callback re-added it, removeTimer would not find the
timer in the queue, so it would be re-added to timer anyway.

Track timers whose callback is currently running in a firing list, and
have removeTimer mark a firing timer as stopped so its callback does not
re-add it.

Also make the timers test drain robust: allow a possible in-flight tick
delivered concurrently with Stop to settle before draining the channel.

Signed-off-by: deadprogram <ron@hybridgroup.com>

* runtime: fix Stop/Reset semantics when racing a firing timer callback

Address review feedback on the ticker Stop-race fix. On the threads and
cores schedulers a timer callback runs concurrently with user goroutines,
which left several problems:

- removeTimer reported a firing timer as successfully removed, so
  Stop/Reset could return true even though the callback had already
  started (wrong semantics, notably for AfterFunc). firingTimerStop now
  returns a bool and removeTimer no longer hands the still-firing node
  back to resetTimer.

- The periodic advance (when += period) ran in timerCallback outside the
  scheduler timer lock. Move it into each scheduler's reAddTimer, under
  the lock and after the stopped check, so a concurrent Reset can't have
  its freshly-queued deadline corrupted.

- resetTimer now sets when/period after removeTimer for the same reason.

Add testdata/timer_stop_reset_race.go and TestTimerStopResetRace, which
reproduce the stop-while-firing and reset-while-firing races via the
runtime timer linkname hooks.

Signed-off-by: deadprogram <ron@hybridgroup.com>

* runtime: fix timer Stop/Reset race with firing periodic timers

Signed-off-by: deadprogram <ron@hybridgroup.com>

---------

Signed-off-by: deadprogram <ron@hybridgroup.com>
2026-07-06 20:14:53 +02:00

66 lines
1.7 KiB
Go

package main
import "time"
var timer = time.NewTimer(time.Millisecond)
func main() {
// Test ticker.
ticker := time.NewTicker(time.Millisecond * 500)
println("waiting on ticker")
go func() {
time.Sleep(time.Millisecond * 150)
println(" - after 150ms")
time.Sleep(time.Millisecond * 200)
println(" - after 200ms")
time.Sleep(time.Millisecond * 300)
println(" - after 300ms")
}()
<-ticker.C
println("waited on ticker at 500ms")
<-ticker.C
println("waited on ticker at 1000ms")
ticker.Stop()
// Ticker.Stop does not drain already-buffered ticks from the channel, and
// on a slow CI runner a final tick may be delivered concurrently right as
// Stop is called. Give any such in-flight tick a chance to arrive, then
// drain the channel before checking that no further ticks are sent.
time.Sleep(time.Millisecond * 100)
select {
case <-ticker.C:
default:
}
time.Sleep(time.Millisecond * 750)
select {
case <-ticker.C:
println("fail: ticker should have stopped!")
default:
println("ticker was stopped (didn't send anything after 750ms)")
}
timer := time.NewTimer(time.Millisecond * 750)
println("waiting on timer")
go func() {
time.Sleep(time.Millisecond * 200)
println(" - after 200ms")
time.Sleep(time.Millisecond * 400)
println(" - after 400ms")
}()
<-timer.C
println("waited on timer at 750ms")
time.Sleep(time.Millisecond * 500)
reset := timer.Reset(time.Millisecond * 750)
println("timer reset:", reset)
println("waiting on timer")
go func() {
time.Sleep(time.Millisecond * 200)
println(" - after 200ms")
time.Sleep(time.Millisecond * 400)
println(" - after 400ms")
}()
<-timer.C
println("waited on timer at 750ms")
time.Sleep(time.Millisecond * 500)
}