Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: MutexProfile missing root frames in go1.23 #69335

Closed
felixge opened this issue Sep 7, 2024 · 5 comments
Closed

runtime: MutexProfile missing root frames in go1.23 #69335

felixge opened this issue Sep 7, 2024 · 5 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FixPending Issues that have a fix which has not yet been reviewed or submitted. NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@felixge
Copy link
Contributor

felixge commented Sep 7, 2024

Go version

go1.23.1

What did you do?

Print the stack traces of a runtime.MutexProfile() using the code below.

package main

import (
	"fmt"
	"runtime"
	"sync"
	"time"
)

func main() {
	produceMutexContention()
	printMutexProfile()
}

func produceMutexContention() {
	runtime.SetMutexProfileFraction(1)
	var mu sync.Mutex
	done := make(chan struct{})
	defer close(done)
	for range runtime.GOMAXPROCS(-1) * 2 {
		go func() {
			for finish := false; !finish; {
				mu.Lock()
				select {
				case <-done:
					finish = true
				default:
					time.Sleep(time.Millisecond)
				}
				mu.Unlock()
			}
		}()
	}
	time.Sleep(time.Second)
}

func printMutexProfile() {
	var records []runtime.BlockProfileRecord
	for {
		n, ok := runtime.MutexProfile(records)
		if ok {
			records = records[:n]
			break
		}
		records = make([]runtime.BlockProfileRecord, n*2)
	}

	for _, r := range records {
		var funcs []string
		frames := runtime.CallersFrames(r.Stack())
		for {
			frame, more := frames.Next()
			funcs = append(funcs, frame.Function)
			if !more {
				break
			}
		}
		fmt.Printf("%#v\n", funcs)
	}
}

What did you see happen?

go1.23.1 produces the following output:

[]string{"sync.(*Mutex).Unlock", "main.produceMutexContention.func1"}
[]string{""}

What did you expect to see?

The same output as go1.22.7:

[]string{"sync.(*Mutex).Unlock", "main.produceMutexContention.func1", "runtime.goexit"}
[]string{"runtime._LostContendedRuntimeLock"}

Additional Notes

This problem only happens when using the runtime.MutexProfile API. runtime/pprof output is not impacted.

I have submitted https://go-review.googlesource.com/c/go/+/611615 to fix this problem.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Sep 7, 2024
@felixge felixge changed the title runtime: MutexProfile misses _LostContendedRuntimeLock in go1.23 runtime: MutexProfile missing root frames in go1.23 Sep 7, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/611615 mentions this issue: runtime: fix MutexProfile missing root frames

@timothy-king timothy-king added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 9, 2024
@timothy-king timothy-king added this to the Go1.23.2 milestone Sep 9, 2024
@timothy-king
Copy link
Contributor

CC @golang/runtime, @mknyszek, @prattmic.

@timothy-king timothy-king modified the milestones: Go1.23.2, Go1.24 Sep 13, 2024
@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. FixPending Issues that have a fix which has not yet been reviewed or submitted. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Sep 13, 2024
@mknyszek
Copy link
Contributor

mknyszek commented Oct 2, 2024

Ping @felixge @nsrip-dd re: @cagedmantis's question from #69243. Does this need a backport as well? Thanks.

@nsrip-dd
Copy link
Contributor

nsrip-dd commented Oct 2, 2024

@mknyszek I'd say yes, since this bug causes mutex profiles obtained via runtime.MutexProfile to miss information, specifically runtime._LostContendedRuntimeLock records will have no stack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FixPending Issues that have a fix which has not yet been reviewed or submitted. NeedsFix The path to resolution is known, but the work has not been done.
Projects
Development

No branches or pull requests

7 participants