Fix ReadOnlySequence<T>.TryGet to return false at end position#130561
Draft
tannergooding with Copilot wants to merge 2 commits into
Draft
Fix ReadOnlySequence<T>.TryGet to return false at end position#130561tannergooding with Copilot wants to merge 2 commits into
tannergooding with Copilot wants to merge 2 commits into
Conversation
Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix ReadOnlySequence<T>.TryGet() to return false for empty slice
Fix ReadOnlySequence<T>.TryGet to return false at end position
Jul 11, 2026
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.
ReadOnlySequence<T>.TryGet(ref position, out memory)returnedtruewith an emptymemorywhenpositionwassequence.End(orsequence.GetPosition(size)). No segment is actually being returned in that case — it should returnfalse.Changes
ReadOnlySequence.Helpers.cs–TryGetBuffernow returnsfalsewith a default memory when the slice frompositionin the end segment would be empty (startIndex == endIndex). Covers both the multi-segment path (startSegment == endObject) and all single-segment paths (Array / String /MemoryManager<T>). Empty middle segments continue to returntrue+ empty memory and advance, so multi-segment enumeration doesn't stall.ReadOnlySequence.cs– Added a<remarks>onTryGetdocumenting the end-of-sequence behavior.Empty_TryGet,Empty_Enumerator,TryGetReturnsEmptySegments,TryGetStopsAtEndWhenEndIsFirstItemOfFull/Empty,EnumerableStopsAtEndWhenEndIsFirstItemOfFull/Empty) and addedTryGetReturnsFalseAtEndPositioncovering single-segment, empty, and multi-segment sequences with bothsequence.Endandsequence.GetPosition(size).Behavior change
Public API semantic change. Loops of the form:
terminate one iteration earlier when the sequence has a trailing empty segment (or is empty) — an iteration that always yielded an empty memory. Iteration through empty middle segments is unchanged.
SequenceReader<T>and the in-treeSystem.IO.Pipelines/System.Text.Jsoncallers already tolerateTryGetreturningfalsein this shape.Note
This PR was authored by GitHub Copilot.