fix(probetest): maybe resource leak

...on failed requests: WebRTC connection wouldn't get
closed in such cases.
This commit is contained in:
WofWca 2024-09-07 15:35:49 +04:00 committed by Shelikhoo
parent 51edbbfd26
commit 2d13e2a5d1
No known key found for this signature in database
GPG key ID: 4C9764E9FE80A3DC

View file

@ -141,6 +141,16 @@ func probeHandler(stunURL string, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return 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{ sdp = &webrtc.SessionDescription{
Type: pc.LocalDescription().Type, 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 // Set a timeout on peerconnection. If the connection state has not
// advanced to PeerConnectionStateConnected in this time, // advanced to PeerConnectionStateConnected in this time,
// destroy the peer connection and return the token. // destroy the peer connection and return the token.
closePcOnReturn = false
go func() { go func() {
timer := time.NewTimer(dataChannelTimeout) timer := time.NewTimer(dataChannelTimeout)
defer timer.Stop() defer timer.Stop()