Skip to content

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

Closed
@felixge

Description

@felixge

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixPendingIssues that have a fix which has not yet been reviewed or submitted.NeedsFixThe path to resolution is known, but the work has not been done.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Status

    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions