Close webrtc socket before closing the PeerConnection.

PiperOrigin-RevId: 522140908
This commit is contained in:
hai007
2023-04-05 13:36:30 -07:00
committed by Copybara-Service
parent c919ed73a9
commit 388c96d125
2 changed files with 12 additions and 3 deletions
@@ -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.");
@@ -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";
}