Skip to content

Fix ReadOnlySequence<T>.TryGet to return false at end position#130561

Draft
tannergooding with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-readonlysequence-tryget
Draft

Fix ReadOnlySequence<T>.TryGet to return false at end position#130561
tannergooding with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-readonlysequence-tryget

Conversation

Copilot AI commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

ReadOnlySequence<T>.TryGet(ref position, out memory) returned true with an empty memory when position was sequence.End (or sequence.GetPosition(size)). No segment is actually being returned in that case — it should return false.

var seq = new ReadOnlySequence<byte>(new byte[10]);
SequencePosition pos = seq.End;
bool ok = seq.TryGet(ref pos, out ReadOnlyMemory<byte> mem);
// before: ok == true,  mem.IsEmpty == true
// after:  ok == false, mem.IsEmpty == true

Changes

  • ReadOnlySequence.Helpers.csTryGetBuffer now returns false with a default memory when the slice from position in 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 return true + empty memory and advance, so multi-segment enumeration doesn't stall.
  • ReadOnlySequence.cs – Added a <remarks> on TryGet documenting the end-of-sequence behavior.
  • Tests – Updated tests that codified the previous behavior (Empty_TryGet, Empty_Enumerator, TryGetReturnsEmptySegments, TryGetStopsAtEndWhenEndIsFirstItemOfFull/Empty, EnumerableStopsAtEndWhenEndIsFirstItemOfFull/Empty) and added TryGetReturnsFalseAtEndPosition covering single-segment, empty, and multi-segment sequences with both sequence.End and sequence.GetPosition(size).

Behavior change

Public API semantic change. Loops of the form:

while (sequence.TryGet(ref position, out ReadOnlyMemory<T> memory)) { ... }

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-tree System.IO.Pipelines / System.Text.Json callers already tolerate TryGet returning false in this shape.

Note

This PR was authored by GitHub Copilot.

Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot July 11, 2026 21:51
Copilot AI changed the title [WIP] Fix ReadOnlySequence<T>.TryGet() to return false for empty slice Fix ReadOnlySequence&lt;T&gt;.TryGet to return false at end position Jul 11, 2026
Copilot AI requested a review from tannergooding July 11, 2026 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

ReadOnlySequence<T>.TryGet() returns empty result

2 participants