From a3cf5f4c2d27831ed1f8c43941ae79f4ff35fd41 Mon Sep 17 00:00:00 2001 From: Benjamin Pracht Date: Fri, 15 Nov 2024 09:50:35 -0800 Subject: [PATCH] Do not attempt to send an offer if the PCTransport was closed (#562) --- engine.go | 15 +++++++++++++++ errors.go | 1 + transport.go | 11 +++++++++++ 3 files changed, 27 insertions(+) diff --git a/engine.go b/engine.go index 5d899cda..ee6acae2 100644 --- a/engine.go +++ b/engine.go @@ -368,6 +368,11 @@ func (e *RTCEngine) configure( // configure client e.client.OnAnswer = func(sd webrtc.SessionDescription) { + if e.closed.Load() { + e.log.Debugw("ignoring SDP answer after closed") + return + } + if err := e.publisher.SetRemoteDescription(sd); err != nil { e.log.Errorw("could not set remote description", err) } else { @@ -375,6 +380,11 @@ func (e *RTCEngine) configure( } } e.client.OnTrickle = func(init webrtc.ICECandidateInit, target livekit.SignalTarget) { + if e.closed.Load() { + e.log.Debugw("ignoring trickle after closed") + return + } + var err error e.log.Debugw("remote ICE candidate", "target", target, @@ -390,6 +400,11 @@ func (e *RTCEngine) configure( } } e.client.OnOffer = func(sd webrtc.SessionDescription) { + if e.closed.Load() { + e.log.Debugw("ignoring SDP offer after closed") + return + } + e.log.Debugw("received offer for subscriber") if err := e.subscriber.SetRemoteDescription(sd); err != nil { e.log.Errorw("could not set remote description", err) diff --git a/errors.go b/errors.go index 1315a00b..3151d2a0 100644 --- a/errors.go +++ b/errors.go @@ -29,4 +29,5 @@ var ( ErrCannotConnectSignal = errors.New("could not establish signal connection") ErrCannotDialSignal = errors.New("could not dial signal connection") ErrNoPeerConnection = errors.New("peer connection not established") + ErrAborted = errors.New("operation was aborted") ) diff --git a/transport.go b/transport.go index 15a99670..16bbe3c1 100644 --- a/transport.go +++ b/transport.go @@ -59,6 +59,7 @@ type PCTransport struct { pendingRestartIceOffer *webrtc.SessionDescription restartAfterGathering bool nackGenerator *sdkinterceptor.NackGeneratorInterceptorFactory + closed bool rttFromXR atomic.Bool onRemoteDescriptionSettled func() error @@ -257,6 +258,11 @@ func (t *PCTransport) IsConnected() bool { } func (t *PCTransport) Close() error { + t.lock.Lock() + defer t.lock.Unlock() + + t.closed = true + return t.pc.Close() } @@ -381,6 +387,11 @@ func (t *PCTransport) createAndSendOffer(options *webrtc.OfferOptions) error { t.lock.Lock() defer t.lock.Unlock() + if t.closed { + t.log.Debugw("aborting Offer since transport is closed") + return ErrAborted + } + iceRestart := options != nil && options.ICERestart if iceRestart { if t.pc.ICEGatheringState() == webrtc.ICEGatheringStateGathering {