fixed panic when client connected abruptly

This commit is contained in:
David Zhao
2021-01-26 17:38:47 -08:00
parent b96028b4d6
commit a065a01592
10 changed files with 54 additions and 10 deletions
+5 -1
View File
@@ -320,7 +320,6 @@ func (p *ParticipantImpl) Close() error {
if p.ctx.Err() != nil {
return p.ctx.Err()
}
close(p.rtcpCh)
p.onICECandidate = nil
p.peerConn.OnDataChannel(nil)
p.peerConn.OnICECandidate(nil)
@@ -332,6 +331,7 @@ func (p *ParticipantImpl) Close() error {
p.onClose(p)
}
p.cancel()
close(p.rtcpCh)
return p.peerConn.Close()
}
@@ -434,6 +434,10 @@ func (p *ParticipantImpl) scheduleNegotiate() {
// initiates server-driven negotiation by creating an offer
func (p *ParticipantImpl) negotiate() {
if p.state == livekit.ParticipantInfo_DISCONNECTED {
// skip when disconnected
return
}
p.negotiationCond.L.Lock()
for p.negotiationState != negotiationStateNone {
p.negotiationCond.Wait()
+21
View File
@@ -5,6 +5,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/livekit/livekit-server/pkg/routing"
"github.com/livekit/livekit-server/pkg/routing/routingfakes"
"github.com/livekit/livekit-server/pkg/rtc/types"
"github.com/livekit/livekit-server/pkg/rtc/types/typesfakes"
@@ -68,6 +69,26 @@ func TestTrackPublishEvents(t *testing.T) {
assert.True(t, updated)
}
// after disconnection, things should continue to function and not panic
func TestDisconnectTiming(t *testing.T) {
t.Run("negotiate doesn't fail after channel closed", func(t *testing.T) {
p := newParticipantForTest("test")
msg := routing.NewMessageChannel()
p.responseSink = msg
go func() {
for msg := range msg.ReadChan() {
t.Log("received message from chan", msg)
}
}()
track := &typesfakes.FakePublishedTrack{}
p.handleTrackPublished(track)
// close channel and then try to negotiate
msg.Close()
p.negotiate()
})
}
func newParticipantForTest(name string) *ParticipantImpl {
p, _ := NewParticipant(
utils.NewGuid(utils.ParticipantPrefix),
+2 -2
View File
@@ -194,8 +194,8 @@ func (r *Room) broadcastParticipantState(p types.Participant) {
updates := ToProtoParticipants([]types.Participant{p})
for _, op := range r.participants {
// skip itself
if p.ID() == op.ID() {
// skip itself && closed participants
if p.ID() == op.ID() || p.State() == livekit.ParticipantInfo_DISCONNECTED {
continue
}
+7 -2
View File
@@ -56,22 +56,27 @@ func TestRoomJoin(t *testing.T) {
})
t.Run("participant state change is broadcasted to others", func(t *testing.T) {
rm := newRoomWithParticipants(t, 1)
rm := newRoomWithParticipants(t, numParticipants)
participants := rm.GetParticipants()
p := participants[0].(*typesfakes.FakeParticipant)
disconnectedParticipant := participants[1].(*typesfakes.FakeParticipant)
disconnectedParticipant.StateReturns(livekit.ParticipantInfo_DISCONNECTED)
rm.RemoveParticipant(p.ID())
p.OnStateChangeArgsForCall(0)(p, livekit.ParticipantInfo_ACTIVE)
time.Sleep(defaultDelay)
numUpdates := 0
for _, op := range participants {
if op == p {
if op == p || op == disconnectedParticipant {
assert.Zero(t, p.SendParticipantUpdateCallCount())
continue
}
fakeP := op.(*typesfakes.FakeParticipant)
assert.Equal(t, 1, fakeP.SendParticipantUpdateCallCount())
numUpdates += 1
}
assert.Equal(t, numParticipants-2, numUpdates)
})
t.Run("cannot exceed max participants", func(t *testing.T) {