]> Cypherpunks repositories - gostls13.git/commitdiff
internal/trace: fix race condition in gc-stress
authorMichael Pratt <mpratt@google.com>
Fri, 9 Feb 2024 19:16:11 +0000 (14:16 -0500)
committerGopher Robot <gobot@golang.org>
Fri, 9 Feb 2024 20:10:45 +0000 (20:10 +0000)
Multiple goroutines all writing to the same sink triggers the race detector,
rightfully so.

Change-Id: Ia64836d0d88c0f587a6cb96ed747f656a3c1804a
Reviewed-on: https://go-review.googlesource.com/c/go/+/562997
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Michael Pratt <mpratt@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
src/internal/trace/v2/testdata/testprog/gc-stress.go

index 70d3a246c397f46e8dfc3b9d7031fcc181a629b4..017f7f07bff3ee32befb423c4130a4b48a970837 100644 (file)
@@ -39,7 +39,7 @@ func makeTree(depth int) *node {
 
 var trees [16]*node
 var ballast *[16]*[8192]*node
-var sink []byte
+var sink [][]byte
 
 func main() {
        for i := range trees {
@@ -54,10 +54,15 @@ func main() {
                        }
                }
        }
-       for i := 0; i < runtime.GOMAXPROCS(-1); i++ {
+
+       procs := runtime.GOMAXPROCS(-1)
+       sink = make([][]byte, procs)
+
+       for i := 0; i < procs; i++ {
+               i := i
                go func() {
                        for {
-                               sink = make([]byte, rand.Intn(32<<10))
+                               sink[i] = make([]byte, rand.Intn(32<<10))
                        }
                }()
        }