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

When i do it

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 } 

I console goroutines if I don’t close them and is there a detection tool when I forget to close goroutines?

+5
source share
1 answer

Yes, in the first example, you will skip 9 goroutines.

I do not believe that there are any tools to tell you this.

it would be interesting to do if there is a way to query all existing non-systemic (i.e. gc) goroutines.

You can probably do something with runtime.Stack , but that would be super-specific to a specific codebase, since you probably have some β€œgood” mountains and some outcasts.

Update: February 4, 2016

I was curious, so I made a very simple (and badly named) library to make the goroutines different over time. The simplest leak detector. https://github.com/dbudworth/greak

+2
source

Source: https://habr.com/ru/post/1241455/


All Articles