Skip to content

Commit

Permalink
Fix race in timer stoppage (#3281)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Aug 8, 2024
1 parent ab504e9 commit 9e67efe
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions network/throttling/inbound_resource_throttler.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,28 +162,21 @@ func (t *systemThrottler) Acquire(ctx context.Context, nodeID ids.NodeID) {
waitDuration = t.MaxRecheckDelay
}

// Reset [timer].
if timer == nil {
// Note this is called at most once.
t.metrics.awaitingAcquire.Inc()

timer = t.timerPool.Get().(*time.Timer)
defer func() {
// Satisfy [t.timerPool] invariant.
if !timer.Stop() {
// The default ensures we don't wait forever in the case
// that the channel was already drained.
select {
case <-timer.C:
default:
}
}
t.timerPool.Put(timer)
}()
defer t.timerPool.Put(timer)
}

timer.Reset(waitDuration)
select {
case <-ctx.Done():
// Satisfy [t.timerPool] invariant.
if !timer.Stop() {
<-timer.C
}
return
case <-timer.C:
}
Expand Down

0 comments on commit 9e67efe

Please sign in to comment.