From f0d71e97d7d43b03658fb30949fbdcf7917953d7 Mon Sep 17 00:00:00 2001 From: Tarrence van As Date: Thu, 21 Jan 2021 10:30:34 -0500 Subject: [PATCH] fix: unbind handlers on close (#387) --- .gitignore | 2 ++ go.sum | 1 + pkg/sfu/peer.go | 7 +++++++ pkg/sfu/router.go | 3 +++ 4 files changed, 13 insertions(+) diff --git a/.gitignore b/.gitignore index 7652436fa..18b217651 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ vendor *.pem bin *.generated.go +.stignore +okteto.yml diff --git a/go.sum b/go.sum index 5aa313ed2..87c87f985 100644 --- a/go.sum +++ b/go.sum @@ -427,6 +427,7 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= diff --git a/pkg/sfu/peer.go b/pkg/sfu/peer.go index 4d6798b7e..1849317b7 100644 --- a/pkg/sfu/peer.go +++ b/pkg/sfu/peer.go @@ -215,6 +215,13 @@ func (p *Peer) Trickle(candidate webrtc.ICECandidateInit, target int) error { // Close shuts down the peer connection and sends true to the done channel func (p *Peer) Close() error { + p.Lock() + defer p.Unlock() + + p.OnOffer = nil + p.OnIceCandidate = nil + p.OnICEConnectionStateChange = nil + if p.session != nil { p.session.RemovePeer(p.id) } diff --git a/pkg/sfu/router.go b/pkg/sfu/router.go index bb62313ac..b34418cb8 100644 --- a/pkg/sfu/router.go +++ b/pkg/sfu/router.go @@ -226,6 +226,9 @@ func (r *router) addDownTrack(sub *Subscriber, recv Receiver) error { downTrack.OnCloseHandler(func() { if sub.pc.ConnectionState() != webrtc.PeerConnectionStateClosed { if err := sub.pc.RemoveTrack(downTrack.transceiver.Sender()); err != nil { + if err == webrtc.ErrConnectionClosed { + return + } log.Errorf("Error closing down track: %v", err) } else { sub.RemoveDownTrack(recv.StreamID(), downTrack)