diff --git a/interceptor_test.go b/interceptor_test.go index aff8b67573c..a2484c27387 100644 --- a/interceptor_test.go +++ b/interceptor_test.go @@ -187,7 +187,8 @@ func Test_Interceptor_BindUnbind(t *testing.T) { } }() - closePairNow(t, sender, receiver) + assert.NoError(t, sender.GracefulClose()) + assert.NoError(t, receiver.GracefulClose()) // Bind/UnbindLocal/RemoteStream should be called from one side. if cnt := atomic.LoadUint32(&cntBindLocalStream); cnt != 1 { diff --git a/peerconnection.go b/peerconnection.go index c175e5e49af..6c518013fd0 100644 --- a/peerconnection.go +++ b/peerconnection.go @@ -2209,8 +2209,6 @@ func (pc *PeerConnection) close(shouldGracefullyClose bool) error { // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #3) pc.signalingState.Set(SignalingStateClosed) - closeErrs = append(closeErrs, pc.api.interceptor.Close()) - // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-close (step #4) pc.mu.Lock() for _, t := range pc.rtpTransceivers { @@ -2247,6 +2245,9 @@ func (pc *PeerConnection) close(shouldGracefullyClose bool) error { closeErrs = append(closeErrs, doGracefulCloseOps()...) + // Interceptor closes at the end to prevent Bind from being called after interceptor is closed + closeErrs = append(closeErrs, pc.api.interceptor.Close()) + return util.FlattenErrs(closeErrs) }