When I do this
done := make(chan bool)
for i := 0; i < 10; i++ {
go func() {
done <- true
}()
}
<-done
instead of this
done := make(chan bool)
for i := 0; i < 10; i++ {
go func() {
done <- true
}()
}
for i := 0; i < 10; i++ {
<-done
}
Am I leaking goroutines if I do not close them and Is there a tool to detect when I forget to close goroutines?
Yes, you are leaking 9 goroutines in you first example.
I don't believe there's any tool to tell you this.
would be an interesting thing to make, if there's a way to query for all existing non-system (ie: gc) goroutines.
Probably can do something with: runtime.Stack, but it would be super-specific to a given codebase as you likely have some "good" goroutines and some "rogue" ones.
Update: Feb 4, 2016
I got curious on this, so I made a really simple (and terribly named) library to do a diff of goroutines over time. A simplistic leak detector. https://github.com/dbudworth/greak
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With