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 }