diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index fd89f7122..be3a380c7 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -207,6 +207,8 @@ type watchSession struct { // this session via [Notify.SelfChange]. A change to self is a profile // boundary for implicit bus state, so per-session dedup state such as // lastSentUserProfile must be reset when this identity changes. + // It is itself reset on a profile change, as the new profile may + // have no self node. lastSentSelf tailcfg.NodeView // policyUID is the user ID used for policy snapshot scoping. @@ -8824,6 +8826,14 @@ func (b *LocalBackend) resetForProfileChangeLocked() error { return nil } b.closeDiskCacheLocked() + // IPN bus watchers may drop the old node's implicit bus state + // on a profile switch, and the new profile may never deliver + // a self node to mark the boundary, such as when the new profile's + // node key has expired. + for _, sess := range b.notifyWatchers { + sess.lastSentUserProfile = nil + sess.lastSentSelf = tailcfg.NodeView{} + } if buildfeatures.HasDNS { if dm, ok := b.sys.DNSManager.GetOK(); ok { dm.Resolver().ClearNegativeCache() diff --git a/ipn/ipnlocal/local_test.go b/ipn/ipnlocal/local_test.go index 1d501c183..09428dc6e 100644 --- a/ipn/ipnlocal/local_test.go +++ b/ipn/ipnlocal/local_test.go @@ -3316,6 +3316,41 @@ func TestNotifyForSessionUserProfilesDedupResetsOnSelfChange(t *testing.T) { } } +// TestNotifyForSessionUserProfilesDedupResetsOnProfileChange tests that +// user profiles are resent after switching to a profile that never gets +// a self node, such as a logged out one, and back. +func TestNotifyForSessionUserProfilesDedupResetsOnProfileChange(t *testing.T) { + b := newTestLocalBackend(t) + + sess := &watchSession{ + sessionID: "test-session", + ctx: t.Context(), + mask: ipn.NotifyPeerChanges, + ch: make(chan *ipn.Notify, 100), + } + self := &tailcfg.Node{ID: 1, StableID: "self", User: 10} + profiles := map[tailcfg.UserID]tailcfg.UserProfileView{ + 10: (&tailcfg.UserProfile{ID: 10, LoginName: "alice@example.com", DisplayName: "Alice"}).View(), + 11: (&tailcfg.UserProfile{ID: 11, LoginName: "tagged-devices", DisplayName: "Tagged Devices"}).View(), + } + + b.mu.Lock() + defer b.mu.Unlock() + mak.Set(&b.notifyWatchers, sess.sessionID, sess) + + n := b.notifyForSessionLocked(sess, &ipn.Notify{SelfChange: self, UserProfiles: profiles}) + if got, want := len(n.UserProfiles), len(profiles); got != want { + t.Fatalf("initial UserProfiles len = %d; want %d", got, want) + } + if err := b.resetForProfileChangeLocked(); err != nil { + t.Fatal(err) + } + n = b.notifyForSessionLocked(sess, &ipn.Notify{SelfChange: self, UserProfiles: profiles}) + if got, want := len(n.UserProfiles), len(profiles); got != want { + t.Fatalf("UserProfiles len after profile change = %d; want %d", got, want) + } +} + // tests LocalBackend.updateNetmapDeltaLocked func TestUpdateNetmapDelta(t *testing.T) { b := newTestLocalBackend(t)