diff options
| author | demo <demo@antix1> | 2026-05-25 09:37:47 -0400 |
|---|---|---|
| committer | demo <demo@antix1> | 2026-05-25 09:51:33 -0400 |
| commit | 3e303d07798caa29f59af9d5aed5328df8647670 (patch) | |
| tree | 61c05b5e3ac857b7cdd90c24dcad43312f20c643 | |
| parent | 700e47bc42b77375658b22fb5efae67a7aeeda83 (diff) | |
feat: use GOEXPERIMENT=goroutineleakprofile
That is, use it to check for goroutine leaks.
| -rw-r--r-- | main.go | 27 |
1 files changed, 26 insertions, 1 deletions
@@ -4,6 +4,8 @@ import ( "flag" "fmt" "log" + "runtime/pprof" + "strings" "sync" "time" ) @@ -24,7 +26,9 @@ func main() { } // Launch the game! - game(*numSecs, *numPlayers) + getLeakProfile(func() { + game(*numSecs, *numPlayers) + }) } func game(numSecs, numPlayers int) { @@ -95,3 +99,24 @@ func player(id int, wg *sync.WaitGroup, input chan Ball) chan Ball { return out } + +// getLeakProfile runs a leaky program snippet, extracts the goroutine +// leak profile, and writes it to stdout. +func getLeakProfile(leakySnippet func()) { + prof := pprof.Lookup("goroutineleak") + defer func() { + time.Sleep(2 * time.Second) + var content strings.Builder + + prof.WriteTo(&content, 2) + // Ignore non leaked goroutines + leaks := strings.Split(content.String(), "\n\n") + for _, leak := range leaks { + if strings.Contains(leak, "(leaked)") { + fmt.Println(leak + "\n") + } + } + }() + + leakySnippet() +} |
