Properly handle data channel opening failures #2936
Merged
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.
Fix #2900
In order to open outbound substreams on a WebRTC connection, the mechanism that the API of smoldot uses is as follows: one function to ask the implementation to open a substream, and one callback that is later called in order to notify that a substream has been fully opened.
The API of smoldot doesn't provide a way to report that opening an outbound substream has failed. This is because this isn't really supposed to happen. If you have a connection, opening a substream is purely a "local side change": you allocate an ID, then send a message. It should maybe not even be asynchronous, but that's off-topic here.
The problem here is that this mental model doesn't really fit what the browser does. In particular, the browser does nothing until a data channel is first opened, plus the browser can call
onclose
oronerror
on the data channel beforeonopen
. To accomodate for that, the current code is a bit hacky in the sense that it reports the connection as immediately open.This PR does it a bit more differently and more properly: we immediately open a data channel at the same time as we create the
RTCPeerConnection
in order for the browser to start connecting, even though smoldot didn't request a channel yet. We store this first data channel on the side, and when smoldot asks for a data channel we provide this one.If a data channel closes while it is still opening (the handshake one or another), we just kill the entire connection.