fix(fullstack): buffer streaming frames across physical reads - #5783
Open
andoowhy wants to merge 1 commit into
Open
fix(fullstack): buffer streaming frames across physical reads#5783andoowhy wants to merge 1 commit into
andoowhy wants to merge 1 commit into
Conversation
`Streaming<T, E>`'s `FromRequest`/`FromResponse` decoding (`byte_stream_to_client_stream`) only looked for a complete length-prefixed frame within whatever bytes a single physical network read handed back, discarding any leftover partial frame instead of carrying it over to the next read. HTTP/TCP give no guarantee that physical chunk boundaries line up with frame boundaries, so any frame larger than a few KB routinely gets split across multiple reads - and each time that happened, the frame surfaced as `StreamingError::Decoding` instead of being reassembled. This affects `ChunkedByteStream`, `CborStream<T>`, and any other `Streaming<T, E>` with a non-`()` encoding, on both the server (`FromRequest`) and client (`FromResponse`) sides. This reworks `byte_stream_to_client_stream` to accumulate a buffer across as many physical reads as it takes to see one complete frame before attempting to decode it, the same pattern `tokio_util::codec::Decoder` uses, instead of resetting per physical chunk. Found via a real reproduction: streaming an 8MB blob through `ChunkedByteStream` over real loopback TCP reliably failed after the first one or two physical reads; after this fix it round-trips correctly regardless of size. Added two regression tests in `payloads::stream::tests`: - a frame deliberately split into many small physical chunks now decodes correctly (fails on the old code with `StreamingError::Decoding`) - multiple frames packed into a single physical chunk still decode in order (unchanged behavior, guards against a regression in the rework)
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.
Streaming<T, E>'sFromRequest/FromResponsedecoding (byte_stream_to_client_stream) only looked for a complete length-prefixed frame within whatever bytes a single physical network read handed back, discarding any leftover partial frame instead of carrying it over to the next read. HTTP/TCP give no guarantee that physical chunk boundaries line up with frame boundaries, so any frame larger than a few KB routinely gets split across multiple reads - and each time that happened, the frame surfaced asStreamingError::Decodinginstead of being reassembled. This affectsChunkedByteStream,CborStream<T>, and any otherStreaming<T, E>with a non-()encoding, on both the server (FromRequest) and client (FromResponse) sides.I hit this using
ChunkedByteStreamto stream file blobs from a client to a server: an 8MB blob reliably failed after the first ~8KB arrived, regardless of how the sender chunked its writes (chunking the application-level frames doesn't help, since the transport is free to coalesce/split them independently of that).Fix
Reworks
byte_stream_to_client_streamto accumulate a buffer across as many physical reads as it takes to see one complete frame, only attempting to decode once the buffer actually contains a whole frame - the same approachtokio_util::codec::Decoder/FramedReaduse. No public API changes.Testing
payloads::stream::tests::frame_split_across_many_physical_chunks_decodes_correctly, which reproduces the bug in-process (no real network needed) by feedingbyte_stream_to_client_streama stream that hands back a single frame's bytes split into 37-byte pieces. This fails withStreamingError::Decodingon currentmainand passes with this fix.payloads::stream::tests::multiple_frames_in_one_physical_chunk_decode_in_order, confirming multiple frames arriving in a single physical chunk still decode correctly and in order (guards against a regression in the buffering rework).cargo test -p dioxus-fullstack --features serverpasses.cargo fmt --checkandcargo clippy --testsare clean (no new warnings).ChunkedByteStreamover real loopback TCP, streaming an 8MB payload from areqwestclient - fails onmain, passes with this patch.