Skip to content

runtime/sema: incorrect mutex contention time #73760

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

Open
func25 opened this issue May 18, 2025 · 1 comment
Open

runtime/sema: incorrect mutex contention time #73760

func25 opened this issue May 18, 2025 · 1 comment
Labels
BugReport Issues describing a possible bug in the Go implementation.

Comments

@func25
Copy link
Contributor

func25 commented May 18, 2025

Go version

go1.23.9

Output of go env in your module/workspace:

none

What did you do?

I tried a simple snippet to evaluate the mutex profile:

var mtx sync.Mutex
var wg sync.WaitGroup

func acquire(n time.Duration) {
	mtx.Lock()
	defer mtx.Unlock()

	time.Sleep(n)
	wg.Done()
}

func main() {
	runtime.SetMutexProfileFraction(1)

	wg.Add(5)
	go acquire(1 * time.Second) // gowrap1
	go acquire(2 * time.Second) // gowrap2
	go acquire(3 * time.Second) // gowrap3
	go acquire(4 * time.Second) // gowrap4
	go acquire(5 * time.Second) // gowrap5

	wg.Wait()

	// Write mutex profile to file
	f, err := os.Create("mutex.prof")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	if err := pprof.Lookup("mutex").WriteTo(f, 0); err != nil {
		log.Fatal(err)
	}
}

What did you see happen?

The order in which goroutines acquire the lock is: gowrap1gowrap5gowrap2gowrap3gowrap4. However, the result shows an overshoot in gowrap3's contention time and underestimates gowrap2:

Image

What did you expect to see?

The expected contention time for gowrap2 should be 4 seconds. The contention time for gowrap3 should be 3 seconds. Not sure if this behavior is intentional.

@func25 func25 closed this as completed May 18, 2025
@func25 func25 reopened this May 18, 2025
@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label May 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation.
Projects
None yet
Development

No branches or pull requests

2 participants