fix deadlocks in track subscription

This commit is contained in:
David Zhao
2021-06-02 20:42:44 -07:00
parent b769faaec5
commit 9ca662f06e
2 changed files with 10 additions and 6 deletions
+5 -4
View File
@@ -318,14 +318,12 @@ func (r *Room) OnParticipantChanged(f func(participant types.Participant)) {
r.onParticipantChanged = f
}
// checks if participant should be autosubscribed to new tracks, assumes lock is already acquired
func (r *Room) autoSubscribe(participant types.Participant) bool {
if !participant.CanSubscribe() {
return false
}
r.lock.RLock()
defer r.lock.RUnlock()
opts := r.participantOpts[participant.Identity()]
// default to true if no options are set
if opts != nil && !opts.AutoSubscribe {
@@ -402,7 +400,10 @@ func (r *Room) onDataPacket(source types.Participant, dp *livekit.DataPacket) {
}
func (r *Room) subscribeToExistingTracks(p types.Participant) {
if !r.autoSubscribe(p) {
r.lock.RLock()
shouldSubscribe := r.autoSubscribe(p)
r.lock.RUnlock()
if !shouldSubscribe {
return
}