Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race in timer stoppage #3281

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions network/throttling/inbound_resource_throttler.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,28 +164,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
Loading