1
0
mirror of https://github.com/golang/go synced 2024-11-15 00:40:31 -07:00

[release-branch.go1.15] testing: fix Cleanup race with Logf and Errorf

Updates #40908
Fixes #41034

Change-Id: I25561a3f18e730a50e6fbf85aa7bd85bf1b73b6e
Reviewed-on: https://go-review.googlesource.com/c/go/+/250078
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
(cherry picked from commit 00a053bd4b)
Reviewed-on: https://go-review.googlesource.com/c/go/+/250617
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Reviewed-by: Michał Łowicki <mlowicki@gmail.com>
This commit is contained in:
Michał Łowicki 2020-08-23 23:53:04 +01:00 committed by Bryan C. Mills
parent b1253d24e1
commit 45265c210c
2 changed files with 25 additions and 0 deletions

View File

@ -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
})
}

View File

@ -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