From 76921467f45c38ed441b4cec347f28f6a09d2965 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Thu, 19 Nov 2026 13:37:43 +0000 Subject: [PATCH 1/2] test: wait for goroutines to exit before the leak check TestMain counted goroutines once, right after m.Run. A goroutine that has finished its work but has not been scheduled to return yet was counted as a leak. os/exec's watchCtx goroutine is one: Cmd.Wait returns once watchCtx has sent its result, before watchCtx itself returns. Running TestAutobahn alone with GOMAXPROCS=1 failed the check in 9 of 10 runs. TestMain now counts again every 10ms for up to 5 seconds and reports a leak only if extra goroutines are still there. --- main_test.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/main_test.go b/main_test.go index 2b93bb18..43158557 100644 --- a/main_test.go +++ b/main_test.go @@ -5,6 +5,7 @@ import ( "os" "runtime" "testing" + "time" ) func goroutineStacks() []byte { @@ -20,9 +21,22 @@ func goroutineStacks() []byte { func TestMain(m *testing.M) { code := m.Run() - if runtime.GOOS != "js" && runtime.NumGoroutine() != 1 || - runtime.GOOS == "js" && runtime.NumGoroutine() != 2 { - fmt.Fprintf(os.Stderr, "goroutine leak detected, expected 1 but got %d goroutines\n", runtime.NumGoroutine()) + + want := 1 + if runtime.GOOS == "js" { + want = 2 + } + // A goroutine can still exist after the call it serves has returned, until + // the scheduler runs it to its end. The watchCtx goroutine of os/exec does + // this after Cmd.Wait returns. A leaked goroutine is still there after the + // wait. + got := runtime.NumGoroutine() + for deadline := time.Now().Add(5 * time.Second); got != want && time.Now().Before(deadline); { + time.Sleep(10 * time.Millisecond) + got = runtime.NumGoroutine() + } + if got != want { + fmt.Fprintf(os.Stderr, "goroutine leak detected, expected %d but got %d goroutines\n", want, got) fmt.Fprintf(os.Stderr, "%s\n", goroutineStacks()) os.Exit(1) } From c4ff7fcf376111f2aa057d00ff076ffa468aa3e5 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Thu, 19 Nov 2026 14:19:14 +0000 Subject: [PATCH 2/2] test: say what the leak check's wait lets through --- main_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main_test.go b/main_test.go index 43158557..29858659 100644 --- a/main_test.go +++ b/main_test.go @@ -28,8 +28,8 @@ func TestMain(m *testing.M) { } // A goroutine can still exist after the call it serves has returned, until // the scheduler runs it to its end. The watchCtx goroutine of os/exec does - // this after Cmd.Wait returns. A leaked goroutine is still there after the - // wait. + // this after Cmd.Wait returns. The wait also lets through a leaked + // goroutine that a timer ends within 5 seconds. got := runtime.NumGoroutine() for deadline := time.Now().Add(5 * time.Second); got != want && time.Now().Before(deadline); { time.Sleep(10 * time.Millisecond)