sdk: H3Transport reassemble frames across reads#934
Merged
Conversation
A single WebTransport reader.read() chunk is not guaranteed to contain whole length-prefixed frames — a frame payload, or even its varint length prefix, may be split across multiple reads. The previous loop assumed every chunk started and ended on a frame boundary, which caused decoded frames to be truncated (or length-decode to throw) when the underlying stream fragmented, surfacing downstream as partial-decode errors on schema patches (missing welcome data / refId errors). Introduce a FrameReassembler that buffers a pending byte tail across reads and only emits complete frames. Both the reliable and unreliable readers now feed a reassembler and dispatch whole frames to onmessage(). Answers the existing "should we buffer the message in case it's not fully read?" TODO in the code.
Member
|
Thank you for reporting @anaibol. We had the same issue in the server-side - I've applied the same |
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
H3TransportTransport.readIncomingData()(and its unreliable counterpart) inpackages/sdk/src/transport/H3Transport.tsassumes everyreader.read()chunk contains one or more whole length-prefixed frames:With WebTransport over HTTP/3, a single stream read is not guaranteed to land on a frame boundary. A chunk can:
onmessageis called with a truncated buffer and the schema decoder then fails on the next patch with partial-decode errors (stalerefId, missing fields, "no welcome data" during handshake).decode.numberthrows and the whole read loop aborts.In practice this manifests as sporadic handshake failures and
ROOM_STATE_PATCHdecode errors on rooms with larger initial state, especially under dev / local latency where chunks are naturally fragmented.Fix
Introduce a
FrameReassemblerthat buffers a pending byte tail across reads and only emits fully-received frames. Both the reliable and unreliable readers feed a reassembler instance and dispatch complete frames toonmessage.This answers the existing
// QUESTION: should we buffer the message in case it's not fully read?TODO — yes, we should.No change to the wire format, message ordering, or public API of
H3TransportTransport.FrameReassembleris exported so it can be unit-tested directly; it's a narrow helper class.Test plan
Unit tests in
packages/sdk/test/h3transport.test.tscover:undefinedchunks (no-op)Notes
Originally scoped for
colyseus/colyseus.js, but that repo is archived; active development lives in this monorepo atpackages/sdk. The bug is present in both.