Skip to content

Commit 00a053b

Browse files
mlowickiBryan C. Mills
authored and
Bryan C. Mills
committed
testing: fix Cleanup race with Logf and Errorf
Fixes #40908 Change-Id: I25561a3f18e730a50e6fbf85aa7bd85bf1b73b6e Reviewed-on: https://go-review.googlesource.com/c/go/+/250078 Reviewed-by: Tobias Klauser <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 8d31ca2 commit 00a053b

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[short] skip
2+
[!race] skip
3+
4+
go test -race testrace
5+
6+
-- testrace/race_test.go --
7+
package testrace
8+
9+
import "testing"
10+
11+
func TestRace(t *testing.T) {
12+
helperDone := make(chan struct{})
13+
go func() {
14+
t.Logf("Something happened before cleanup.")
15+
close(helperDone)
16+
}()
17+
18+
t.Cleanup(func() {
19+
<-helperDone
20+
})
21+
}

src/testing/testing.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,11 +860,15 @@ func (c *common) Cleanup(f func()) {
860860
c.cleanup = func() {
861861
if oldCleanup != nil {
862862
defer func() {
863+
c.mu.Lock()
863864
c.cleanupPc = oldCleanupPc
865+
c.mu.Unlock()
864866
oldCleanup()
865867
}()
866868
}
869+
c.mu.Lock()
867870
c.cleanupName = callerName(0)
871+
c.mu.Unlock()
868872
f()
869873
}
870874
var pc [maxStackLen]uintptr

0 commit comments

Comments
 (0)