From: Michał Łowicki Date: Sun, 23 Aug 2020 22:53:04 +0000 (+0100) Subject: [release-branch.go1.15] testing: fix Cleanup race with Logf and Errorf X-Git-Tag: go1.15.2~13 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=45265c210c64b10ef86e120a3616602047036ca9;p=gostls13.git [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 Run-TryBot: Bryan C. Mills TryBot-Result: Gobot Gobot (cherry picked from commit 00a053bd4b2c19b2d9680f78f4c8657fcc6f1c88) Reviewed-on: https://go-review.googlesource.com/c/go/+/250617 Reviewed-by: Emmanuel Odeke Reviewed-by: Michał Łowicki --- 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