Fix JsonReaderData buffer refill to respect valid bytes on partial stream reads#38666
Open
AndriySvyryd with Copilot wants to merge 2 commits into
Open
Fix JsonReaderData buffer refill to respect valid bytes on partial stream reads#38666AndriySvyryd with Copilot wants to merge 2 commits into
AndriySvyryd with Copilot wants to merge 2 commits into
Conversation
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix leftover calculation in JsonReaderData during stream refill
Fix JsonReaderData buffer refill to respect valid bytes on partial stream reads
Jul 17, 2026
There was a problem hiding this comment.
Pull request overview
Fixes JsonReaderData.ReadBytes refill behavior when a stream returns short/partial reads by ensuring only valid bytes are treated as available, preventing stale buffer data from being reused and skewing BytesConsumed / tokenization.
Changes:
- Adjusted refill “leftover” calculation to use
_bytesAvailable(valid bytes) rather thanbuffer.Length. - Replaced single
Stream.Read(...)calls with a bounded read loop (ReadToEnd) that fills the target span until EOF. - Added a regression test using a stream that intentionally returns small chunks per read, asserting correct end-to-end parsing and
BytesConsumed.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/EFCore/Storage/Json/JsonReaderData.cs | Fixes buffer refill logic to respect valid-byte boundaries and handle partial stream reads via a read-to-fill loop. |
| test/EFCore.Tests/Storage/Json/JsonReaderDataTest.cs | Adds regression coverage for partial-read streams to validate BytesConsumed correctness through EndArray. |
cincuranet
reviewed
Jul 20, 2026
| var totalConsumed = bytesConsumed + _positionInBuffer; | ||
| _bytesRead += totalConsumed; | ||
| if (_bytesAvailable != 0 && totalConsumed < buffer.Length) | ||
| if (_bytesAvailable != 0 && totalConsumed < _bytesAvailable) |
Contributor
There was a problem hiding this comment.
Unless totalConsumed can somehow be negative I guess the _bytesAvailable != 0 is reduntant, isn't it?
Comment on lines
+122
to
+137
| private static int ReadToEnd(Stream stream, Span<byte> buffer) | ||
| { | ||
| var bytesRead = 0; | ||
| while (bytesRead < buffer.Length) | ||
| { | ||
| var read = stream.Read(buffer[bytesRead..]); | ||
| if (read == 0) | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| bytesRead += read; | ||
| } | ||
|
|
||
| return bytesRead; | ||
| } |
Contributor
There was a problem hiding this comment.
Unless I'm mistaken, this looks like what stream.ReadAtLeast(buffer, buffer.Length, throwOnEndOfStream: false); would do.
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.
JsonReaderData.ReadBytestreatedbuffer.Lengthas valid data during refills, even when_bytesAvailablewas smaller after short stream reads. That could copy stale bytes into the next reader window, skewBytesConsumed, and break JSON tokenization.Refill logic now uses valid-byte boundaries
totalConsumed < buffer.LengthtototalConsumed < _bytesAvailable.buffer.AsSpan(totalConsumed, _bytesAvailable - totalConsumed).Refill reads now handle short reads correctly
Read(...)calls with a bounded loop that keeps reading until the target span is filled or EOF._bytesAvailable, buffered content, andUtf8JsonReaderfinal-block behavior.Regression coverage for partial-read streams
EndArrayand exactBytesConsumedaccounting.