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

Prevent leaks from time.After #11982

Closed
shoenig opened this issue Feb 2, 2022 · 1 comment · Fixed by #11983
Closed

Prevent leaks from time.After #11982

shoenig opened this issue Feb 2, 2022 · 1 comment · Fixed by #11983
Assignees
Milestone

Comments

@shoenig
Copy link
Member

shoenig commented Feb 2, 2022

The use of time.After in a way that leaks goroutines is a common pitfall.

https://pkg.go.dev/time#After

The underlying Timer is not recovered by the garbage collector until the timer fires. If efficiency is a concern, use NewTimer instead and call Timer.Stop if the timer is no longer needed.

This is problematic in code structured something like,

for {
  select {
    case <- time.After(dur):
    case <- ch:
  }
}

which is a pattern we use in Nomad in quite a few places. If ch sends quickly, there can be a large number of allocated underlying channels from the call to After, which will hang around forever because there is nothing to read the returned value. A recent raft PR describes some of the performance impact of the acumulated allocations in more detail.

@shoenig shoenig self-assigned this Feb 2, 2022
@shoenig shoenig added this to the 1.3.0 milestone Feb 2, 2022
@lgfa29 lgfa29 added backport/1.1.x backport to 1.1.x release line backport/1.2.x backport to 1.1.x release line and removed backport/1.1.x backport to 1.1.x release line backport/1.2.x backport to 1.1.x release line labels Apr 19, 2022
@github-actions
Copy link

github-actions bot commented Oct 9, 2022

I'm going to lock this issue because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants