Skip to content

Commit 4830ad9

Browse files
jellevandenhooffbradfitz
authored andcommitted
runtime: set raceignore to zero when starting a new goroutine
When reusing a g struct the runtime did not reset g.raceignore. Initialize raceignore to zero when initially setting racectx. A goroutine can end with a non-zero raceignore if it exits after calling runtime.RaceDisable without a matching runtime.RaceEnable. If that goroutine's g is later reused the race detector is in a weird state: the underlying g.racectx is active, yet g.raceignore is non-zero, and raceacquire/racerelease which check g.raceignore become no-ops. This causes the race detector to report races when there are none. Fixes golang#60934 Change-Id: Ib8e412f11badbaf69a480f03740da70891f4093f Reviewed-on: https://go-review.googlesource.com/c/go/+/505055 Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Michael Knyszek <[email protected]>
1 parent 1632203 commit 4830ad9

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/runtime/proc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4570,6 +4570,7 @@ func newproc1(fn *funcval, callergp *g, callerpc uintptr) *g {
45704570
pp.goidcache++
45714571
if raceenabled {
45724572
newg.racectx = racegostart(callerpc)
4573+
newg.raceignore = 0
45734574
if newg.labels != nil {
45744575
// See note in proflabel.go on labelSync's role in synchronizing
45754576
// with the reads in the signal handler.

src/runtime/race/testdata/mop_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,3 +2093,40 @@ func TestNoRaceTinyAlloc(t *testing.T) {
20932093
<-done
20942094
}
20952095
}
2096+
2097+
func TestNoRaceIssue60934(t *testing.T) {
2098+
// Test that runtime.RaceDisable state doesn't accidentally get applied to
2099+
// new goroutines.
2100+
2101+
// Create several goroutines that end after calling runtime.RaceDisable.
2102+
var wg sync.WaitGroup
2103+
ready := make(chan struct{})
2104+
wg.Add(32)
2105+
for i := 0; i < 32; i++ {
2106+
go func() {
2107+
<-ready // ensure we have multiple goroutines running at the same time
2108+
runtime.RaceDisable()
2109+
wg.Done()
2110+
}()
2111+
}
2112+
close(ready)
2113+
wg.Wait()
2114+
2115+
// Make sure race detector still works. If the runtime.RaceDisable state
2116+
// leaks, the happens-before edges here will be ignored and a race on x will
2117+
// be reported.
2118+
var x int
2119+
ch := make(chan struct{}, 0)
2120+
wg.Add(2)
2121+
go func() {
2122+
x = 1
2123+
ch <- struct{}{}
2124+
wg.Done()
2125+
}()
2126+
go func() {
2127+
<-ch
2128+
_ = x
2129+
wg.Done()
2130+
}()
2131+
wg.Wait()
2132+
}

0 commit comments

Comments
 (0)