From a11f8f636b330e23bce4b8cedcaa7e48644630c2 Mon Sep 17 00:00:00 2001 From: Brendan Creane Date: Thu, 19 Nov 2026 23:18:24 +0000 Subject: [PATCH] ipn/ipnlocal: announce a changed exit node suggestion on every path Two paths reach suggestExitNodeLocked and then return without a broadcast: editPrefsLocked, when the edit is a no-op or the policy check rejects it, and reconcilePrefs, when the prefs are unchanged. Before the pending flag they announced a changed suggestion at once. Both now flush it before returning. TestSuggestedExitNodePendingFlushed marks a suggestion as pending and calls each entry point that can recompute it, checking that a Notify carries the suggestion and the flag is clear afterwards. Without the two flushes, the EditPrefs and reconcilePrefs cases fail. Updates #22206 Signed-off-by: Brendan Creane --- ipn/ipnlocal/local.go | 7 ++++ ipn/ipnlocal/local_test.go | 75 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 9993bdd0e..6f9a1190a 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -2518,6 +2518,9 @@ func (b *LocalBackend) reconcilePrefs() (_ ipn.PrefsView, anyChange bool) { prefs := b.pm.CurrentPrefs().AsStruct() if !b.reconcilePrefsLocked(prefs) { + // Reconciling can recompute the exit node suggestion even when + // the prefs end up unchanged. + b.sendSuggestedExitNodeLocked() return prefs.View(), false } return b.setPrefsLocked(prefs), true @@ -5844,6 +5847,10 @@ func (b *LocalBackend) editPrefsLocked(actor ipnauth.Actor, mp *ipn.MaskedPrefs) syncs.RequiresMutex(&b.mu) p0 := b.pm.CurrentPrefs() + // The access check below can recompute the exit node suggestion. The + // returns that send no prefs still announce a changed suggestion. + defer b.sendSuggestedExitNodeLocked() + // Check if the changes in mp are allowed. if err := b.checkEditPrefsAccessLocked(actor, p0, mp); err != nil { b.logf("EditPrefs(%v): %v", mp.Pretty(), err) diff --git a/ipn/ipnlocal/local_test.go b/ipn/ipnlocal/local_test.go index 2879f0854..7cac8e013 100644 --- a/ipn/ipnlocal/local_test.go +++ b/ipn/ipnlocal/local_test.go @@ -1729,6 +1729,81 @@ func TestSuggestExitNodeNotifies(t *testing.T) { nw.check() } +// TestSuggestedExitNodePendingFlushed checks that every entry point that can +// recompute the exit node suggestion announces a pending change before it +// returns, including the paths that send no prefs of their own. +func TestSuggestedExitNodePendingFlushed(t *testing.T) { + self := makePeer(1, withName("self"), withDERP(1), withAddresses(netip.MustParsePrefix("100.64.1.1/32"))) + tests := []struct { + name string + run func(t *testing.T, b *LocalBackend) + }{ + {"SetControlClientStatus without netmap", func(t *testing.T, b *LocalBackend) { + b.SetControlClientStatus(b.cc, controlclient.Status{}) + }}, + {"UpdateNetmapDelta without mutations", func(t *testing.T, b *LocalBackend) { + if !b.UpdateNetmapDelta(nil) { + t.Fatal("UpdateNetmapDelta returned false") + } + }}, + {"EditPrefs no-op", func(t *testing.T, b *LocalBackend) { + if _, err := b.EditPrefs(&ipn.MaskedPrefs{}); err != nil { + t.Fatal(err) + } + }}, + {"reconcilePrefs no-op", func(t *testing.T, b *LocalBackend) { + if _, changed := b.reconcilePrefs(); changed { + t.Fatal("reconcilePrefs reported a change") + } + }}, + {"RefreshExitNode no-op", func(t *testing.T, b *LocalBackend) { + b.RefreshExitNode() + }}, + {"SuggestExitNode", func(t *testing.T, b *LocalBackend) { + b.SuggestExitNode() + }}, + {"Start", func(t *testing.T, b *LocalBackend) { + if err := b.Start(ipn.Options{}); err != nil { + t.Fatal(err) + } + }}, + {"WatchNotifications initial suggestion", func(t *testing.T, b *LocalBackend) { + second := newNotificationWatcher(t, b, ipnauth.Self) + second.watch(ipn.NotifyInitialSuggestedExitNode, nil, func(testing.TB, ipnauth.Actor, *ipn.Notify) bool { return false }) + }}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := newTestLocalBackend(t) + b.SetControlClientStatus(b.cc, controlclient.Status{NetMap: buildNetmapWithPeers(self), LoggedIn: true}) + + nw := newNotificationWatcher(t, b, ipnauth.Self) + nw.watch(0, []wantedNotification{{ + name: "suggested exit node", + cond: func(_ testing.TB, _ ipnauth.Actor, n *ipn.Notify) bool { + return n.SuggestedExitNode != nil + }, + }}) + + // Pretend a suggestion changed without being announced yet. + b.mu.Lock() + b.lastSuggestedExitNode = "stale" + b.suggestedExitNodePending = true + b.mu.Unlock() + + tt.run(t, b) + nw.check() + + b.mu.Lock() + pending := b.suggestedExitNodePending + b.mu.Unlock() + if pending { + t.Error("suggestedExitNodePending still set after the call") + } + }) + } +} + func TestInternalAndExternalInterfaces(t *testing.T) { type interfacePrefix struct { i netmon.Interface