From 09c62a86a96e51d535336de1c7b7e10cd3acd849 Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Fri, 9 Feb 2024 14:16:11 -0500 Subject: [PATCH] internal/trace: fix race condition in gc-stress 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 Auto-Submit: Michael Pratt Reviewed-by: Michael Knyszek --- src/internal/trace/v2/testdata/testprog/gc-stress.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/internal/trace/v2/testdata/testprog/gc-stress.go b/src/internal/trace/v2/testdata/testprog/gc-stress.go index 70d3a246c3..017f7f07bf 100644 --- a/src/internal/trace/v2/testdata/testprog/gc-stress.go +++ b/src/internal/trace/v2/testdata/testprog/gc-stress.go @@ -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)) } }() } -- 2.48.1