From f40c1eab921e75a14767693b6f7114cb08bbaf8b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 3 Oct 2025 17:49:53 +0000 Subject: [PATCH] Fix ticker stop race condition by clearing period before removing timer Co-authored-by: cataggar <87583576+cataggar@users.noreply.github.com> --- src/runtime/time_go122.go | 5 +++++ src/runtime/time_go123.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/runtime/time_go122.go b/src/runtime/time_go122.go index 2693555cf5..57481a6e71 100644 --- a/src/runtime/time_go122.go +++ b/src/runtime/time_go122.go @@ -53,6 +53,11 @@ func startTimer(tim *timer) { //go:linkname stopTimer time.stopTimer func stopTimer(tim *timer) bool { + // Set period to 0 to prevent the timer from being re-added to the queue + // if the callback is currently running. This must be done before removing + // the timer to avoid a race condition where the callback re-adds the timer + // after it's been removed. + tim.period = 0 return removeTimer(tim) != nil } diff --git a/src/runtime/time_go123.go b/src/runtime/time_go123.go index e39da74488..9ea36e6487 100644 --- a/src/runtime/time_go123.go +++ b/src/runtime/time_go123.go @@ -52,6 +52,11 @@ func newTimer(when, period int64, f func(arg any, seq uintptr, delta int64), arg //go:linkname stopTimer time.stopTimer func stopTimer(tim *timeTimer) bool { + // Set period to 0 to prevent the timer from being re-added to the queue + // if the callback is currently running. This must be done before removing + // the timer to avoid a race condition where the callback re-adds the timer + // after it's been removed. + tim.timer.period = 0 return removeTimer(&tim.timer) != nil }