mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-03 23:22:23 -08:00
health: don't publish a stale Change from a superseded visibility timer
The visibility timer's callback only checked that its Warnable was still unhealthy. If the Warnable was reset just as the timer fired, the callback published the old Change even though the Warnable was no longer visible, and removed the new timer from pendingVisibleTimers, so that SetHealthy could not stop it. Similarly, changing a pending Warnable's args left the old timer running. Subscribers such as captive portal detection then received a Change for a Warnable that was not in CurrentState. Stop the pending timer when the unhealthy state changes, and make the callback check that the state it was started for is still current. Updates #15396 Updates #1634 Updates tailscale/corp#27985 Signed-off-by: Nick Khyl <[email protected]>
This commit is contained in:
1 parent
94f41e9841
commit
cb5166bdc7
2 files changed
+82
-6
No files matched your search
+10
-6
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user