From 213ae69bee190c124465a98b7e63dc199ad2bb34 Mon Sep 17 00:00:00 2001 From: Spike Curtis Date: Tue, 6 Feb 2024 20:21:23 +0400 Subject: [PATCH] fix: start timer before subscribing to avoid test race (#12031) Fixes #12030 This is a good example of the kind of thing I'd like to address with a time-testing lib. The problem is that there is a race between the watchdog starting it's timer and the test incrementing the time. What would make this easier is if the time-testing library could wait for and assert the call to start the timer before incrementing the time. --- coderd/database/pubsub/watchdog.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/coderd/database/pubsub/watchdog.go b/coderd/database/pubsub/watchdog.go index 2b8d929fa7..687129fc5b 100644 --- a/coderd/database/pubsub/watchdog.go +++ b/coderd/database/pubsub/watchdog.go @@ -103,6 +103,8 @@ func (w *Watchdog) publishLoop() { func (w *Watchdog) subscribeMonitor() { defer w.wg.Done() + tmr := w.clock.Timer(periodTimeout) + defer tmr.Stop() beats := make(chan struct{}) unsub, err := w.ps.Subscribe(EventPubsubWatchdog, func(context.Context, []byte) { w.logger.Debug(w.ctx, "got heartbeat for pubsub watchdog") @@ -117,8 +119,6 @@ func (w *Watchdog) subscribeMonitor() { return } defer unsub() - tmr := w.clock.Timer(periodTimeout) - defer tmr.Stop() for { select { case <-w.ctx.Done():