Skip to content

Commit

Permalink
resource control: fix unsafe usage of time.After and timer.Reset (#8929)
Browse files Browse the repository at this point in the history
close #8876

Signed-off-by: lhy1024 <[email protected]>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
lhy1024 and ti-chi-bot[bot] authored Dec 19, 2024
1 parent e00e45b commit 17f3ae3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 19 deletions.
8 changes: 4 additions & 4 deletions client/resource_group/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,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)
})
}
}
Expand Down Expand Up @@ -336,7 +336,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
}
Expand Down Expand Up @@ -372,7 +372,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
}
Expand Down Expand Up @@ -525,7 +525,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() {
Expand Down
4 changes: 3 additions & 1 deletion client/retry/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ func (bo *BackOffer) Exec(
fn func() error,
) error {
if err := fn(); err != nil {
timer := time.NewTimer(bo.nextInterval())
defer timer.Stop()
select {
case <-ctx.Done():
case <-time.After(bo.nextInterval()):
case <-timer.C:
failpoint.Inject("backOffExecute", func() {
testBackOffExecuteFlag = true
})
Expand Down
17 changes: 3 additions & 14 deletions pkg/syncer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/tikv/pd/pkg/storage"
"github.com/tikv/pd/pkg/utils/grpcutil"
"github.com/tikv/pd/pkg/utils/logutil"
"github.com/tikv/pd/pkg/utils/timerutil"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
Expand Down Expand Up @@ -142,13 +143,7 @@ func (s *RegionSyncer) StartSyncWithLeader(addr string) {
}
}
log.Error("server failed to establish sync stream with leader", zap.String("server", s.server.Name()), zap.String("leader", s.server.GetLeader().GetName()), errs.ZapError(err))
if !timer.Stop() {
select {
case <-timer.C: // try to drain from the channel
default:
}
}
timer.Reset(retryInterval)
timerutil.SafeResetTimer(timer, retryInterval)
select {
case <-ctx.Done():
log.Info("stop synchronizing with leader due to context canceled")
Expand All @@ -166,13 +161,7 @@ func (s *RegionSyncer) StartSyncWithLeader(addr string) {
if err = stream.CloseSend(); err != nil {
log.Error("failed to terminate client stream", errs.ZapError(errs.ErrGRPCCloseSend, err))
}
if !timer.Stop() {
select {
case <-timer.C: // try to drain from the channel
default:
}
}
timer.Reset(retryInterval)
timerutil.SafeResetTimer(timer, retryInterval)
select {
case <-ctx.Done():
log.Info("stop synchronizing with leader due to context canceled")
Expand Down

0 comments on commit 17f3ae3

Please sign in to comment.