From 64b8fd355464ae5e02a3b8e1f05f6765c11fc394 Mon Sep 17 00:00:00 2001 From: Alessandro Ros Date: Tue, 21 Feb 2023 23:35:22 +0100 Subject: [PATCH] webrtc: fix crash during client disconnection (#1482) (#1501) OnConnectionStateChange of pion/webrtc is not thread safe. Add a mutex to make it thread safe. --- internal/core/webrtc_conn.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/core/webrtc_conn.go b/internal/core/webrtc_conn.go index 904ea11d..8daf437e 100644 --- a/internal/core/webrtc_conn.go +++ b/internal/core/webrtc_conn.go @@ -345,8 +345,12 @@ func (c *webRTCConn) runInner(ctx context.Context) error { pcConnected := make(chan struct{}) pcDisconnected := make(chan struct{}) pcClosed := make(chan struct{}) + var stateChangeMutex sync.Mutex pc.OnConnectionStateChange(func(state webrtc.PeerConnectionState) { + stateChangeMutex.Lock() + defer stateChangeMutex.Unlock() + select { case <-pcClosed: return