diff --git a/health/health.go b/health/health.go index 4dd9f7f12..aec2310e4 100644 --- a/health/health.go +++ b/health/health.go @@ -427,9 +427,12 @@ func (t *Tracker) setUnhealthyLocked(w *Warnable, args Args) { BrokenSince: brokenSince, Args: args, } - prevWs := t.warnableVal[w] - mak.Set(&t.warnableVal, w, ws) - if !ws.Equal(prevWs) { + if prevWs := t.warnableVal[w]; !ws.Equal(prevWs) { + mak.Set(&t.warnableVal, w, ws) + if tc, ok := t.pendingVisibleTimers[w]; ok { + tc.Stop() + delete(t.pendingVisibleTimers, w) + } change := Change{ WarnableChanged: true, @@ -446,9 +449,10 @@ func (t *Tracker) setUnhealthyLocked(w *Warnable, args Args) { tc := t.clock().AfterFunc(visibleIn, func() { t.mu.Lock() defer t.mu.Unlock() - // Check if the Warnable is still unhealthy, as it could have become healthy between the time - // the timer was set for and the time it was executed. - if t.warnableVal[w] != nil { + // Check that ws is still current, as the Warnable could have become healthy, or unhealthy + // again with a different state, between the time the timer fired and the time it was executed. + // In that case, the pending timer, if any, isn't ours either. + if t.warnableVal[w] == ws { t.changePub.Publish(change) delete(t.pendingVisibleTimers, w) } diff --git a/health/health_test.go b/health/health_test.go index 6af8b6820..a40019922 100644 --- a/health/health_test.go +++ b/health/health_test.go @@ -1225,3 +1225,75 @@ func TestDERPWarnablesOutOfMapPoll(t *testing.T) { } }) } + +// TestStaleVisibilityTimer verifies that a visibility timer only publishes +// the unhealthy state it was started for. +func TestStaleVisibilityTimer(t *testing.T) { + w := Register(&Warnable{ + Code: "test-stale-visibility-timer", + Text: func(args Args) string { return args[ArgError] }, + TimeToVisible: 10 * time.Second, + }) + defer unregister(w) + + // start returns a Tracker and the texts of the Changes it publishes for w + // while w isn't in the current state. + start := func(t *testing.T) (*Tracker, *[]string) { + bus := eventbustest.NewBus(t) + ht := NewTracker(bus) + var stale []string + eventbus.SubscribeFunc(bus.Client("test"), func(c Change) { + if c.Warnable != w || c.UnhealthyState == nil { + return + } + if got := ht.CurrentState().Warnings[w.Code].Text; got != c.UnhealthyState.Text { + stale = append(stale, c.UnhealthyState.Text) + } + }) + return ht, &stale + } + + t.Run("reset-while-firing", func(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + ht, stale := start(t) + ht.SetUnhealthy(w, Args{ArgError: "first"}) + + // Reset w just as its timer fires, with the timer's + // callback waiting for the mutex. + ht.mu.Lock() + time.Sleep(w.TimeToVisible) + ht.setHealthyLocked(w) + ht.setUnhealthyLocked(w, Args{ArgError: "second"}) + ht.mu.Unlock() + synctest.Wait() + + // The new timer must still be cancelable. + ht.SetHealthy(w) + ht.SetUnhealthy(w, Args{ArgError: "third"}) + time.Sleep(w.TimeToVisible) + synctest.Wait() + + if len(*stale) != 0 { + t.Errorf("published changes not in the current state: %q", *stale) + } + }) + }) + + t.Run("args-changed", func(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + ht, stale := start(t) + ht.SetUnhealthy(w, Args{ArgError: "first"}) + time.Sleep(w.TimeToVisible / 2) + ht.SetUnhealthy(w, Args{ArgError: "second"}) + time.Sleep(w.TimeToVisible) + synctest.Wait() + + if len(*stale) != 0 { + t.Errorf("published changes not in the current state: %q", *stale) + } + if got := ht.CurrentState().Warnings[w.Code].Text; got != "second" { + t.Errorf("got text %q; want %q", got, "second") + } + }) + }) +}