From e2aa441e8108002db0e724dd26618fe05ca362d6 Mon Sep 17 00:00:00 2001 From: Nick Khyl Date: Sun, 29 Nov 2026 07:43:59 +0000 Subject: [PATCH] feature/captiveportal: run detection once per network or connectivity change Every health change while connectivity remained impacted ran captive portal detection again, even though nothing about the network had changed. Track whether detection is due. It becomes due when connectivity stops being impacted and when netmon reports a network change that likely requires a rebind, and is no longer due once detection has run. Health and pref changes still call maybeDetectCaptivePortal, but it only runs detection when it is due. Otherwise, it just drops the warning once connectivity is no longer impacted. This also makes ignoring changes to the captive portal warnable itself unnecessary. Updates #15396 Signed-off-by: Nick Khyl --- feature/captiveportal/captiveportal.go | 33 +++++-- feature/captiveportal/captiveportal_test.go | 100 ++++++++++++++------ 2 files changed, 98 insertions(+), 35 deletions(-) diff --git a/feature/captiveportal/captiveportal.go b/feature/captiveportal/captiveportal.go index 9253b3d8a..d92916b2b 100644 --- a/feature/captiveportal/captiveportal.go +++ b/feature/captiveportal/captiveportal.go @@ -8,6 +8,7 @@ package captiveportal import ( "context" + "sync/atomic" "time" "tailscale.com/feature" @@ -16,6 +17,7 @@ import ( "tailscale.com/ipn" "tailscale.com/ipn/ipnext" "tailscale.com/net/captivedetection" + "tailscale.com/net/netmon" "tailscale.com/tailcfg" "tailscale.com/types/logger" "tailscale.com/util/clientmetric" @@ -64,6 +66,10 @@ type Extension struct { // maybeDetectOp serializes and coalesces calls // to [Extension.maybeDetectCaptivePortal]. maybeDetectOp *coalescedop.CoalescedOp + + // captiveDetectionDue is whether captive portal detection has not run + // since connectivity became impacted or the network last changed. + captiveDetectionDue atomic.Bool } func newExtension(logf logger.Logf, sb ipnext.SafeBackend) (ipnext.Extension, error) { @@ -76,6 +82,7 @@ func newExtension(logf logger.Logf, sb ipnext.SafeBackend) (ipnext.Extension, er cancel: cancel, } e.maybeDetectOp = coalescedop.New(e.maybeDetectCaptivePortal) + e.captiveDetectionDue.Store(true) return e, nil } @@ -86,6 +93,7 @@ func (e *Extension) Init(h ipnext.Host) error { h.Hooks().ProfileStateChange.Add(e.onProfileStateChange) e.ec = e.sb.Sys().Bus.Get().Client("captiveportal") eventbus.SubscribeFunc(e.ec, e.onHealthChange) + eventbus.SubscribeFunc(e.ec, e.onNetworkChange) e.maybeDetectOp.Do() // non-blocking return nil } @@ -106,10 +114,15 @@ func (e *Extension) onProfileStateChange(ipn.LoginProfileView, ipn.PrefsView, bo } // onHealthChange is called (via the eventbus) whenever the node's health -// state changes. It ignores changes to the [captivePortalWarnable], which are -// made by check itself. -func (e *Extension) onHealthChange(c health.Change) { - if c.Warnable != captivePortalWarnable { +// state changes. +func (e *Extension) onHealthChange(health.Change) { + e.maybeDetectOp.Do() // non-blocking; coalesced +} + +// onNetworkChange is called (via the eventbus) whenever the network changes. +func (e *Extension) onNetworkChange(delta netmon.ChangeDelta) { + if delta.RebindLikelyRequired { + e.captiveDetectionDue.Store(true) e.maybeDetectOp.Do() // non-blocking; coalesced } } @@ -132,10 +145,11 @@ func (e *Extension) wantCaptiveDetection() bool { } // maybeDetectCaptivePortal runs captive portal detection -// if connectivity has been impacted for [captivePortalDetectionDelay], -// and updates the [captivePortalWarnable] accordingly. +// if it is due and connectivity has been impacted for +// [captivePortalDetectionDelay], and updates the [captivePortalWarnable] +// accordingly. func (e *Extension) maybeDetectCaptivePortal() { - if e.wantCaptiveDetection() { + if e.wantCaptiveDetection() && e.captiveDetectionDue.Load() { select { case <-e.ctx.Done(): return @@ -146,6 +160,11 @@ func (e *Extension) maybeDetectCaptivePortal() { // If connectivity is not impacted, we know for sure // we're not behind a captive portal. e.health.SetHealthy(captivePortalWarnable) + // Detect again the next time it is. + e.captiveDetectionDue.Store(true) + return + } + if !e.captiveDetectionDue.Swap(false) { return } diff --git a/feature/captiveportal/captiveportal_test.go b/feature/captiveportal/captiveportal_test.go index 4d1d68c4b..87d916b71 100644 --- a/feature/captiveportal/captiveportal_test.go +++ b/feature/captiveportal/captiveportal_test.go @@ -4,6 +4,7 @@ package captiveportal import ( + "sync/atomic" "testing" "testing/synctest" "time" @@ -11,7 +12,10 @@ import ( "tailscale.com/health" "tailscale.com/ipn" "tailscale.com/ipn/ipnext" + "tailscale.com/net/netmon" + "tailscale.com/tailcfg" "tailscale.com/tsd" + "tailscale.com/util/eventbus" "tailscale.com/util/eventbus/eventbustest" ) @@ -22,73 +26,113 @@ type fakeBackend struct { func (b fakeBackend) Sys() *tsd.System { return b.sys } -// fakeHost is an [ipnext.Host] that only provides hooks and the current prefs. +// fakeHost is an [ipnext.Host] that only provides hooks and the current +// prefs, and counts the detections that ask it for a NodeBackend. type fakeHost struct { ipnext.Host ipnext.ProfileServices - hooks ipnext.Hooks - prefs ipn.Prefs + hooks ipnext.Hooks + wantRunning atomic.Bool + detections atomic.Int64 } +type fakeNodeBackend struct{ ipnext.NodeBackend } + +func (fakeNodeBackend) DERPMap() *tailcfg.DERPMap { return nil } + func (h *fakeHost) Hooks() *ipnext.Hooks { return &h.hooks } func (h *fakeHost) Profiles() ipnext.ProfileServices { return h } -func (h *fakeHost) CurrentPrefs() ipn.PrefsView { return h.prefs.View() } + +func (h *fakeHost) CurrentPrefs() ipn.PrefsView { + return (&ipn.Prefs{WantRunning: h.wantRunning.Load()}).View() +} + +func (h *fakeHost) NodeBackend() ipnext.NodeBackend { + h.detections.Add(1) + return fakeNodeBackend{} +} func (h *fakeHost) setWantRunning(e *Extension, wantRunning bool) { - h.prefs.WantRunning = wantRunning - e.onProfileStateChange(ipn.LoginProfileView{}, h.prefs.View(), true) + h.wantRunning.Store(wantRunning) + e.onProfileStateChange(ipn.LoginProfileView{}, h.CurrentPrefs(), true) } +var testWarnable = health.Register(&health.Warnable{ + Code: "test-captiveportal", + Text: health.StaticMessage("unrelated to connectivity"), +}) + func TestMaybeDetectCaptivePortal(t *testing.T) { synctest.Test(t, func(t *testing.T) { - sys := tsd.NewSystemWithBus(eventbustest.NewBus(t)) + bus := eventbustest.NewBus(t) + sys := tsd.NewSystemWithBus(bus) + // An empty Monitor has no interfaces up, so detection + // finds nothing without using the network. + sys.Set(new(netmon.Monitor)) ht := sys.HealthTracker.Get() ht.SetIPNState("Running", true) + networkChanged := eventbus.Publish[netmon.ChangeDelta](bus.Client("test")) + ext, err := newExtension(t.Logf, fakeBackend{sys: sys}) if err != nil { t.Fatal(err) } e := ext.(*Extension) defer e.Shutdown() - wait := func() { - synctest.Wait() // for the health.Change subscriber + h := new(fakeHost) + h.wantRunning.Store(true) + + // check verifies how many detections ran since the last check. + check := func(step string, want int64) { + t.Helper() + synctest.Wait() // for the eventbus subscribers e.maybeDetectOp.Wait() + if got := h.detections.Swap(0); got != want { + t.Errorf("%s: ran %d detections; want %d", step, got, want) + } } - // Init runs a check, which drops a warning that shouldn't be there. ht.SetUnhealthy(captivePortalWarnable, nil) - h := &fakeHost{prefs: ipn.Prefs{WantRunning: true}} if err := e.Init(h); err != nil { t.Fatal(err) } - wait() + check("Init", 0) if ht.IsUnhealthy(captivePortalWarnable) { t.Error("warning remained after Init") } - // Connectivity recovers while check waits to run detection, - // so it doesn't run it. It would panic without a NodeBackend. - ht.SetAnyInterfaceUp(false) - time.Sleep(health.NetworkStatusWarnable.TimeToVisible + captivePortalDetectionDelay/2) - ht.SetAnyInterfaceUp(true) - wait() + ht.SetUDP4Unbound(true) // impacts connectivity + check("connectivity impacted", 1) - // A change to control health has no Warnable. + ht.SetUnhealthy(testWarnable, nil) e.onHealthChange(health.Change{ControlHealthChanged: true}) - wait() + h.setWantRunning(e, true) + check("unrelated changes", 0) + + networkChanged.Publish(netmon.ChangeDelta{}) + check("minor network change", 0) + networkChanged.Publish(netmon.ChangeDelta{RebindLikelyRequired: true}) + check("network change", 1) + + ht.SetUDP4Unbound(false) + check("connectivity recovered", 0) + ht.SetUDP4Unbound(true) + check("connectivity impacted again", 1) - // A change to the warning itself doesn't cause a check. ht.SetUnhealthy(captivePortalWarnable, nil) - wait() - if !ht.IsUnhealthy(captivePortalWarnable) { - t.Error("warning dropped by its own change") - } - - // Clearing WantRunning drops the warning. h.setWantRunning(e, false) - wait() + check("WantRunning cleared", 0) if ht.IsUnhealthy(captivePortalWarnable) { t.Error("warning remained after clearing WantRunning") } + h.setWantRunning(e, true) + check("WantRunning set", 1) + + ht.SetUDP4Unbound(false) + check("connectivity recovered", 0) + ht.SetUDP4Unbound(true) + time.Sleep(captivePortalDetectionDelay / 2) + ht.SetUDP4Unbound(false) + check("connectivity recovered during the delay", 0) }) }