diff --git a/client/tso_dispatcher.go b/client/tso_dispatcher.go index 6c644e2a6e0..1f5d6411ea6 100644 --- a/client/tso_dispatcher.go +++ b/client/tso_dispatcher.go @@ -158,6 +158,15 @@ func NewTSDeadline( cancel context.CancelFunc, ) *TSDeadline { timer := timerPool.Get().(*time.Timer) + // Stop the timer if it's not stopped. + if !timer.Stop() { + select { + case <-timer.C: // try to drain from the channel + default: + } + } + // We need be careful here, see more details in the comments of Timer.Reset. + // https://pkg.go.dev/time@master#Timer.Reset timer.Reset(timeout) return &TSDeadline{ timer: timer, @@ -419,6 +428,15 @@ tsoBatchLoop: if maxBatchWaitInterval >= 0 { tbc.adjustBestBatchSize() } + // Stop the timer if it's not stopped. + if !streamLoopTimer.Stop() { + select { + case <-streamLoopTimer.C: // try to drain from the channel + default: + } + } + // We need be careful here, see more details in the comments of Timer.Reset. + // https://pkg.go.dev/time@master#Timer.Reset streamLoopTimer.Reset(c.option.timeout) // Choose a stream to send the TSO gRPC request. streamChoosingLoop: diff --git a/pkg/utils/tsoutil/tso_dispatcher.go b/pkg/utils/tsoutil/tso_dispatcher.go index 875b1ab925f..9db0d27a3ab 100644 --- a/pkg/utils/tsoutil/tso_dispatcher.go +++ b/pkg/utils/tsoutil/tso_dispatcher.go @@ -215,6 +215,15 @@ func NewTSDeadline( cancel context.CancelFunc, ) *TSDeadline { timer := timerPool.Get().(*time.Timer) + // Stop the timer if it's not stopped. + if !timer.Stop() { + select { + case <-timer.C: // try to drain from the channel + default: + } + } + // We need be careful here, see more details in the comments of Timer.Reset. + // https://pkg.go.dev/time@master#Timer.Reset timer.Reset(timeout) return &TSDeadline{ timer: timer,