fixed deadlock when client disconnecting

This commit is contained in:
David Zhao
2020-12-05 22:20:13 -08:00
parent 71a7914eec
commit 2b9fee45c4
4 changed files with 30 additions and 12 deletions
+7 -4
View File
@@ -106,10 +106,13 @@ func (r *Room) RemoveParticipant(id string) {
defer r.lock.Unlock() defer r.lock.Unlock()
if p, ok := r.participants[id]; ok { if p, ok := r.participants[id]; ok {
// also stop connection if needed // avoid blocking lock
p.Close() go func() {
// update clients // also stop connection if needed
r.broadcastParticipantState(p) p.Close()
// update clients
r.broadcastParticipantState(p)
}()
} }
delete(r.participants, id) delete(r.participants, id)
+7 -4
View File
@@ -6,6 +6,7 @@ import (
"time" "time"
"github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/pkg/rtcerr"
"github.com/livekit/livekit-server/pkg/logger" "github.com/livekit/livekit-server/pkg/logger"
"github.com/livekit/livekit-server/pkg/utils" "github.com/livekit/livekit-server/pkg/utils"
@@ -72,7 +73,7 @@ func (t *Track) AddSubscriber(participant *Participant) error {
} }
// pack ID to identify all tracks // pack ID to identify all tracks
packedId := PackTrackId(t.participantId, t.mediaTrack.ID()) packedId := PackTrackId(t.participantId, t.id)
// use existing SSRC with simple forwarders. adaptive forwarders require unique SSRC per layer // use existing SSRC with simple forwarders. adaptive forwarders require unique SSRC per layer
outTrack, err := participant.peerConn.NewTrack(codecs[0].PayloadType, t.mediaTrack.SSRC(), packedId, t.mediaTrack.Label()) outTrack, err := participant.peerConn.NewTrack(codecs[0].PayloadType, t.mediaTrack.SSRC(), packedId, t.mediaTrack.Label())
@@ -95,9 +96,11 @@ func (t *Track) AddSubscriber(participant *Participant) error {
return return
} }
if err := participant.peerConn.RemoveTrack(rtpSender); err != nil { if err := participant.peerConn.RemoveTrack(rtpSender); err != nil {
logger.GetLogger().Warnw("could not remove mediaTrack from forwarder", if _, ok := err.(*rtcerr.InvalidStateError); !ok {
"participant", participant.ID(), logger.GetLogger().Warnw("could not remove mediaTrack from forwarder",
"err", err) "participant", participant.ID(),
"err", err)
}
} }
}) })
+6
View File
@@ -1,6 +1,8 @@
package rtc package rtc
import ( import (
"sync"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
@@ -16,12 +18,14 @@ type SignalConnection interface {
type WSSignalConnection struct { type WSSignalConnection struct {
conn *websocket.Conn conn *websocket.Conn
mu sync.Mutex
useJSON bool useJSON bool
} }
func NewWSSignalConnection(conn *websocket.Conn) *WSSignalConnection { func NewWSSignalConnection(conn *websocket.Conn) *WSSignalConnection {
return &WSSignalConnection{ return &WSSignalConnection{
conn: conn, conn: conn,
mu: sync.Mutex{},
useJSON: true, useJSON: true,
} }
} }
@@ -71,5 +75,7 @@ func (c *WSSignalConnection) WriteResponse(msg *livekit.SignalResponse) error {
return err return err
} }
c.mu.Lock()
defer c.mu.Unlock()
return c.conn.WriteMessage(msgType, payload) return c.conn.WriteMessage(msgType, payload)
} }
+10 -4
View File
@@ -2,6 +2,7 @@ package service
import ( import (
"encoding/json" "encoding/json"
"io"
"net/http" "net/http"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
@@ -95,11 +96,16 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
//ctx := context.Background() //ctx := context.Background()
for { for {
req, err := signalConn.ReadRequest() req, err := signalConn.ReadRequest()
if err == io.EOF {
// client disconnected from websocket
return
}
if err != nil { if err != nil {
logger.GetLogger().Errorw("error reading WS", // most of these are disconnection, just return vs clogging up logs
"err", err, //logger.GetLogger().Errorw("error reading WS",
"participantName", pName, // "err", err,
"roomId", roomId) // "participantName", pName,
// "roomId", roomId)
return return
} }