From f3552c29c04e5ec4d0d38637e67e1f8a40d8ca09 Mon Sep 17 00:00:00 2001 From: "M. J. Fromberger" Date: Wed, 19 Aug 2026 10:31:38 -0700 Subject: [PATCH] ipn/ipnlocal: fix cache update for peers deleted by netmap deltas (#20851) After a netmap delta is applied, we scan the mutations for affected peers and update the cache (if enabled) for those peers. For removals in particular, we were relying on the node backend to resolve node IDs (provided by the delta mutation) to stable IDs. Prior to 65fd320a this happened to work because the node backend would hold on to all the peers mentioned by the previous full netmap, even after applying deltas. But that was essentially accidental, and once we fixed it not to do that, these lookups no longer worked. We need the stable ID, since that is how the cache is keyed, and now that they're no longer pinned, we were not properly evicting removed peers from the cache. To fix this, capture removed peer stable IDs while applying mutations to the node backend, instead of trying to look them up afterward. Updates #20796 Change-Id: I14ded78eaf9657645f0869a52460fd3cd86edba6 Signed-off-by: M. J. Fromberger --- ipn/ipnlocal/local.go | 8 +------- ipn/ipnlocal/local_test.go | 22 ++++++++++++++++++++++ ipn/ipnlocal/node_backend.go | 5 +++++ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index dd56eb775..0c47238fc 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -2566,13 +2566,7 @@ func (b *LocalBackend) UpdateNetmapDelta(muts []netmap.NodeMutation) (handled bo peersToUpdate = append(peersToUpdate, n) } } - var peersToRemove []tailcfg.StableNodeID - for id := range removeIDs { - if n, ok := cn.NodeByID(id); ok { - peersToRemove = append(peersToRemove, n.StableID()) - } - } - if err := b.writePeerDeltaToDiskLocked(peersToUpdate, peersToRemove); err != nil { + if err := b.writePeerDeltaToDiskLocked(peersToUpdate, deltaRes.RemovedPeers); err != nil { b.logf("update netmap cache for peer deltas: %v", err) } } diff --git a/ipn/ipnlocal/local_test.go b/ipn/ipnlocal/local_test.go index b50623e4d..410e0b5bb 100644 --- a/ipn/ipnlocal/local_test.go +++ b/ipn/ipnlocal/local_test.go @@ -621,6 +621,15 @@ func TestUpdateNetMapCache(t *testing.T) { netip.MustParsePrefix("100.2.3.5/32"), }, }).View(), + (&tailcfg.Node{ + ID: 602, + StableID: "n602FAKE", + User: tailcfg.UserID(1), + Key: makeNodeKeyFromID(602), + Addresses: []netip.Prefix{ + netip.MustParsePrefix("100.3.4.6/32"), + }, + }).View(), }, } @@ -688,6 +697,19 @@ func TestUpdateNetMapCache(t *testing.T) { t.Logf("Cache directory has %d entries (OK)", len(des)) } + // Apply a delta update that removes a node, and verify that this gets + // reflected in the cache. + clb.UpdateNetmapDelta([]netmap.NodeMutation{ + netmap.MakeNodeMutationRemove(602), + }) + if got, err := netmapcache.NewCache(netmapcache.FileStore(cacheDir)).Load(t.Context()); err != nil { + t.Errorf("Load cached netmap: %v", err) + } else if i := slices.IndexFunc(got.Peers, func(n tailcfg.NodeView) bool { + return n.ID() == 602 + }); i >= 0 { + t.Errorf("Cache did not get updated, %d (%v) still present", got.Peers[i].ID(), got.Peers[i].StableID()) + } + // Now disable the node attribute again, send another update, and verify // that the cache got cleaned up. testMap.AllCaps = nil diff --git a/ipn/ipnlocal/node_backend.go b/ipn/ipnlocal/node_backend.go index e451405b3..3c91a391b 100644 --- a/ipn/ipnlocal/node_backend.go +++ b/ipn/ipnlocal/node_backend.go @@ -1078,6 +1078,10 @@ type netmapDeltaResult struct { // way that requires a WireGuard session reset (see // [nodeBackend.discoChangedLocked]). DiscoChanged set.Set[key.NodePublic] + + // RemovedPeers is a slice of peer stable node IDs (if any) that were + // removed by applying the delta. + RemovedPeers []tailcfg.StableNodeID } // UpdateNetmapDelta applies the given netmap mutations to the live @@ -1162,6 +1166,7 @@ func (nb *nodeBackend) UpdateNetmapDelta(muts []netmap.NodeMutation) (res netmap nb.removeNodeNameLocked(old.Name()) delete(nb.peers, nid) rt.RemovePeer(nid) + res.RemovedPeers = append(res.RemovedPeers, old.StableID()) } continue }