Skip to content

Commit

Permalink
[release-branch.go1.15] testing: fix Cleanup race with Logf and Errorf
Browse files Browse the repository at this point in the history
Updates #40908
Fixes #41034

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]>
(cherry picked from commit 00a053b)
Reviewed-on: https://go-review.googlesource.com/c/go/+/250617
Reviewed-by: Emmanuel Odeke <[email protected]>
Reviewed-by: Michał Łowicki <[email protected]>
  • Loading branch information
mlowicki authored and Bryan C. Mills committed Aug 27, 2020
1 parent b1253d2 commit 45265c2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/cmd/go/testdata/script/testing_issue40908.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[short] skip
[!race] skip

go test -race testrace

-- testrace/race_test.go --
package testrace

import "testing"

func TestRace(t *testing.T) {
helperDone := make(chan struct{})
go func() {
t.Logf("Something happened before cleanup.")
close(helperDone)
}()

t.Cleanup(func() {
<-helperDone
})
}
4 changes: 4 additions & 0 deletions src/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,11 +851,15 @@ func (c *common) Cleanup(f func()) {
c.cleanup = func() {
if oldCleanup != nil {
defer func() {
c.mu.Lock()
c.cleanupPc = oldCleanupPc
c.mu.Unlock()
oldCleanup()
}()
}
c.mu.Lock()
c.cleanupName = callerName(0)
c.mu.Unlock()
f()
}
var pc [maxStackLen]uintptr
Expand Down

0 comments on commit 45265c2

Please sign in to comment.