Fix: Main thread blocked for ~1s during WebRTC connection setup#834
Open
sampepose wants to merge 2 commits intoGetStream:developfrom
Open
Fix: Main thread blocked for ~1s during WebRTC connection setup#834sampepose wants to merge 2 commits intoGetStream:developfrom
sampepose wants to merge 2 commits intoGetStream:developfrom
Conversation
- Remove @mainactor from setLocalDescription and setRemoteDescription - Fixes ~1s UI freeze when joining calls after app launch
- Add connectionQueue to serialize all RTCPeerConnection access - Prevent 968ms UI hangs by moving WebRTC operations off main thread - Maintain HasRemoteDescription event for proper video flow - Fix thread safety for addTransceiver, restartIce, and close operations
75c89ae to
322e4e4
Compare
Contributor
|
Hey @sampepose, thank you for this PR. We are currently in the process of resolving many performance issues (you can take see a sneak peek in the open PRs). We are now in a state where we have resolved all the hungs and for that reason I'm not going to accept this PR. I'm going to keep open though as I reminder to check again once the changes have been merged. Thanks, |
Author
|
I don't think this is addressed with #825 since it still calls these WebRTC functions on the MainActor, blocking it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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