From 388c96d125e5c88f58f3b0364703d5600867ea9a Mon Sep 17 00:00:00 2001 From: hai007 Date: Wed, 5 Apr 2023 13:35:34 -0700 Subject: [PATCH] Close webrtc socket before closing the PeerConnection. PiperOrigin-RevId: 522140908 --- .../implementation/mediums/webrtc/connection_flow.cc | 7 ++++++- .../implementation/mediums/webrtc/webrtc_socket_impl.cc | 8 ++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/connections/implementation/mediums/webrtc/connection_flow.cc b/connections/implementation/mediums/webrtc/connection_flow.cc index 46f435a6..1dc62f1d 100644 --- a/connections/implementation/mediums/webrtc/connection_flow.cc +++ b/connections/implementation/mediums/webrtc/connection_flow.cc @@ -495,12 +495,17 @@ bool ConnectionFlow::CloseOnSignalingThread() { return false; } state_ = State::kEnded; + // Close the socket wrapper before terminating the PeerConnection + // since the teardown process of the PC may close threads that are + // otherwise depended upon by objects kept alive by the socket_wrapper. + if (socket_wrapper_.IsValid()) socket_wrapper_.Close(); + // This prevents other tasks from queuing on the signaling thread for this // object. auto pc = GetAndResetPeerConnection(); NEARBY_LOG(INFO, "Closing WebRTC peer connection."); - // NOTE: Closing the peer conection will close the data channel and thus the + // NOTE: Closing the peer connection will close the data channel and thus the // socket implicitly. if (pc) pc->Close(); NEARBY_LOG(INFO, "Closed WebRTC peer connection."); diff --git a/connections/implementation/mediums/webrtc/webrtc_socket_impl.cc b/connections/implementation/mediums/webrtc/webrtc_socket_impl.cc index e4a75f7f..63e66752 100644 --- a/connections/implementation/mediums/webrtc/webrtc_socket_impl.cc +++ b/connections/implementation/mediums/webrtc/webrtc_socket_impl.cc @@ -67,8 +67,12 @@ WebRtcSocket::WebRtcSocket( WebRtcSocket::~WebRtcSocket() { NEARBY_LOGS(INFO) << "WebRtcSocket::~WebRtcSocket(" << name_ << ") this: " << this; - data_channel_->UnregisterObserver(); - Close(); + + if (!IsClosed()) { + data_channel_->UnregisterObserver(); + Close(); + } + NEARBY_LOGS(INFO) << "WebRtcSocket::~WebRtcSocket(" << name_ << ") this: " << this << " done"; }