1
0
mirror of https://github.com/golang/go synced 2024-11-11 23:10:23 -07:00

runtime: fix deadlock in runtime.Stack

It shouldn't semacquire() inside an acquirem(), the runtime
thinks that means deadlock.  It actually isn't a deadlock, but it
looks like it because acquirem() does m.locks++.

Candidate for inclusion in 1.4.1.  runtime.Stack with all=true
is pretty unuseable in GOMAXPROCS>1 environment.

fixes #9321

Change-Id: Iac6b664217d24763b9878c20e49229a1ecffc805
Reviewed-on: https://go-review.googlesource.com/1600
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
This commit is contained in:
Keith Randall 2014-12-15 14:39:28 -08:00
parent 8f36655346
commit 50bc3d5bbc
2 changed files with 42 additions and 9 deletions

View File

@ -564,20 +564,16 @@ func saveg(pc, sp uintptr, gp *g, r *StackRecord) {
// If all is true, Stack formats stack traces of all other goroutines // If all is true, Stack formats stack traces of all other goroutines
// into buf after the trace for the current goroutine. // into buf after the trace for the current goroutine.
func Stack(buf []byte, all bool) int { func Stack(buf []byte, all bool) int {
mp := acquirem()
gp := mp.curg
if all { if all {
semacquire(&worldsema, false) semacquire(&worldsema, false)
mp.gcing = 1 gp := getg()
releasem(mp) gp.m.gcing = 1
systemstack(stoptheworld) systemstack(stoptheworld)
if mp != acquirem() {
gothrow("Stack: rescheduled")
}
} }
n := 0 n := 0
if len(buf) > 0 { if len(buf) > 0 {
gp := getg()
sp := getcallersp(unsafe.Pointer(&buf)) sp := getcallersp(unsafe.Pointer(&buf))
pc := getcallerpc(unsafe.Pointer(&buf)) pc := getcallerpc(unsafe.Pointer(&buf))
systemstack(func() { systemstack(func() {
@ -594,11 +590,11 @@ func Stack(buf []byte, all bool) int {
} }
if all { if all {
mp.gcing = 0 gp := getg()
gp.m.gcing = 0
semrelease(&worldsema) semrelease(&worldsema)
systemstack(starttheworld) systemstack(starttheworld)
} }
releasem(mp)
return n return n
} }

View File

@ -0,0 +1,37 @@
// run
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"runtime"
"runtime/pprof"
"sync"
)
func test() {
var wg sync.WaitGroup
wg.Add(2)
test := func() {
for i := 0; i < 100; i++ {
buf := &bytes.Buffer{}
pprof.Lookup("goroutine").WriteTo(buf, 2)
}
wg.Done()
}
go test()
go test()
wg.Wait()
}
func main() {
runtime.GOMAXPROCS(2)
for i := 0; i < 100; i++ {
test()
}
}