diff --git a/derp/derpserver/derpserver.go b/derp/derpserver/derpserver.go index 4a922947b..343ecefd2 100644 --- a/derp/derpserver/derpserver.go +++ b/derp/derpserver/derpserver.go @@ -324,10 +324,14 @@ type dupClientSet struct { // data since. last *sclient - // sendHistory is a log of which members of set have sent - // frames to the derp server, with adjacent duplicates - // removed. When a member of set is removed, the same - // element(s) are removed from sendHistory. + // sendHistory records which members of set have sent frames to + // the DERP server, ordered from least to most recently active. + // Each member appears at most once: recording a member that is + // already present moves it to the end instead of appending a + // duplicate. That keeps the slice bounded by the size of set. + // Without the bound, two connections sharing a key and taking + // turns sending could grow it without limit. When a member of + // set is removed, it is also removed from sendHistory. sendHistory []*sclient } @@ -1879,7 +1883,14 @@ func (s *Server) noteClientActivity(c *sclient) { } } - // Append this client to the list of clients who spoke last. + // Record c as the most recent sender. If c is already in + // sendHistory, remove the earlier occurrence first so that each + // member appears at most once and the slice stays bounded by the + // number of connections in the set. The LastEqual check above + // already handled the case where c is the current tail. + if i := slices.Index(dup.sendHistory, c); i >= 0 { + dup.sendHistory = slices.Delete(dup.sendHistory, i, i+1) + } dup.sendHistory = append(dup.sendHistory, c) } diff --git a/derp/derpserver/derpserver_test.go b/derp/derpserver/derpserver_test.go index e1beadd8d..d96aa48a0 100644 --- a/derp/derpserver/derpserver_test.go +++ b/derp/derpserver/derpserver_test.go @@ -749,6 +749,78 @@ func TestServerDupClients(t *testing.T) { }) } +// TestServerDupClientSendHistoryBounded verifies that a dup client set's +// sendHistory does not grow without bound when two connections sharing a +// key take turns sending frames. See the sendHistory field comment. +func TestServerDupClientSendHistoryBounded(t *testing.T) { + clientPub := key.NewNode().Public() + + newClient := func(name string) *sclient { + return &sclient{key: clientPub, logf: logger.WithPrefix(t.Logf, name+": ")} + } + + t.Run("last_writer_active", func(t *testing.T) { + s := New(key.NewNode(), t.Logf) + s.dupPolicy = lastWriterIsActive + c1, c2 := newClient("c1"), newClient("c2") + s.registerClient(c1) + s.registerClient(c2) + + cs, _ := s.clients.Load(clientPub) + dup := cs.dup + if dup == nil { + t.Fatal("no dup set") + } + for range 5000 { + s.noteClientActivity(c1) + s.noteClientActivity(c2) + } + if got := len(dup.sendHistory); got > len(dup.set) { + t.Fatalf("sendHistory len = %d; want <= %d (set size)", got, len(dup.set)) + } + // The clients must remain active and undisabled, and the last + // one to send is the active one. + if c1.isDisabled.Load() || c2.isDisabled.Load() { + t.Fatal("client disabled under lastWriterIsActive; want none disabled") + } + if got := cs.activeClient.Load(); got != c2 { + t.Fatalf("active client = %p; want c2 %p", got, c2) + } + + // When the active connection drops, the surviving one must + // still be picked up from the recorded history. + s.unregisterClient(c2) + if got, _ := s.clients.Load(clientPub); got.activeClient.Load() != c1 { + t.Fatalf("after dropping c2, active = %p; want c1 %p", got.activeClient.Load(), c1) + } + }) + + t.Run("disable_fighters", func(t *testing.T) { + s := New(key.NewNode(), t.Logf) + s.dupPolicy = disableFighters + c1, c2 := newClient("c1"), newClient("c2") + s.registerClient(c1) + s.registerClient(c2) + + cs, _ := s.clients.Load(clientPub) + dup := cs.dup + if dup == nil { + t.Fatal("no dup set") + } + for range 5000 { + s.noteClientActivity(c1) + s.noteClientActivity(c2) + } + if got := len(dup.sendHistory); got > len(dup.set) { + t.Fatalf("sendHistory len = %d; want <= %d (set size)", got, len(dup.set)) + } + // Fighting connections must be disabled. + if !c1.isDisabled.Load() || !c2.isDisabled.Load() { + t.Fatal("fighting clients not disabled; want both disabled") + } + }) +} + func TestLimiter(t *testing.T) { rl := rate.NewLimiter(rate.Every(time.Minute), 100) for i := range 200 {