From c1ee9cf28f6dfd2afce205d75974dc0e964075f2 Mon Sep 17 00:00:00 2001 From: lhy1024 Date: Fri, 20 Dec 2024 11:22:09 +0800 Subject: [PATCH] resource control: fix unsafe usage of time.After and timer.Reset (#8928) close tikv/pd#8876 Signed-off-by: lhy1024 --- client/resource_group/controller/controller.go | 8 ++++---- client/retry/backoff.go | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/client/resource_group/controller/controller.go b/client/resource_group/controller/controller.go index e5161ad9e3e..0bbda058b30 100755 --- a/client/resource_group/controller/controller.go +++ b/client/resource_group/controller/controller.go @@ -294,7 +294,7 @@ func (c *ResourceGroupsController) Start(ctx context.Context) { log.Warn("watch resource group meta failed", zap.Error(err)) timerutil.SafeResetTimer(watchRetryTimer, watchRetryInterval) failpoint.Inject("watchStreamError", func() { - watchRetryTimer.Reset(20 * time.Millisecond) + timerutil.SafeResetTimer(watchRetryTimer, 20*time.Millisecond) }) } } @@ -338,7 +338,7 @@ func (c *ResourceGroupsController) Start(ctx context.Context) { watchMetaChannel = nil timerutil.SafeResetTimer(watchRetryTimer, watchRetryInterval) failpoint.Inject("watchStreamError", func() { - watchRetryTimer.Reset(20 * time.Millisecond) + timerutil.SafeResetTimer(watchRetryTimer, 20*time.Millisecond) }) continue } @@ -374,7 +374,7 @@ func (c *ResourceGroupsController) Start(ctx context.Context) { watchConfigChannel = nil timerutil.SafeResetTimer(watchRetryTimer, watchRetryInterval) failpoint.Inject("watchStreamError", func() { - watchRetryTimer.Reset(20 * time.Millisecond) + timerutil.SafeResetTimer(watchRetryTimer, 20*time.Millisecond) }) continue } @@ -527,7 +527,7 @@ func (c *ResourceGroupsController) sendTokenBucketRequests(ctx context.Context, ClientUniqueId: c.clientUniqueID, } if c.ruConfig.DegradedModeWaitDuration > 0 && c.responseDeadlineCh == nil { - c.run.responseDeadline.Reset(c.ruConfig.DegradedModeWaitDuration) + timerutil.SafeResetTimer(c.run.responseDeadline, c.ruConfig.DegradedModeWaitDuration) c.responseDeadlineCh = c.run.responseDeadline.C } go func() { diff --git a/client/retry/backoff.go b/client/retry/backoff.go index 9161ad0fea1..d24e92ca0be 100644 --- a/client/retry/backoff.go +++ b/client/retry/backoff.go @@ -24,6 +24,7 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/failpoint" "github.com/pingcap/log" + "github.com/tikv/pd/client/timerutil" "go.uber.org/zap" ) @@ -86,7 +87,7 @@ func (bo *Backoffer) Exec( if after == nil { after = time.NewTimer(currentInterval) } else { - after.Reset(currentInterval) + timerutil.SafeResetTimer(after, currentInterval) } select { case <-ctx.Done():