From 11d76f62213f50540c280253fc0c5dc8efe31b59 Mon Sep 17 00:00:00 2001 From: Lee Bousfield Date: Thu, 21 Dec 2023 10:21:31 -0700 Subject: [PATCH] Fix broadcastSegment Contains and its usage --- broadcaster/backlog/backlog.go | 2 +- wsbroadcastserver/clientconnection.go | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/broadcaster/backlog/backlog.go b/broadcaster/backlog/backlog.go index 851561f482..0ef32167fe 100644 --- a/broadcaster/backlog/backlog.go +++ b/broadcaster/backlog/backlog.go @@ -379,7 +379,7 @@ func (s *backlogSegment) Contains(i uint64) bool { s.messagesLock.RLock() defer s.messagesLock.RUnlock() start := s.start() - if i < start || i > s.end() { + if i < start || i > s.end() || len(s.messages) == 0 { return false } diff --git a/wsbroadcastserver/clientconnection.go b/wsbroadcastserver/clientconnection.go index 49cd2af7e6..5e8763bd95 100644 --- a/wsbroadcastserver/clientconnection.go +++ b/wsbroadcastserver/clientconnection.go @@ -134,7 +134,10 @@ func (cc *ClientConnection) writeBacklog(ctx context.Context, segment backlog.Ba msgs := prevSegment.Messages() if prevSegment.Contains(uint64(cc.requestedSeqNum)) { requestedIdx := int(cc.requestedSeqNum) - int(prevSegment.Start()) - msgs = msgs[requestedIdx:] + // This might be false if messages were added after we fetched the segment's messages + if len(msgs) > requestedIdx { + msgs = msgs[requestedIdx:] + } } bm := &m.BroadcastMessage{ Version: m.V1,