From 2d13e2a5d1531a4a39d0089dfb89d5f690a8456b Mon Sep 17 00:00:00 2001 From: WofWca Date: Sat, 7 Sep 2024 15:35:49 +0400 Subject: [PATCH] fix(probetest): maybe resource leak ...on failed requests: WebRTC connection wouldn't get closed in such cases. --- probetest/probetest.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/probetest/probetest.go b/probetest/probetest.go index 944fb9b..3c7c70f 100644 --- a/probetest/probetest.go +++ b/probetest/probetest.go @@ -141,6 +141,16 @@ func probeHandler(stunURL string, w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) return } + // We'll set this to `false` if the signaling (this function) succeeds. + closePcOnReturn := true + defer func() { + if closePcOnReturn { + if err := pc.Close(); err != nil { + log.Printf("Error calling pc.Close: %v", err) + } + } + // Otherwise it must be closed below, wherever `closePcOnReturn` is set to `false`. + }() sdp = &webrtc.SessionDescription{ Type: pc.LocalDescription().Type, @@ -163,6 +173,7 @@ func probeHandler(stunURL string, w http.ResponseWriter, r *http.Request) { // Set a timeout on peerconnection. If the connection state has not // advanced to PeerConnectionStateConnected in this time, // destroy the peer connection and return the token. + closePcOnReturn = false go func() { timer := time.NewTimer(dataChannelTimeout) defer timer.Stop()