Commit 2e937a979c for golang
commit 2e937a979c74d066334bb51c43c6adb43b9346a4
Author: Harrison Crosse <harrison@crosse.dev>
Date: Tue Dec 1 23:41:29 2026 +0000
runtime: count leaked goroutines as durably blocked in synctest bubbles
(*synctestBubble).changegstatus keeps bubble.running, the number of
goroutines that are not durably blocked, by classifying the old and new
status of each goroutine. It only treated _Gwaiting as a status in
which a goroutine can be durably blocked. Every other live status
counted as running.
When the GC builds a goroutine leak profile, it moves goroutines that
are blocked on unreachable concurrency primitives from _Gwaiting to
_Gleaked with casgstatus. For a goroutine in a bubble that was
durably blocked, changegstatus saw a durably blocked goroutine start
running and incremented bubble.running. At the start of the next GC
cycle, allGsSnapshotSortedForGC moves the goroutine back to _Gwaiting
with a plain compare-and-swap, which does not notify the bubble, so the
count was never corrected.
With bubble.running too high, the bubble never became idle again. Fake
time stopped advancing, and a bubble whose main goroutine had exited
neither made progress nor reported its deadlock.
Treat _Gleaked like _Gwaiting in both switches. A leaked goroutine
keeps the wait reason it had, so the transitions into and out of
_Gleaked no longer change the bubble's idle accounting, and neither
does the unnotified reset.
Add a test that takes the goroutineleak profile inside a bubble and
checks that the bubble still advances fake time and reports its
deadlock.
Fixes #83065
Change-Id: Ib6fac013adc589d29b2f8c5441b7bf77c9606ea0
GitHub-Last-Rev: 07c17e4f6e5a4ce4dd1c28aad3837cba0af23d25
GitHub-Pull-Request: golang/go#83072
Reviewed-on: https://go-review.googlesource.com/c/go/+/862904
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
diff --git a/src/internal/synctest/synctest_test.go b/src/internal/synctest/synctest_test.go
index 6b638aecbc..f09761e3e6 100644
--- a/src/internal/synctest/synctest_test.go
+++ b/src/internal/synctest/synctest_test.go
@@ -9,10 +9,12 @@ import (
"fmt"
"internal/synctest"
"internal/testenv"
+ "io"
"iter"
"os"
"reflect"
"runtime"
+ "runtime/pprof"
"slices"
"strconv"
"strings"
@@ -574,6 +576,43 @@ func TestDeadlockChild(t *testing.T) {
})
}
+// TestDeadlockAfterGoroutineLeakProfile checks that a goroutine which the
+// goroutineleak profile has marked as leaked still counts as durably blocked.
+// Otherwise the bubble never becomes idle, time stops advancing, and the
+// bubble hangs instead of reporting its deadlock.
+func TestDeadlockAfterGoroutineLeakProfile(t *testing.T) {
+ const want = "deadlock: main bubble goroutine has exited but blocked goroutines remain"
+ if os.Getenv("GO_WANT_HELPER_PROCESS") == "1" {
+ synctest.Run(func() {
+ go func() {
+ var ch chan int
+ <-ch
+ }()
+ synctest.Wait()
+ if err := pprof.Lookup("goroutineleak").WriteTo(io.Discard, 1); err != nil {
+ t.Error(err)
+ }
+ time.Sleep(time.Second)
+ fmt.Println("slept in bubble")
+ })
+ return
+ }
+
+ cmd := testenv.Command(t, testenv.Executable(t), "-test.run=^"+t.Name()+"$")
+ cmd = testenv.CleanCmdEnv(cmd)
+ cmd.Env = append(cmd.Env, "GO_WANT_HELPER_PROCESS=1")
+ out, err := cmd.CombinedOutput()
+ if err == nil {
+ t.Errorf("expected test function to panic, but test returned successfully")
+ }
+ if !strings.Contains(string(out), "slept in bubble") {
+ t.Errorf("fake time did not advance in the bubble; output:\n%s", out)
+ }
+ if !strings.Contains(string(out), want) {
+ t.Errorf("wanted test output containing %q; got %q", want, out)
+ }
+}
+
func TestDeadlockTicker(t *testing.T) {
defer wantPanic(t, "deadlock: main bubble goroutine has exited but blocked goroutines remain")
synctest.Run(func() {
diff --git a/src/runtime/synctest.go b/src/runtime/synctest.go
index e49c2d6b5a..6f41db4279 100644
--- a/src/runtime/synctest.go
+++ b/src/runtime/synctest.go
@@ -55,7 +55,7 @@ func (bubble *synctestBubble) changegstatus(gp *g, oldval, newval uint32) {
case _Gdead, _Gdeadextra:
wasRunning = false
totalDelta++
- case _Gwaiting:
+ case _Gwaiting, _Gleaked:
if gp.waitreason.isIdleInSynctest() {
wasRunning = false
}
@@ -68,7 +68,7 @@ func (bubble *synctestBubble) changegstatus(gp *g, oldval, newval uint32) {
if gp == bubble.main {
bubble.done = true
}
- case _Gwaiting:
+ case _Gwaiting, _Gleaked:
if gp.waitreason.isIdleInSynctest() {
isRunning = false
}