From dab49b63cb8009a2bb5a0400ef6ae319a8aaaef5 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sat, 12 Apr 2025 11:54:43 +0200 Subject: [PATCH] ci: try to fix race condition in testdata/goroutines.go A race condition was possible because the 'acquire' goroutine might not have started in 4 milliseconds which changed the ordering of the test. This patch fixes it by making sure the goroutine has started (and locked the mutex) before continuing with the test. --- testdata/goroutines.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/testdata/goroutines.go b/testdata/goroutines.go index cf19cc3ca0..8653cdb173 100644 --- a/testdata/goroutines.go +++ b/testdata/goroutines.go @@ -62,12 +62,15 @@ func main() { time.Sleep(2 * time.Millisecond) var m sync.Mutex + var wg sync.WaitGroup m.Lock() println("pre-acquired mutex") - go acquire(&m) + wg.Add(1) + go acquire(&m, &wg) time.Sleep(2 * time.Millisecond) println("releasing mutex") m.Unlock() + wg.Wait() time.Sleep(2 * time.Millisecond) m.Lock() println("re-acquired mutex") @@ -89,8 +92,9 @@ func main() { <-done } -func acquire(m *sync.Mutex) { +func acquire(m *sync.Mutex, wg *sync.WaitGroup) { m.Lock() + wg.Done() println("acquired mutex from goroutine") time.Sleep(2 * time.Millisecond) println("releasing mutex from goroutine")