]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.15] testing: fix Cleanup race with Logf and Errorf
authorMichał Łowicki <mlowicki@gmail.com>
Sun, 23 Aug 2020 22:53:04 +0000 (23:53 +0100)
committerBryan C. Mills <bcmills@google.com>
Thu, 27 Aug 2020 20:26:34 +0000 (20:26 +0000)
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>
src/cmd/go/testdata/script/testing_issue40908.txt [new file with mode: 0644]
src/testing/testing.go

diff --git a/src/cmd/go/testdata/script/testing_issue40908.txt b/src/cmd/go/testdata/script/testing_issue40908.txt
new file mode 100644 (file)
index 0000000..4939de0
--- /dev/null
@@ -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
+       })
+}
index 061142b9abd2a736593a625f81ac067abd65d792..1bfcbb27e418655241df1a88f930a7deb0252c14 100644 (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