Skip to content

feat(serialization): add owned Arc buffer codec and copier - #11463

Merged
ReubenBond merged 5 commits into
dotnet:mainfrom
ReubenBond:rb-arc-serialization-foundations
Oct 20, 2026
Merged

ReubenBond merged 5 commits into
dotnet:mainfrom
ReubenBond:rb-arc-serialization-foundations

Conversation

@ReubenBond

@ReubenBond ReubenBond commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

ArcBuffer gains a built-in codec that preserves source ownership and contents across repeated serialization, and a copier that acquires independent pins over the referenced bytes. Reader<TInput>.ReadOwnedBuffer(int) centralizes owned payload reads: nonempty Arc input acquires slice pins from the reader's current backing page, other inputs copy into available pooled-page capacity through the shared reader-to-writer helper, and zero-length reads return owner-free ArcBuffer.Empty without advancing. Cursor-relative slicing preserves sequential traversal across page boundaries, empty pages, and nested fork offsets.

Ownership is explicit. Struct assignment creates a borrowed view whose lifetime is bounded by its owner. Every independent owner acquires a pin before handoff using Slice, Pin, or the copier, keeps its own struct copy, and disposes that handle exactly once. Concurrent owners can release their own handles while the remaining owners keep the immutable bytes alive. Serialization borrows the source; decoded/copied results belong to their caller. Code that acquires a result is responsible for releasing it, including when its subsequent work fails. Temporary writers used during payload copying are disposed when that operation fails, and sender-owned inputs remain available for retry.

Following maintainer direction, this revision uses the existing explicit pin/borrowing contract and coding discipline. It removes the earlier per-handle lease and operation-rollback machinery, including CopyContext.ArcBuffers and SerializerSession.ArcBuffers. General serializer and copy-context implementations match main. The focused tests cover borrowed struct assignments, independent concurrent Slice/Pin/copier owners, custom cleanup on failure, non-consuming serialization and retry, reference identity, and reader cursor/lifetime boundaries.

Public signatures and the length-prefixed wire format are preserved. General buffer bounds/lifetime correctness and raw lifetime tests were split into merged #11474; this branch builds on main fbe240c34e9a1389ca5ab2d52f292ed8749ea530.

Originally extracted from #10693, this layer serves independently owned pooled-byte serialization. Durable Messaging uses GC-owned byte[] envelope payloads and remains independent. Main's page-pool policy is preserved; RPC lifetimes and retention policy remain separate concerns.

Microsoft Reviewers: Open in CodeFlow

Copilot AI balanced review requested due to automatic review settings October 10, 2026 15:26

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.

🟡 Changes recommended

Arc readers can fail on valid buffers containing consecutive zero-length pages.

1 open finding
What changed in this PR

Adds ownership-aware serialization and copying for ArcBuffer, with lifetime fixes and focused tests.

Changes:

  • Adds an ArcBuffer codec and deep copier.
  • Improves empty-buffer, disposal, pinning, and reader-fork behavior.
  • Adds ownership/lifetime tests, documentation, and generated API updates.
File Description
test/​Orleans.Serialization.UnitTests/​BuiltInCodecTests.cs Recognizes ownership-specific codec tests.
test/​Orleans.Serialization.UnitTests/​ArcBufferLifetimeTests.cs Tests buffer and writer lifetimes.
test/​Orleans.Serialization.UnitTests/​ArcBufferCodecTests.cs Tests codec ownership and failure cases.
src/​Orleans.Serialization/​README.md Documents reference-counted buffer usage.
src/​Orleans.Serialization/​Codecs/​ArcBufferCodec.cs Implements serialization and deep copying.
src/​Orleans.Serialization/​Buffers/​Reader.cs Adds owned Arc slice reads and fixes fork offsets.
src/​Orleans.Serialization/​Buffers/​ArcBufferWriter.cs Improves empty-buffer and validity handling.
src/​Orleans.Serialization/​Buffers/​Adaptors/​BufferSliceReaderInput.cs Validates Arc input and exposes slicing.
src/​api/​Orleans.Serialization/​Orleans.Serialization.cs Updates the generated public API surface.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI balanced review requested due to automatic review settings October 10, 2026 15:34

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

The new valid owner-free buffer state conflicts with the non-nullable public First contract.

1 open finding
Previously missed (1)

In code that hasn't changed since last review

Medium severity Update ArcBuffer.First nullability contract for empty buffers

src/​Orleans.Serialization/​Buffers/​ArcBufferWriter.cs:1068

Making default/Empty a supported value means First is legitimately null for a valid ArcBuffer (as the new tests assert), but the public First field and constructor parameter still advertise non-nullable ArcBufferPage. Nullable-aware consumers can therefore dereference ArcBuffer.Empty.First without a warning. Please update the public nullability contract (and regenerate the API surface) so owner-free buffers expose ArcBufferPage?.

🧠 Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 10, 2026 15:53

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

Consecutive empty pages can break valid reads, and the new empty state has an incorrect nullability contract.

1 open finding
Previously missed (2)

In code that hasn't changed since last review

Medium severity Expose nullable First and constructor page contracts

src/​Orleans.Serialization/​Buffers/​ArcBufferWriter.cs:1068

Empty makes First == null a valid public state, but First and the constructor parameter still expose non-nullable ArcBufferPage. Nullable-aware callers therefore receive no warning before dereferencing ArcBuffer.Empty.First. Change both contracts to ArcBufferPage? and regenerate the API surface.

Medium severity Consume exact remaining page capacity before allocating

src/​Orleans.Serialization/​Buffers/​Reader.cs:536

Requesting exactly 4096 bytes causes ArcBufferWriter.GetSpan to allocate whenever the current page has exactly 4096 bytes left (sizeHint >= WriteCapacity). As a result, this copy path leaves one quarter of each 16 KiB pooled page unused and allocates roughly 33% more pages for large payloads. Obtain the current span first and cap the read size afterward so exact remaining capacity is consumed.

🧠 Review effort: Balanced

@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Code coverage

Metric Pull request
Lines 83.65% (119,265 / 142,577)
Branches 73.35% (35,567 / 48,488)

Report-only conclusion: current-main baseline stale.

The newest successful coverage run tested 880559d, not current main 3f42edf.

Coverage combines every CI test matrix job, including providers, CodeGen, .NET 8/10, Linux, Windows, and macOS, using canonical physical source and branch identities.

The comparison remains report-only while normal line and branch variance is calibrated.

Coverage details

Copilot AI balanced review requested due to automatic review settings October 10, 2026 16:10

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

The new valid empty state exposes a null page through a public member declared non-nullable.

1 open finding
Previously missed (1)

In code that hasn't changed since last review

Medium severity Annotate ArcBuffer.First as nullable

src/​Orleans.Serialization/​Buffers/​ArcBufferWriter.cs:1068

Empty makes a null First part of the valid public contract, but ArcBuffer.First and the constructor's first parameter are still declared non-nullable (and the generated API surface preserves that annotation). Nullable-aware consumers can therefore dereference ArcBuffer.Empty.First without a warning and get a NullReferenceException. Please annotate the page as ArcBufferPage? throughout the public and dependent internal API, then regenerate the API surface.

🧠 Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 10, 2026 16:21

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.

🟡 Changes recommended

Structurally invalid public ArcBuffer values can be serialized into malformed length-prefixed payloads.

2 open findings

🧠 Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 10, 2026 18:09
@ReubenBond
ReubenBond force-pushed the rb-arc-serialization-foundations branch from ef5a482 to 6b2e783 Compare October 10, 2026 18:09

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.

🟡 Changes recommended

Empty-page reads, malformed public buffer shapes, and the nullable First contract remain unresolved.

3 open findings

🧠 Review effort: Balanced

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

Several public writer operations still bypass the new disposed-state guard and throw the wrong exception.

0 open findings

3 resolved since last review
Previously missed (1)

In code that hasn't changed since last review

Medium severity Add disposed checks to slice and zero-length reader methods

src/​Orleans.Serialization/​Buffers/​ArcBufferWriter.cs:395

The disposed-state guard is only effective for methods which call ThrowIfPinnedPages. After Dispose() nulls the page fields, AdvanceReader(0), PeekSlice(0), and ConsumeSlice(0) still bypass this guard and dereference _readPage, producing NullReferenceException instead of the newly established ObjectDisposedException behavior. Add a shared disposed check to these public entry points as well.

🧠 Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 10, 2026 23:20

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

The reference-counting and pooled-page lifetime changes warrant final human validation despite strong focused coverage.

0 open findings

🧠 Review effort: Balanced

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.

🟡 Changes recommended

Enclosing operation failures leave acquired pins unreleased, and sequential Arc-backed owned reads repeatedly traverse consumed pages.

3 open issues

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 19, 2026 21:08
@ReubenBond
ReubenBond force-pushed the rb-arc-serialization-foundations branch from 1266efe to d7e1fc9 Compare October 19, 2026 21: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 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.

Create owned slices from the current backing page instead of rescanning consumed prefixes. Reuse the shared reader-to-writer copy helper and cover reference composition, cursor boundaries, and custom-codec local ownership cleanup.

Composite-failure rollback remains a separate ownership-design blocker; no borrowed-copy rollback assumption is introduced.
Copilot AI balanced review requested due to automatic review settings October 20, 2026 14:00

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.

Track codec-acquired owners within returned-value serializer and copy operations. Transfer ownership at successful outermost return and release failed-operation acquisitions through release state shared with custom cleanup.

Preserve caller ownership for direct low-level calls and in-place partial results, document Reset before failed-context reuse, and regenerate the ArcBuffer reference-layout marker using official GenAPI.
Copilot AI balanced review requested due to automatic review settings October 20, 2026 14:56

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.

AppendPinned adopts an independently pinned raw slice because the writer releases individual pages directly. Cover disposal, reader advancement, reset and writer survival across enclosing codec and copier failures.

Use relative empty-copy pin assertions, an exact Assert.Throws check for truncated empty input, and CI-compatible test whitespace.
Copilot AI balanced review requested due to automatic review settings October 20, 2026 19:59

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

Cross-cutting pooled-buffer ownership and nested rollback semantics warrant final human validation.

No open issues 🎉

3 resolved since last review

Review effort: Balanced

Remove per-handle leases and serializer/copy-context rollback tracking per maintainer direction. Struct assignment borrows; each independent concurrent owner acquires its own pin before handoff and disposes its own copy exactly once.

Keep non-consuming serialization, independently owned decoding/copying, cursor-local reads and temporary-output failure cleanup. Document ownership responsibilities, cover parallel disposal and sender retry, and remove tests of the retired aggregate rollback guarantee.
Copilot AI balanced review requested due to automatic review settings October 20, 2026 20:35

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

Pooled-page ownership and cursor changes warrant final human review, with current-revision test and coverage results unverified.

No open issues 🎉

Review effort: Balanced

@ReubenBond
ReubenBond merged commit a59a7bd into dotnet:main Oct 20, 2026
88 checks passed
@ReubenBond
ReubenBond deleted the rb-arc-serialization-foundations branch October 20, 2026 20:43
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 20, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants