fix: resolve race condition in mock mode update loop

This commit is contained in:
rcourtman 2025-10-16 09:04:50 +00:00
parent 219fcc6de5
commit f7c4e9cdde

View file

@ -20,6 +20,7 @@ var (
enabled atomic.Bool
updateTicker *time.Ticker
stopUpdatesCh chan struct{}
updateLoopWg sync.WaitGroup
)
const updateInterval = 2 * time.Second
@ -125,7 +126,9 @@ func startUpdateLoopLocked() {
stopUpdatesCh = stopCh
updateTicker = ticker
updateLoopWg.Add(1)
go func(stop <-chan struct{}, tick *time.Ticker) {
defer updateLoopWg.Done()
for {
select {
case <-tick.C:
@ -139,14 +142,16 @@ func startUpdateLoopLocked() {
}
func stopUpdateLoopLocked() {
if ticker := updateTicker; ticker != nil {
ticker.Stop()
updateTicker = nil
}
if ch := stopUpdatesCh; ch != nil {
close(ch)
stopUpdatesCh = nil
}
if ticker := updateTicker; ticker != nil {
ticker.Stop()
updateTicker = nil
}
// Wait for the update goroutine to exit
updateLoopWg.Wait()
}
func updateMetrics(cfg MockConfig) {