-
Notifications
You must be signed in to change notification settings - Fork 284
fix: error on uncompressed datastreams when chunks are missing #2073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lukasIO
wants to merge
12
commits into
main
Choose a base branch
from
lukas/fix-data-stream-order
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
47ed3e9
fix: error on datastreams when chunks are missing
lukasIO 1683f9b
Create seven-buckets-help.md
lukasIO 06e7214
fix return
lukasIO f4b4c9d
Merge branch 'lukas/fix-data-stream-order' of github.com:livekit/clie…
lukasIO 995aa21
address one more place where controller should error
lukasIO 0e2623c
keep expected behaviour
lukasIO 6309f72
revert unneeded changes and address comments
lukasIO 90b4055
align throw style for streams
lukasIO bc37b01
add tests and fix empty chunk behaviour
lukasIO 30cf64e
drop version overrides explicitly
lukasIO 57aba77
simplify stream readers without versioning
lukasIO 0c44f64
only log version if it's bigger than 0
lukasIO File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "livekit-client": patch | ||
| --- | ||
|
|
||
| fix: error on datastreams when chunks are missing |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -241,7 +241,7 @@ export default class IncomingDataStreamManager { | |
| info, | ||
| compressed | ||
| ? inflateRawByteChunkStream(stream, streamHeader.streamId, this.maxPayloadByteLength) | ||
| : stream, | ||
| : stream.pipeThrough(ensureOrderedChunks(streamHeader.streamId)), | ||
|
1egoman marked this conversation as resolved.
|
||
| // `totalLength` is the pre-compression size, and the reader counts decompressed bytes, | ||
|
1egoman marked this conversation as resolved.
|
||
| // so it applies to both paths (mirrors text). | ||
| bigIntToNumber(streamHeader.totalLength), | ||
|
|
@@ -344,7 +344,7 @@ export default class IncomingDataStreamManager { | |
| info, | ||
| compressed | ||
| ? inflateRawChunkStream(stream, streamHeader.streamId, this.maxPayloadByteLength) | ||
| : stream, | ||
| : stream.pipeThrough(ensureOrderedChunks(streamHeader.streamId)), | ||
|
devin-ai-integration[bot] marked this conversation as resolved.
|
||
| // `totalLength` is the pre-compression size, and the reader sees decompressed bytes, so | ||
| // it applies to both paths. | ||
| bigIntToNumber(streamHeader.totalLength), | ||
|
|
@@ -367,7 +367,7 @@ export default class IncomingDataStreamManager { | |
| ), | ||
| ); | ||
| this.byteStreamControllers.delete(chunk.streamId); | ||
| } else if (chunk.content.length > 0) { | ||
| } else { | ||
| fileBuffer.controller.enqueue(chunk); | ||
| } | ||
| } | ||
|
|
@@ -381,7 +381,7 @@ export default class IncomingDataStreamManager { | |
| ), | ||
| ); | ||
| this.textStreamControllers.delete(chunk.streamId); | ||
| } else if (chunk.content.length > 0) { | ||
| } else { | ||
| textBuffer.controller.enqueue(chunk); | ||
| } | ||
| } | ||
|
|
@@ -455,25 +455,21 @@ function createInlineStream( | |
| ): ReadableStream<DataStream_Chunk> { | ||
| return new ReadableStream<DataStream_Chunk>({ | ||
| start: async (controller) => { | ||
| try { | ||
| const bytes = await content; | ||
| controller.enqueue( | ||
| new DataStream_Chunk({ streamId, chunkIndex: BigInt(0), content: bytes }), | ||
| ); | ||
| controller.close(); | ||
| } catch (err) { | ||
| controller.error(err); | ||
| } | ||
| const bytes = await content; | ||
| controller.enqueue(new DataStream_Chunk({ streamId, chunkIndex: BigInt(0), content: bytes })); | ||
| controller.close(); | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Validates that chunks are received in order, dropping duplicates and throwing if gaps are found. | ||
| * | ||
| * A stateful decompressor silently corrupts on duplicated or out-of-order input, so duplicates are | ||
| * dropped (with a warning - in-order delivery is expected on the reliable channel, but reconnect | ||
| * handling may replay) and a gap is a hard error. Shared by the text and byte deflate-raw decoders. | ||
| * Reassembly (and, for compressed streams, a stateful decompressor) silently corrupts on duplicated | ||
| * or out-of-order input, so duplicates are dropped (with a warning - in-order delivery is expected | ||
| * on the reliable channel, but reconnect handling may replay) and a gap is a hard error. Empty | ||
| * chunks consume their index and are then dropped, so they never reach the reader. Applied to every | ||
| * chunked stream, compressed or not. | ||
| */ | ||
| function ensureOrderedChunks( | ||
| streamId: string, | ||
|
|
@@ -484,17 +480,20 @@ function ensureOrderedChunks( | |
| const index = bigIntToNumber(value.chunkIndex); | ||
| if (index <= lastChunkIndex) { | ||
| log.warn( | ||
| `ignoring duplicate chunk ${index} for compressed data stream ${streamId} (last processed: ${lastChunkIndex})`, | ||
| `ignoring duplicate chunk ${index} ${value.version > 0 ? `(version ${value.version})` : ''} for data stream ${streamId} (last processed: ${lastChunkIndex})`, | ||
| ); | ||
| return; | ||
| } | ||
| if (index > lastChunkIndex + 1) { | ||
| throw new DataStreamError( | ||
| `Missing chunk(s) ${lastChunkIndex + 1}..${index - 1} for compressed data stream ${streamId} - cannot continue decompressing`, | ||
| `Missing chunk(s) ${lastChunkIndex + 1}..${index - 1} for data stream ${streamId} - cannot reassemble payload`, | ||
| DataStreamErrorReason.Incomplete, | ||
| ); | ||
| } | ||
| lastChunkIndex = index; | ||
| if (value.content.length === 0) { | ||
| return; | ||
| } | ||
| controller.enqueue(value); | ||
|
Comment on lines
493
to
497
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Just wanted to say explicitly that I think moving this to the send path instead of the receive path is probably the better place for this to live!) |
||
| }, | ||
| }); | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: It also might be good to add a test here which sends chunk version 1 and then 0. From our conversation on slack, this is now "first one wins", so assuming I have that right I think it would be good to test both permutations.