Skip to content

Commit f40c1ea

Browse files
Copilotcataggar
andcommitted
Fix ticker stop race condition by clearing period before removing timer
Co-authored-by: cataggar <[email protected]>
1 parent 256e84f commit f40c1ea

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/runtime/time_go122.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ func startTimer(tim *timer) {
5353

5454
//go:linkname stopTimer time.stopTimer
5555
func stopTimer(tim *timer) bool {
56+
// Set period to 0 to prevent the timer from being re-added to the queue
57+
// if the callback is currently running. This must be done before removing
58+
// the timer to avoid a race condition where the callback re-adds the timer
59+
// after it's been removed.
60+
tim.period = 0
5661
return removeTimer(tim) != nil
5762
}
5863

src/runtime/time_go123.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ func newTimer(when, period int64, f func(arg any, seq uintptr, delta int64), arg
5252

5353
//go:linkname stopTimer time.stopTimer
5454
func stopTimer(tim *timeTimer) bool {
55+
// Set period to 0 to prevent the timer from being re-added to the queue
56+
// if the callback is currently running. This must be done before removing
57+
// the timer to avoid a race condition where the callback re-adds the timer
58+
// after it's been removed.
59+
tim.timer.period = 0
5560
return removeTimer(&tim.timer) != nil
5661
}
5762

0 commit comments

Comments
 (0)