Skip to content

Add incremental chunked reading to CborReader - #133602

Merged
bartonjs merged 5 commits into
dotnet:mainfrom
muhammad-othman:cbor-support-incremental-chunked-reading
Sep 21, 2026
Merged

bartonjs merged 5 commits into
dotnet:mainfrom
muhammad-othman:cbor-support-incremental-chunked-reading

Conversation

@muhammad-othman

Copy link
Copy Markdown
Contributor

Implements the API approved in #129718, adding incremental reading mode where the caller owns I/O and supplies data in chunks:

public partial class CborReader
{
    public CborReader(ReadOnlyMemory<byte> data, CborReaderOptions? options, bool isFinalBlock);
    public void SlideData(ReadOnlyMemory<byte> data, bool isFinalBlock);
    public void Reset(ReadOnlyMemory<byte> data, bool isFinalBlock);
    public bool TrySkipValue(bool disableConformanceModeChecks = false);
    public bool TrySkipToParent(bool disableConformanceModeChecks = false);
}

public partial enum CborReaderState
{
    NeedsMoreData,
}
  • In non-final mode, PeekState() returns NeedsMoreData when the next token is incomplete in the buffer; any other state guarantees the single-token read won't fail on end of data. Multi-token reads (indefinite-length strings, ReadEncodedValue, semantic tag readers) still throw on truncation and restore the reader state, so the caller can slide in more data and retry.
  • SlideData replaces the buffer and preserves nesting state; the caller must keep the unread bytes (BytesRemaining) at the start of the new buffer. Only the length is validated.
  • Non-final mode is Lax-only in this version (per the proposal): other conformance modes track map keys by buffer offset, which cannot survive a slide. The validation is centralized so a future all-modes implementation has one place to rebase that bookkeeping.
  • Final-block readers (all existing constructors and Reset(data)) are behaviorally unchanged.

Notes for reviewers:

  • In non-final mode, reading at a root-value boundary throws CborContentException rather than InvalidOperationException, since the reader can't know the sequence ended until the final block arrives.
  • Found but not fixed here: a root sequence ending in a dangling tag reports Finished, though it isn't well-formed. This predates this change; incremental readers deliberately match it for consistency. I think we should track it separately and will create an issue for it.

Implements the API approved in dotnet#129718: adding incremental reading
mode where the caller owns I/O and supplies data in chunks via SlideData,
with PeekState() reporting NeedsMoreData on incomplete tokens and
TrySkipValue/TrySkipToParent returning false instead of throwing.

Non-final mode requires CborConformanceMode.Lax, since the other
modes retain map key encodings as buffer offsets that cannot survive
a slide. Final-block readers are unchanged.

Fix dotnet#129718
Copilot AI lite review requested due to automatic review settings September 10, 2026 15:03
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Sep 10, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-formats-cbor, @bartonjs, @vcsjones
See info in area-owners.md if you want to be subscribed.

@muhammad-othman

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree company="Amazon"

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

It adds new public API surface and changes core reader behavior, which warrants final maintainer review despite strong accompanying test coverage.

Pull request overview

This PR extends System.Formats.Cbor.CborReader with an incremental (chunked) reading mode where callers can supply data in multiple buffers, while preserving nesting state between refills.

Changes:

  • Adds new public APIs to support incremental reading (isFinalBlock constructor/Reset, SlideData, TrySkipValue/TrySkipToParent) and introduces CborReaderState.NeedsMoreData.
  • Updates PeekState() so non-final readers return NeedsMoreData for truncated next-token scenarios (instead of throwing), while multi-token semantic tag readers continue to throw on truncation and roll back state.
  • Adds/updates tests to validate SlideData semantics, NeedsMoreData gating behavior, and resumable skipping across all split points.
File summaries
File Description
src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.cs Adds isFinalBlock plumbing, SlideData, Reset overload, and checkpoint/state support needed for incremental mode.
src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.PeekState.cs Implements NeedsMoreData behavior for non-final readers and token-availability gating.
src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.SkipValue.cs Introduces TrySkipValue/TrySkipToParent and reworks skipping to return false for needs-more-data in non-final mode.
src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.Integer.cs Adds DecodeCollectionLength to relax definite-length collection header handling for non-final buffers.
src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.Array.cs Uses DecodeCollectionLength so array headers can be read even when contents arrive later.
src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.Map.cs Uses DecodeCollectionLength and adjusts definite-length map size validation for non-final mode.
src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.Tag.cs Ensures semantic tag readers treat NeedsMoreData as truncation (throw) to preserve atomic multi-token semantics.
src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.String.cs Documents that returned memory views can be invalidated if the caller reuses buffers via SlideData.
src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReaderState.cs Adds CborReaderState.NeedsMoreData with API docs.
src/libraries/System.Formats.Cbor/src/Resources/Strings.resx Adds localized strings for new incremental-mode validation and exceptions.
src/libraries/System.Formats.Cbor/ref/System.Formats.Cbor.cs Updates public surface area to include the new APIs and enum member.
src/libraries/System.Formats.Cbor/tests/System.Formats.Cbor.Tests.csproj Includes the new test file in the test build.
src/libraries/System.Formats.Cbor/tests/Reader/CborReaderTests.SlideData.cs Adds extensive incremental-mode tests (sliding, gating, truncation behavior, resumability).
src/libraries/System.Formats.Cbor/tests/Reader/CborReaderTests.SkipValue.cs Adds tests for TrySkipValue/TrySkipToParent behavior in non-final mode.
src/libraries/System.Formats.Cbor/tests/Reader/CborReaderTests.Helpers.cs Expands test vectors and splits invalid inputs into truncated-vs-malformed sets for incremental semantics.
Review details
  • Files reviewed: 15/15 changed files
  • Comments generated: 0
  • Review effort level: Lite

@bartonjs bartonjs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM, but a few notes.

Comment thread src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.cs Outdated
Comment thread src/libraries/System.Formats.Cbor/tests/Reader/CborReaderTests.SkipValue.cs Outdated
Comment thread src/libraries/System.Formats.Cbor/tests/Reader/CborReaderTests.SlideData.cs Outdated
Copilot AI review requested due to automatic review settings September 21, 2026 15:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

The changes span a broad API and state-management surface requiring final human review.

Review effort: Lite
Findings: None

Copilot AI review requested due to automatic review settings September 21, 2026 16:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

A moderate retry-allocation issue remains unresolved, requiring changes and final human review.

Review effort: Lite
Findings: None

Copilot AI review requested due to automatic review settings September 21, 2026 16:47
@muhammad-othman
muhammad-othman force-pushed the cbor-support-incremental-chunked-reading branch from 28502db to b534825 Compare September 21, 2026 16:47

@muhammad-othman muhammad-othman left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review, I've applied your suggestions to the PR, please let me know if there are anything that I missed.

Comment thread src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.cs Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Public state documentation and exception-contract wording need alignment before approval.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 2 Low severity

Open (2)

@artl93 artl93 added the Servicing-approved Approved for servicing release label Sep 21, 2026
@am11

am11 commented Sep 21, 2026

Copy link
Copy Markdown
Member

@artl93, was the servicing label meant for this PR? It's not a backport. Looks like a mistake.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Correct the NeedsMoreData documentation and malformed XML documentation before approval.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 2 Low severity

Open (2)
Resolved since last review (1)

Copilot AI review requested due to automatic review settings September 21, 2026 17:32
@artl93 artl93 removed the Servicing-approved Approved for servicing release label Sep 21, 2026
@artl93

artl93 commented Sep 21, 2026

Copy link
Copy Markdown
Member

@artl93, was the servicing label meant for this PR? It's not a backport. Looks like a mistake.

@am11 - you are correct. This still needs to get this backported to 11.0 to make the cutoff.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

Clarify the NeedsMoreData root-boundary documentation and resolve the exception-contract discrepancy.

Review effort: Lite
Findings: 1 Low severity

Open (1)
Resolved since last review (1)

@bartonjs
bartonjs enabled auto-merge (squash) September 21, 2026 18:13
Copilot AI review requested due to automatic review settings September 21, 2026 18:13
auto-merge was automatically disabled September 21, 2026 18:13

Head branch was pushed to by a user without write access

@muhammad-othman
muhammad-othman force-pushed the cbor-support-incremental-chunked-reading branch from 8ccc8fa to 10210bb Compare September 21, 2026 18:13
@bartonjs

Copy link
Copy Markdown
Member

@muhammad-othman No more commits, please (and please, never force-push to an already open PR).

Every time you commit again you reset the CI clock, which is measured in hours.

@bartonjs
bartonjs enabled auto-merge (squash) September 21, 2026 18:17
@muhammad-othman

Copy link
Copy Markdown
Contributor Author

@muhammad-othman No more commits, please (and please, never force-push to an already open PR).

Every time you commit again you reset the CI clock, which is measured in hours.

Sorry I was trying to get the commit history in good shape 😓 , no more commits from my side.

@bartonjs

Copy link
Copy Markdown
Member

@muhammad-othman We squash-merge, so all of the individual commits more or less go away at merge time.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Resolve the public overload ambiguity and address the documented skip and allocation issues.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity

Open (1)
Resolved since last review (1)

Comment thread src/libraries/System.Formats.Cbor/ref/System.Formats.Cbor.cs
@bartonjs

Copy link
Copy Markdown
Member

/ba-g Tests have already succeeded on multiple platforms, no platform-specific dependencies

@bartonjs
bartonjs merged commit 55b3dad into dotnet:main Sep 21, 2026
66 of 82 checks passed
@bartonjs

Copy link
Copy Markdown
Member

/backport to release/11.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

artl93 pushed a commit that referenced this pull request Sep 21, 2026
Backport of #133602 to release/11.0

/cc @bartonjs @muhammad-othman

## Customer Impact

- [X] Customer reported
- [ ] Found internally

New API for .NET 11 to support processing a CBOR payload without forcing
the whole document into contiguous memory.

## Regression

- [ ] Yes
- [X] No

## Testing

Existing tests show that the existing "non-streaming" model works
exactly as before, and the new tests confirm the behavior of the new
"streaming" model.

## Risk

Low. The test coverage says existing callers are unaffected, and the new
behaviors are only visible to callers who use the new API.

Co-authored-by: Muhammad Othman <muhammmadothman@gmail.com>
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Formats.Cbor community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants