Skip to content

fix(transport-webrtc): detect terminal state at construction time#3459

Open
paschal533 wants to merge 3 commits intolibp2p:mainfrom
paschal533:fix/webrtc-initial-connection-state
Open

fix(transport-webrtc): detect terminal state at construction time#3459
paschal533 wants to merge 3 commits intolibp2p:mainfrom
paschal533:fix/webrtc-initial-connection-state

Conversation

@paschal533
Copy link
Copy Markdown
Contributor

Problem

When RTCPeerConnectionMultiaddrConnection is constructed, ICE may have already reached a terminal state ('failed' or 'closed') before the onconnectionstatechange handler is registered. Because that handler is a property assignment, it does not fire retroactively for transitions that already occurred.

This left a half-open connection that the connection manager never cleaned up — the dialing side appeared "connected" while the listening side correctly detected the failure.

Closes #2646

Solution

Explicitly check the initial connectionState at construction time and call onTransportClosed() / peerConnection.close() when the state is already terminal.

Note: 'disconnected' is transient — the ICE agent may recover to 'connected' without intervention — so only 'failed' and 'closed' are treated as terminal here.

Test plan

  • Added test: closes immediately when peer connection is already in 'failed' state at construction time
  • Added test: closes when peer connection transitions to 'failed' state after construction
  • Existing 'can open and close' test still passes

Related PRs

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

  • This PR — initial state check at construction time
  • fix(transport-webrtc): fix UnexpectedEOFError 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

…ction time

When RTCPeerConnectionMultiaddrConnection is constructed, ICE may have
already reached a terminal state ('failed' or 'closed') before the
onconnectionstatechange handler is registered. Because that handler is a
property assignment, it does not fire retroactively for transitions that
already occurred, leaving a half-open connection the connection manager
never cleans up.

Explicitly check the initial connectionState at construction time and
call onTransportClosed() / peerConnection.close() when the state is
already terminal.

Note: 'disconnected' is transient — the ICE agent may recover to
'connected' — so only 'failed' and 'closed' are treated as terminal here.

Closes libp2p#2646
@paschal533 paschal533 requested a review from a team as a code owner April 17, 2026 11:40
@paschal533 paschal533 changed the title fix(transport-webrtc): check initial peer connection state at construction time fix(transport-webrtc): detect terminal state at construction time 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.

When failing to establish WebRTC connection a "half open" connection can occur

4 participants