-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
context: WithTimeout seems to not release the goroutines created internally #20576
Comments
https://golang.org/pkg/context/#WithTimeout
are you doing this? |
Yes, forgive me I forgot to add that line of code. Cancel function is being called. |
On every path? If possible, please post an auto-contained reproducer that can be used to assess the problem. |
The |
Again, it's hard to say without having the code and without knowing how, where and when you spawn the goroutine that cancels, and how and when it does it; but yes, that could be your issue. |
Here you go: https://play.golang.org/p/p68dFUVJhs |
As a note, seeing the Timer implementation (internally used to handle the context termination after given amount of time has passed): type Timer struct {
C <-chan Time
r runtimeTimer
} Apparently, EDIT: Weirdly enough, if I print the number of goroutines at the very beginning of the main function, it gives me 1 – so, in theory, no goroutine is leaking. |
I'm closing this until we have more information to be able to confirm that it's an issue. |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?1.8.1
What operating system and processor architecture are you using (
go env
)?What did you do?
I have a simple implementation that uses context.WithTimeout, more or less like:
When I monitor the number of currently existing goroutines (using runtime.NumGoroutines()), it seems that whenever the WithTimeout() function gets called, it internally creates a goroutine to keep track of the time and eventually cancel the context (which makes sense, if you look at the internals (https://github.com/golang/go/blob/master/src/context/context.go#L401 and https://github.com/golang/go/blob/master/src/time/sleep.go#L170).
However, when the context is finished and the case statement is reached, the goroutine is not being released. It seems that it keeps running forever and the resources are not released properly.
If I change the implementation to use a simpler version of a timeout, without the context.WithTimeout(), more or less like:
Then the number of goroutines is the expected in the first place and nothing seems to leak.
What could be the problem? What am I missing?
What did you expect to see?
Expect the goroutine created internally to be released
What did you see instead?
Seems that the goroutine created internally is never released
The text was updated successfully, but these errors were encountered: