From 3e303d07798caa29f59af9d5aed5328df8647670 Mon Sep 17 00:00:00 2001 From: demo Date: Mon, 25 May 2026 09:37:47 -0400 Subject: feat: use GOEXPERIMENT=goroutineleakprofile That is, use it to check for goroutine leaks. --- main.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'main.go') diff --git a/main.go b/main.go index 686981b..947e9a0 100644 --- a/main.go +++ b/main.go @@ -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() +} -- cgit v1.2.3