summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordemo <demo@antix1>2026-05-25 09:37:47 -0400
committerdemo <demo@antix1>2026-05-25 09:51:33 -0400
commit3e303d07798caa29f59af9d5aed5328df8647670 (patch)
tree61c05b5e3ac857b7cdd90c24dcad43312f20c643
parent700e47bc42b77375658b22fb5efae67a7aeeda83 (diff)
feat: use GOEXPERIMENT=goroutineleakprofile
That is, use it to check for goroutine leaks.
-rw-r--r--main.go27
1 files changed, 26 insertions, 1 deletions
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()
+}