Skip to content

fix(transport-webrtc): fix EOF race in readCandidatesUntilConnected#3460

Open
paschal533 wants to merge 2 commits intolibp2p:mainfrom
paschal533:fix/webrtc-signaling-eof-race
Open

fix(transport-webrtc): fix EOF race in readCandidatesUntilConnected#3460
paschal533 wants to merge 2 commits intolibp2p:mainfrom
paschal533:fix/webrtc-signaling-eof-race

Conversation

@paschal533
Copy link
Copy Markdown
Contributor

Problem

A race condition caused the WebRTC "simple" transport compliance test to fail intermittently: the listener closes its signaling stream just before the initiator's connectionstatechange: connected event fires. The initiator then received EOF on the signaling stream and threw an UnexpectedEOFError, discarding a perfectly good peer connection.

Root cause: connectedPromise was declared inside the try block, making it inaccessible in the catch handler for stream read errors.

Solution

Move connectedPromise outside the try block. In the catch handler, if the peer connection is not yet 'connected', await connectedPromise:

  • If it resolves → the connection succeeded, ignore the stream error
  • If it rejects → the connection failed, re-throw the original stream error

Also corrects the log message from "error parsing ICE candidate" to "error reading ICE candidates" for accuracy.

Test plan

  • Added readCandidatesUntilConnected describe block with 4 tests
  • Test: throws ConnectionFailedError when the peer connection enters 'failed' state
  • Test: does not fail when peer connection is temporarily disconnected then recovers
  • Test: throws ConnectionFailedError when peer connection goes disconnected then failed
  • Test: returns without error when peer connection reaches 'connected' state

Related PRs

This is one of four focused fixes split from #3406 as requested by @dozyio:

  • fix(transport-webrtc): check initial peer connection state at construction time
  • This PRUnexpectedEOFError race in readCandidatesUntilConnected
  • fix(transport-webrtc): do not treat 'disconnected' as a terminal peer connection state
  • fix(interface-compliance-tests): increase abort propagation timeout in stream muxer close test

…ntilConnected

A race condition caused intermittent failures in the WebRTC 'simple'
transport compliance test: the listener closes its signaling stream just
before the initiator's connectionstatechange fires for the 'connected'
state. The initiator then receives EOF on the signaling stream and threw
an UnexpectedEOFError, discarding a perfectly good peer connection.

Root cause: connectedPromise was declared inside the try block, making it
inaccessible in the catch handler for stream read errors.

Fix: move connectedPromise outside the try block. In the catch handler,
if the PC is not yet 'connected', await connectedPromise — if it resolves
we ignore the stream error (the connection succeeded); if it rejects we
re-throw the original stream error.
@paschal533 paschal533 requested a review from a team as a code owner April 17, 2026 11:41
@paschal533 paschal533 changed the title fix(transport-webrtc): fix UnexpectedEOFError race in readCandidatesUntilConnected fix(transport-webrtc): fix EOF race in readCandidatesUntilConnected Apr 17, 2026
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.

1 participant