Fix incorrect usage of Timer.Reset, honour Context deadlines, avoid leaking timers#1
Open
Fix incorrect usage of Timer.Reset, honour Context deadlines, avoid leaking timers#1
Conversation
There are a few places in the codebase where `Timer.Reset` is being called right before attempting to receive from the
`Timer.C` channel. If the timer has already fired, this can lead to a receive from `Timer.C` returning earlier than
the duration supplied to `Timer.Reset` as `Reset` does not drain the `Timer.C` channel.
Here's an example to demonstrate this:
```golang
timer := time.NewTimer(100 * time.Millisecond)
time.Sleep(200 * time.Millisecond)
timer.Reset(24 * time.Hour)
select {
case <-time.After(1 * time.Millisecond):
case <-timer.C:
panic("fired before 24 hours")
}
```
Additionally, it's not advisable to call `Timer.Reset` while it's still active on a `Timer` obtained from a call to `time.NewTimer`,
as mentioned in the [docs](https://pkg.go.dev/time#Timer.Reset). The correct usage is to call `Reset` only on timers
that are stopped or have had their channels drained.
This changelist fixes the issue by creating a new `Timer` before waiting on `Timer.C` and `Stop`ping it before the function returns.
Some parts of the codebase do not call `Timer.Stop`, leaking the underlying `Timer` if it has not already fired.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
TODO: open this PR in the upstream repo after reviewing the contribution guidelines.
I came across this while going through the codebase.
This changelist includes three changes:
Timer.Reset.http.Request.There are a few places in the codebase where
Timer.Resetis being called right before attempting to receive from theTimer.Cchannel. If the timer has already fired, this can lead to a receive fromTimer.Creturning earlier thanthe duration supplied to
Timer.ResetasResetdoes not drain theTimer.Cchannel.Here's an example to demonstrate this:
It's not advisable to call
Timer.Resetwhile it's still active on aTimerobtained from a call totime.NewTimer,as mentioned in the docs. The correct usage is to call
Resetonly on timersthat are stopped or have had their channels drained.
This changelist fixes the issue by creating a new
Timerbefore waiting onTimer.C, andStopping it before the function returns.Why?
Checklist
make lint) and all checks pass.make tests) and all tests pass.Related PR(s)/Issue(s)