Skip to content

FCE-3224: Fix reconnect race condition#521

Merged
MiloszFilimowski merged 10 commits into
mainfrom
mfilimowski/fix-reconnect
Apr 24, 2026
Merged

FCE-3224: Fix reconnect race condition#521
MiloszFilimowski merged 10 commits into
mainfrom
mfilimowski/fix-reconnect

Conversation

@MiloszFilimowski

@MiloszFilimowski MiloszFilimowski commented Apr 24, 2026

Copy link
Copy Markdown
Collaborator

Description

  • ConnectionManager: stop excess recvonly transceivers (preferring orphaned mid === 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 (including connected) through a single promise-chain queue with one handleMediaEvent entry point, so async handlers run strictly in order; dedup the duplicate local endpoint metadata parse/set in handleConnected.
  • Tests: await receiveMediaEvent in event/method tests; add serializedMediaEvents.test.ts (offer/answer ordering, pre-connect events are dropped, consecutive connected events stay in order) and addTransceiversIfNeeded.test.ts (clamping, negative-delta regression, stopped-transceiver counting and stop-candidate exclusion).

Motivation and Context

Fixes reconnect-related state drift where stale recvonly transceivers accumulated and async server events interleaved, leaving the client and server out of sync.

Documentation impact

  • Documentation update required
  • Documentation updated in another PR
  • No documentation update required

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to
    not work as expected)

@MiloszFilimowski MiloszFilimowski force-pushed the mfilimowski/fix-reconnect branch from 254bf7b to 645ab81 Compare April 24, 2026 10:56
@MiloszFilimowski MiloszFilimowski changed the title debugging FCE-3224: Fix reconnect race condition Apr 24, 2026
@linear

linear Bot commented Apr 24, 2026

Copy link
Copy Markdown

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 recvonly transceivers (preferring orphaned mid === null) before adding new ones in ConnectionManager.
  • Serialize non-connected media events in WebRTCEndpoint via a promise queue; refactor connected handling into handleConnected.
  • 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.

Comment thread packages/webrtc-client/src/webRTCEndpoint.ts Outdated
Comment thread packages/webrtc-client/src/webRTCEndpoint.ts Outdated
Comment thread packages/webrtc-client/src/webRTCEndpoint.ts Outdated
Comment thread packages/webrtc-client/src/ConnectionManager.ts
@MiloszFilimowski MiloszFilimowski marked this pull request as draft April 24, 2026 12:23
MiloszFilimowski and others added 3 commits April 24, 2026 14:29
Prevents Vitest 3 breakage for the two remaining async rejection
assertions that were not already awaited.

Made-with: Cursor
@MiloszFilimowski MiloszFilimowski marked this pull request as ready for review April 24, 2026 12:39
@MiloszFilimowski MiloszFilimowski merged commit 38bca76 into main Apr 24, 2026
2 checks passed
@MiloszFilimowski MiloszFilimowski deleted the mfilimowski/fix-reconnect branch April 24, 2026 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants