aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Anthony Knyszek <mknyszek@google.com>2024-10-23 16:28:52 +0000
committerMichael Knyszek <mknyszek@google.com>2024-10-24 17:09:10 +0000
commit2a98a1849f059ffa94ab23a1ab7d8fa0fd0b48dd (patch)
tree073fc15eb15188766d8a5885fcfc50590df84cf2 /src
parentfb2a7f8ce1b1abf1195ba61e40286985f1189fa8 (diff)
downloadgo-2a98a1849f059ffa94ab23a1ab7d8fa0fd0b48dd.tar.xz
runtime: uphold goroutine profile invariants in coroswitch
Goroutine profiles require checking in with the profiler before any goroutine starts running. coroswitch is a place where a goroutine may start running, but where we do not check in with the profiler, which leads to crashes. Fix this by checking in with the profiler the same way execute does. Fixes #69998. Change-Id: Idef6dd31b70a73dd1c967b56c307c7a46a26ba73 Reviewed-on: https://go-review.googlesource.com/c/go/+/622016 Reviewed-by: David Chase <drchase@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src')
-rw-r--r--src/runtime/coro.go12
-rw-r--r--src/runtime/pprof/pprof_test.go45
2 files changed, 57 insertions, 0 deletions
diff --git a/src/runtime/coro.go b/src/runtime/coro.go
index f0aa868952..d378e92de8 100644
--- a/src/runtime/coro.go
+++ b/src/runtime/coro.go
@@ -211,6 +211,18 @@ func coroswitch_m(gp *g) {
// directly if possible.
setGNoWB(&mp.curg, gnext)
setMNoWB(&gnext.m, mp)
+
+ // Synchronize with any out-standing goroutine profile. We're about to start
+ // executing, and an invariant of the profiler is that we tryRecordGoroutineProfile
+ // whenever a goroutine is about to start running.
+ //
+ // N.B. We must do this before transitioning to _Grunning but after installing gnext
+ // in curg, so that we have a valid curg for allocation (tryRecordGoroutineProfile
+ // may allocate).
+ if goroutineProfile.active {
+ tryRecordGoroutineProfile(gnext, nil, osyield)
+ }
+
if !gnext.atomicstatus.CompareAndSwap(_Gwaiting, _Grunning) {
// The CAS failed: use casgstatus, which will take care of
// coordinating with the garbage collector about the state change.
diff --git a/src/runtime/pprof/pprof_test.go b/src/runtime/pprof/pprof_test.go
index e9f287df60..19641f62aa 100644
--- a/src/runtime/pprof/pprof_test.go
+++ b/src/runtime/pprof/pprof_test.go
@@ -15,6 +15,7 @@ import (
"internal/syscall/unix"
"internal/testenv"
"io"
+ "iter"
"math"
"math/big"
"os"
@@ -1784,6 +1785,50 @@ func TestGoroutineProfileConcurrency(t *testing.T) {
}
}
+// Regression test for #69998.
+func TestGoroutineProfileCoro(t *testing.T) {
+ testenv.MustHaveParallelism(t)
+
+ goroutineProf := Lookup("goroutine")
+
+ // Set up a goroutine to just create and run coroutine goroutines all day.
+ iterFunc := func() {
+ p, stop := iter.Pull2(
+ func(yield func(int, int) bool) {
+ for i := 0; i < 10000; i++ {
+ if !yield(i, i) {
+ return
+ }
+ }
+ },
+ )
+ defer stop()
+ for {
+ _, _, ok := p()
+ if !ok {
+ break
+ }
+ }
+ }
+ var wg sync.WaitGroup
+ done := make(chan struct{})
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ for {
+ iterFunc()
+ select {
+ case <-done:
+ default:
+ }
+ }
+ }()
+
+ // Take a goroutine profile. If the bug in #69998 is present, this will crash
+ // with high probability. We don't care about the output for this bug.
+ goroutineProf.WriteTo(io.Discard, 1)
+}
+
func BenchmarkGoroutine(b *testing.B) {
withIdle := func(n int, fn func(b *testing.B)) func(b *testing.B) {
return func(b *testing.B) {