From f8d2bb93e84eeb5e60a1b096bdb075fc52ad9e5d Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Tue, 27 Oct 2020 18:20:09 +0100 Subject: [PATCH] Avoid deadlock in DelLocal. --- rtpconn/rtpconn.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/rtpconn/rtpconn.go b/rtpconn/rtpconn.go index b48e09a..b2ecaa7 100644 --- a/rtpconn/rtpconn.go +++ b/rtpconn/rtpconn.go @@ -211,30 +211,33 @@ func (up *rtpUpTrack) notifyLocal(add bool, track conn.DownTrack) { func (up *rtpUpTrack) AddLocal(local conn.DownTrack) error { up.mu.Lock() + defer up.mu.Unlock() + for _, t := range up.local { if t == local { - up.mu.Unlock() return nil } } up.local = append(up.local, local) - up.mu.Unlock() - up.notifyLocal(true, local) + // do this asynchronously, to avoid deadlocks when multiple + // clients call this simultaneously. + go up.notifyLocal(true, local) return nil } func (up *rtpUpTrack) DelLocal(local conn.DownTrack) bool { up.mu.Lock() + defer up.mu.Unlock() for i, l := range up.local { if l == local { up.local = append(up.local[:i], up.local[i+1:]...) - up.mu.Unlock() - up.notifyLocal(false, l) + // do this asynchronously, to avoid deadlocking when + // multiple clients call this simultaneously. + go up.notifyLocal(false, l) return true } } - up.mu.Unlock() return false }