fix(transport-webrtc): detect terminal state at construction time#3459
Open
paschal533 wants to merge 3 commits intolibp2p:mainfrom
Open
fix(transport-webrtc): detect terminal state at construction time#3459paschal533 wants to merge 3 commits intolibp2p:mainfrom
paschal533 wants to merge 3 commits intolibp2p:mainfrom
Conversation
…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
dozyio
approved these changes
Apr 18, 2026
tabcat
approved these changes
Apr 18, 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.
Problem
When
RTCPeerConnectionMultiaddrConnectionis constructed, ICE may have already reached a terminal state ('failed'or'closed') before theonconnectionstatechangehandler 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
connectionStateat construction time and callonTransportClosed()/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
'failed'state at construction time'failed'state after construction'can open and close'test still passesRelated PRs
This is one of four focused fixes split from #3406 as requested by @dozyio:
UnexpectedEOFErrorrace inreadCandidatesUntilConnected'disconnected'as a terminal peer connection state