diff --git a/.changeset/session_description_signaling_thread.md b/.changeset/session_description_signaling_thread.md new file mode 100644 index 000000000..bd61a1a53 --- /dev/null +++ b/.changeset/session_description_signaling_thread.md @@ -0,0 +1,5 @@ +--- +webrtc-sys: patch +--- + +Copy peer connection session descriptions on the WebRTC signaling thread to prevent crashes when a description is replaced concurrently. diff --git a/libwebrtc/src/peer_connection.rs b/libwebrtc/src/peer_connection.rs index 704ca84ac..8c6950695 100644 --- a/libwebrtc/src/peer_connection.rs +++ b/libwebrtc/src/peer_connection.rs @@ -330,7 +330,7 @@ mod tests { use log::trace; use tokio::sync::mpsc; - use crate::{peer_connection::*, peer_connection_factory::*}; + use crate::{peer_connection::*, peer_connection_factory::*, session_description::SdpType}; #[tokio::test] async fn create_pc() { @@ -350,6 +350,8 @@ mod tests { let bob = factory.create_peer_connection(config.clone()).unwrap(); let alice = factory.create_peer_connection(config.clone()).unwrap(); + assert!(bob.current_local_description().is_none()); + assert!(bob.current_remote_description().is_none()); let (bob_ice_tx, mut bob_ice_rx) = mpsc::unbounded_channel::(); let (alice_ice_tx, mut alice_ice_rx) = mpsc::unbounded_channel::(); @@ -378,6 +380,8 @@ mod tests { trace!("Alice answer: {:?}", answer); alice.set_local_description(answer.clone()).await.unwrap(); bob.set_remote_description(answer).await.unwrap(); + let local_description = bob.current_local_description().unwrap(); + let remote_description = bob.current_remote_description().unwrap(); let bob_ice = bob_ice_rx.recv().await.unwrap(); let alice_ice = alice_ice_rx.recv().await.unwrap(); @@ -396,5 +400,8 @@ mod tests { alice.close(); bob.close(); + drop(bob); + assert_eq!(local_description.sdp_type(), SdpType::Offer); + assert_eq!(remote_description.sdp_type(), SdpType::Answer); } } diff --git a/webrtc-sys/src/peer_connection.cpp b/webrtc-sys/src/peer_connection.cpp index 479620968..83a2f0ae9 100644 --- a/webrtc-sys/src/peer_connection.cpp +++ b/webrtc-sys/src/peer_connection.cpp @@ -287,56 +287,64 @@ rust::Vec PeerConnection::get_transceivers() const { return vec; } +namespace { + +using DescriptionGetter = const webrtc::SessionDescriptionInterface* ( + webrtc::PeerConnectionInterface::*)() const; + +std::unique_ptr clone_session_description( + webrtc::Thread* signaling_thread, + const webrtc::PeerConnectionInterface* peer_connection, + DescriptionGetter getter) { + // WebRTC may replace the borrowed description on the signaling thread. + return signaling_thread->BlockingCall([&] { + const auto* description = (peer_connection->*getter)(); + return description + ? std::make_unique(description->Clone()) + : nullptr; + }); +} + +} // namespace + std::unique_ptr PeerConnection::current_local_description() const { - auto local_description = peer_connection_->current_local_description(); - if (local_description) - return std::make_unique(local_description->Clone()); - - return nullptr; + return clone_session_description( + rtc_runtime_->signaling_thread(), peer_connection_.get(), + &webrtc::PeerConnectionInterface::current_local_description); } std::unique_ptr PeerConnection::current_remote_description() const { - auto remote_description = peer_connection_->current_remote_description(); - if (remote_description) - return std::make_unique(remote_description->Clone()); - - return nullptr; + return clone_session_description( + rtc_runtime_->signaling_thread(), peer_connection_.get(), + &webrtc::PeerConnectionInterface::current_remote_description); } std::unique_ptr PeerConnection::pending_local_description() const { - auto local_description = peer_connection_->pending_local_description(); - if (local_description) - return std::make_unique(local_description->Clone()); - - return nullptr; + return clone_session_description( + rtc_runtime_->signaling_thread(), peer_connection_.get(), + &webrtc::PeerConnectionInterface::pending_local_description); } std::unique_ptr PeerConnection::pending_remote_description() const { - auto remote_description = peer_connection_->pending_remote_description(); - if (remote_description) - return std::make_unique(remote_description->Clone()); - - return nullptr; + return clone_session_description( + rtc_runtime_->signaling_thread(), peer_connection_.get(), + &webrtc::PeerConnectionInterface::pending_remote_description); } std::unique_ptr PeerConnection::local_description() const { - auto local_description = peer_connection_->local_description(); - if (local_description) - return std::make_unique(local_description->Clone()); - - return nullptr; + return clone_session_description( + rtc_runtime_->signaling_thread(), peer_connection_.get(), + &webrtc::PeerConnectionInterface::local_description); } std::unique_ptr PeerConnection::remote_description() const { - auto remote_description = peer_connection_->remote_description(); - if (remote_description) - return std::make_unique(remote_description->Clone()); - - return nullptr; + return clone_session_description( + rtc_runtime_->signaling_thread(), peer_connection_.get(), + &webrtc::PeerConnectionInterface::remote_description); } PeerConnectionState PeerConnection::connection_state() const {