]> Cypherpunks repositories - gostls13.git/commitdiff
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>
Tue, 25 Aug 2020 18:28:59 +0000 (18:28 +0000)
Fixes #40908

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>

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 6fc8c4fa9f843b2630c134c6cb550a34933fb90d..bf83df8863244346b13be6152eba7d75f97fe764 100644 (file)
@@ -860,11 +860,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