FCE-3224: Fix reconnect race condition#521
Merged
Merged
Conversation
254bf7b to
645ab81
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes reconnect-related state inconsistencies in webrtc-client by keeping transceiver counts aligned with server track counts and ensuring async media-event handlers execute in-order.
Changes:
- Stop excess
recvonlytransceivers (preferring orphanedmid === null) before adding new ones inConnectionManager. - Serialize non-
connectedmedia events inWebRTCEndpointvia a promise queue; refactorconnectedhandling intohandleConnected. - Update/add tests to
await receiveMediaEvent, plus new coverage for transceiver clamping/stopping and media-event ordering.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/webrtc-client/src/webRTCEndpoint.ts | Adds a media-event promise queue and refactors connected handling. |
| packages/webrtc-client/src/ConnectionManager.ts | Adds logic to stop excess recvonly transceivers and clamps deltas. |
| packages/webrtc-client/tests/utils.ts | Makes setupRoom async and awaits media-event processing for deterministic tests. |
| packages/webrtc-client/tests/events/vadNotificationEvent.test.ts | Awaits media events; replaces done patterns with awaited promises. |
| packages/webrtc-client/tests/events/trackUpdatedEvent.test.ts | Awaits media events and modernizes async assertions. |
| packages/webrtc-client/tests/events/trackRemovedEvent.test.ts | Awaits media events and makes event assertions deterministic. |
| packages/webrtc-client/tests/events/trackAddedEvent.test.ts | Awaits media events and makes event assertions deterministic. |
| packages/webrtc-client/tests/events/endpointUpdatedEvent.test.ts | Awaits media events in metadata update paths. |
| packages/webrtc-client/tests/events/endpointRemovedEvent.test.ts | Awaits media events; modernizes rejection assertions and event checks. |
| packages/webrtc-client/tests/events/endpointAddedEvent.test.ts | Awaits media events for state updates. |
| packages/webrtc-client/tests/events/encodingSwitchedEvent.test.ts | Awaits media events and modernizes async assertions. |
| packages/webrtc-client/tests/events/serializedMediaEvents.test.ts | New test ensuring queued media events preserve ordering across async handlers. |
| packages/webrtc-client/tests/addTransceiversIfNeeded.test.ts | New unit tests for add/stop behavior and negative-delta regression coverage. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Prevents Vitest 3 breakage for the two remaining async rejection assertions that were not already awaited. Made-with: Cursor
roznawsk
approved these changes
Apr 24, 2026
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.
Description
ConnectionManager: stop excessrecvonlytransceivers (preferring orphanedmid === null) before adding new ones, and exclude already-stopped transceivers from counting and stop-candidate selection so server-track counts stay in sync after reconnects.webRTCEndpoint: serialize all incoming media events (includingconnected) through a single promise-chain queue with onehandleMediaEvententry point, so async handlers run strictly in order; dedup the duplicate local endpoint metadata parse/set inhandleConnected.await receiveMediaEventin event/method tests; addserializedMediaEvents.test.ts(offer/answer ordering, pre-connect events are dropped, consecutiveconnectedevents stay in order) andaddTransceiversIfNeeded.test.ts(clamping, negative-delta regression, stopped-transceiver counting and stop-candidate exclusion).Motivation and Context
Fixes reconnect-related state drift where stale
recvonlytransceivers accumulated and async server events interleaved, leaving the client and server out of sync.Documentation impact
Types of changes
not work as expected)