From 035b1cdd01ca3059a554b4a759e93624d7c923b7 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Tue, 25 Nov 2025 17:10:44 +0000 Subject: [PATCH] Fix issue with tests failing due to test-related race conditions. --- internal/forward/watchdog_test.go | 19 ++++++++++++++++--- internal/mdns/publisher.go | 3 ++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/internal/forward/watchdog_test.go b/internal/forward/watchdog_test.go index 346b34c..725977c 100644 --- a/internal/forward/watchdog_test.go +++ b/internal/forward/watchdog_test.go @@ -154,12 +154,21 @@ func (s *WatchdogTestSuite) TestMultipleWorkers() { s.watchdog.RegisterWorker("worker-3", makeCallback("worker-3")) // worker-1: Keep sending heartbeats (healthy) + // Use a done channel to ensure goroutine exits before test ends ticker1 := time.NewTicker(50 * time.Millisecond) - defer ticker1.Stop() + done := make(chan struct{}) + var wg sync.WaitGroup + wg.Add(1) go func() { + defer wg.Done() + defer ticker1.Stop() for i := 0; i < 10; i++ { - <-ticker1.C - s.watchdog.Heartbeat("worker-1") + select { + case <-ticker1.C: + s.watchdog.Heartbeat("worker-1") + case <-done: + return + } } }() @@ -172,6 +181,10 @@ func (s *WatchdogTestSuite) TestMultipleWorkers() { // Wait for hung workers to be detected time.Sleep(600 * time.Millisecond) + // Signal goroutine to stop and wait for it + close(done) + wg.Wait() + // Check results mu.Lock() defer mu.Unlock() diff --git a/internal/mdns/publisher.go b/internal/mdns/publisher.go index aa98d57..6f67b88 100644 --- a/internal/mdns/publisher.go +++ b/internal/mdns/publisher.go @@ -155,7 +155,8 @@ func (p *Publisher) Stop() { // goroutines to exit cleanly. This works around a race condition in the // grandcat/zeroconf library where recv4() can access ipv4conn after shutdown() // sets it to nil. See: https://github.com/grandcat/zeroconf/issues/95 -const shutdownSettleTime = 50 * time.Millisecond +// Note: 100ms is needed for CI environments where timing can be more variable. +const shutdownSettleTime = 100 * time.Millisecond // shutdownWithTimeout attempts to shutdown a zeroconf server with a timeout. // If shutdown hangs, it logs a warning and returns anyway.