Fix: Main thread blocked for ~1s during WebRTC connection setup #834
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🎯 Goal
When joining a call, especially the first call after app launch, the UI freezes for approximately 1 second. This creates a poor user experience with an unresponsive interface.
📝 Summary
Root Cause
setLocalDescriptionandsetRemoteDescriptionare marked with@MainActorInstrumentation Graphs
See here there is a 1 second hang in instrumentation:

WebRTC Code Trace
setLocalDescriptionis defined inRTCPeerConnection: https://github.com/GetStream/webrtc/blob/main/sdk/objc/api/peerconnection/RTCPeerConnection.mm#L595-L601_peerConnectionis artc::scoped_refptr<webrtc::PeerConnectionInterface>, with it's concreteSetLocalDescriptiondefined here: https://github.com/GetStream/webrtc/blob/main/pc/peer_connection.cc#L1494-L1499sdp_handler_is aSdpOfferAnswerHandler, with it'sSetLocalDescriptiondefined here: https://github.com/GetStream/webrtc/blob/main/pc/sdp_offer_answer.cc#L1555-L1580DoSetLocalDescription(https://github.com/GetStream/webrtc/blob/main/pc/sdp_offer_answer.cc#L1542C24-L1545) which then invokesPushdownTransportDescription(https://github.com/GetStream/webrtc/blob/main/pc/sdp_offer_answer.cc#L1696)PushdownTransportDescriptionfinally callsSetLocalDescriptionon thetransport_controller_swhich is aJsepTransportController: https://github.com/GetStream/webrtc/blob/main/pc/sdp_offer_answer.cc#L4909-L4910🛠 Implementation
@MainActorfrom both methods to allow them to run on any threadWhy Queue Serialization is Necessary
Removing
@MainActoralone introduces race conditions when multiple threads access the RTCPeerConnection simultaneously. Manual testing showed:The queue maintains the thread safety that
@MainActorprovided, but without blocking the main thread.🧪 Manual Testing Notes
Testing Results (10 attempts each):
☑️ Contributor Checklist