Guard against ParticipantUpdate sent before JoinResponse (#1271)

* Guard against ParticipantUpdate sent before JoinResponse
This commit is contained in:
David Zhao
2022-12-29 01:08:08 -08:00
committed by GitHub
parent 12931f20fd
commit 1cffa98311
6 changed files with 40 additions and 38 deletions
+5 -6
View File
@@ -909,13 +909,17 @@ func (p *ParticipantImpl) UpdateSubscribedTrackSettings(trackID livekit.TrackID,
}
func (p *ParticipantImpl) VerifySubscribeParticipantInfo(pID livekit.ParticipantID, version uint32) {
if !p.IsReady() {
// we have not sent a JoinResponse yet. metadata would be covered in JoinResponse
return
}
if v, ok := p.updateCache.Get(pID); ok && v >= version {
return
}
if f := p.params.GetParticipantInfo; f != nil {
if info := f(pID); info != nil {
p.SendParticipantUpdate([]*livekit.ParticipantInfo{info})
_ = p.SendParticipantUpdate([]*livekit.ParticipantInfo{info})
}
}
}
@@ -1187,11 +1191,6 @@ func (p *ParticipantImpl) updateState(state livekit.ParticipantInfo_State) {
// when the server has an offer for participant
func (p *ParticipantImpl) onSubscriberOffer(offer webrtc.SessionDescription) error {
if p.State() == livekit.ParticipantInfo_DISCONNECTED {
// skip when disconnected
return nil
}
p.params.Logger.Infow("sending offer", "transport", livekit.SignalTarget_SUBSCRIBER)
return p.writeMessage(&livekit.SignalResponse{
Message: &livekit.SignalResponse_Offer{
+2 -1
View File
@@ -58,7 +58,6 @@ func TestIsReady(t *testing.T) {
func TestTrackPublishing(t *testing.T) {
t.Run("should send the correct events", func(t *testing.T) {
p := newParticipantForTest("test")
p.state.Store(livekit.ParticipantInfo_ACTIVE)
track := &typesfakes.FakeMediaTrack{}
track.IDReturns("id")
published := false
@@ -184,6 +183,7 @@ func TestTrackPublishing(t *testing.T) {
func TestOutOfOrderUpdates(t *testing.T) {
p := newParticipantForTest("test")
p.updateState(livekit.ParticipantInfo_JOINED)
p.SetMetadata("initial metadata")
sink := p.getResponseSink().(*routingfakes.FakeMessageSink)
pi1 := p.ToProto()
@@ -483,6 +483,7 @@ func newParticipantForTestWithOpts(identity livekit.ParticipantIdentity, opts *p
Logger: LoggerWithParticipant(logger.GetLogger(), identity, sid, false),
})
p.isPublisher.Store(opts.publisher)
p.updateState(livekit.ParticipantInfo_ACTIVE)
return p
}
+6 -1
View File
@@ -30,6 +30,7 @@ func (p *ParticipantImpl) SetResponseSink(sink routing.MessageSink) {
}
func (p *ParticipantImpl) SendJoinResponse(joinResponse *livekit.JoinResponse) error {
// update state prior to sending message, or the message would not be sent
if p.State() == livekit.ParticipantInfo_JOINING {
p.updateState(livekit.ParticipantInfo_JOINED)
}
@@ -43,6 +44,10 @@ func (p *ParticipantImpl) SendJoinResponse(joinResponse *livekit.JoinResponse) e
}
func (p *ParticipantImpl) SendParticipantUpdate(participantsToUpdate []*livekit.ParticipantInfo) error {
if !p.IsReady() {
// avoid manipulating cache before it's ready
return nil
}
p.updateLock.Lock()
validUpdates := make([]*livekit.ParticipantInfo, 0, len(participantsToUpdate))
for _, pi := range participantsToUpdate {
@@ -173,7 +178,7 @@ func (p *ParticipantImpl) sendTrackUnpublished(trackID livekit.TrackID) {
}
func (p *ParticipantImpl) writeMessage(msg *livekit.SignalResponse) error {
if p.State() == livekit.ParticipantInfo_DISCONNECTED {
if !p.IsReady() {
return nil
}