Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a tool to detect when I forget to close goroutines?

Tags:

go

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?

like image 806
Gert Cuykens Avatar asked Oct 19 '22 17:10

Gert Cuykens


1 Answers

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

like image 96
David Budworth Avatar answered Oct 22 '22 03:10

David Budworth