diff options
author | Michał Łowicki <mlowicki@gmail.com> | 2020-08-23 23:53:04 +0100 |
---|---|---|
committer | Bryan C. Mills <bcmills@google.com> | 2020-08-27 20:26:34 +0000 |
commit | 45265c210c64b10ef86e120a3616602047036ca9 (patch) | |
tree | 2136db4d7d01c1769ecc1d737785f4517f502cb9 | |
parent | b1253d24e159129c778377c3a2a0bde15904a417 (diff) | |
download | go-45265c210c64b10ef86e120a3616602047036ca9.tar.gz go-45265c210c64b10ef86e120a3616602047036ca9.zip |
[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 00a053bd4b2c19b2d9680f78f4c8657fcc6f1c88)
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>
-rw-r--r-- | src/cmd/go/testdata/script/testing_issue40908.txt | 21 | ||||
-rw-r--r-- | src/testing/testing.go | 4 |
2 files changed, 25 insertions, 0 deletions
diff --git a/src/cmd/go/testdata/script/testing_issue40908.txt b/src/cmd/go/testdata/script/testing_issue40908.txt new file mode 100644 index 0000000000..4939de080c --- /dev/null +++ b/src/cmd/go/testdata/script/testing_issue40908.txt @@ -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 + }) +} diff --git a/src/testing/testing.go b/src/testing/testing.go index 061142b9ab..1bfcbb27e4 100644 --- a/src/testing/testing.go +++ b/src/testing/testing.go @@ -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 |