Skip to content

Respect IBufferWriter leases after commit - #69002

Open
DeagleGross wants to merge 3 commits into
dotnet:mainfrom
DeagleGross:deaglegross-fix-pipewriter-advance
Open

Respect IBufferWriter leases after commit#69002
DeagleGross wants to merge 3 commits into
dotnet:mainfrom
DeagleGross:deaglegross-fix-pipewriter-advance

Conversation

@DeagleGross

Copy link
Copy Markdown
Member

BufferWriter<T> retained the remainder of a span after committing bytes to the underlying IBufferWriter<byte>. Subsequent writes could therefore use an expired buffer lease and call Advance again without first calling GetSpan or GetMemory.

Clear the cached span after committing and reacquire a buffer before the next non-empty write.

Fixes #68148

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 6750479f-a9b1-44b2-8f85-3fce698443e3
@DeagleGross DeagleGross self-assigned this Sep 2, 2026
Copilot AI lite review requested due to automatic review settings September 2, 2026 16:49
@DeagleGross DeagleGross added feature-kestrel area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions labels Sep 2, 2026

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

🟢 Approval recommended

The change directly enforces the IBufferWriter<T> leasing contract and is covered by targeted tests that validate the corrected post-commit behavior.

Review tier: Lite
Findings: None

What changed in this PR

This PR fixes a contract violation in BufferWriter<T> by ensuring it does not retain and reuse an IBufferWriter<byte>-leased span after committing (i.e., after calling the underlying writer’s Advance). This aligns BufferWriter<T> behavior with the IBufferWriter<T> contract and prevents writing into expired leases across multiple commits.

Changes:

  • Clear the cached _span after Commit() publishes buffered bytes to the underlying writer, forcing reacquisition before subsequent writes.
  • Ensure Write(ReadOnlySpan<byte>) reacquires a span on the first non-empty write after a commit.
  • Add/adjust tests to validate the new “reacquire after commit” behavior and update PipeWriter-based expectations.
File Description
src/​Shared/​ServerInfrastructure/​BufferWriter.cs Clears cached span on commit and ensures reacquisition before non-empty writes after a commit.
src/​Shared/​test/​Shared.Tests/​ServerInfrastructure/​BufferWriterTests.cs Adds a strict IBufferWriter<byte> test validating that post-commit writes require a new buffer acquisition.
src/​Servers/​Kestrel/​Core/​test/​BufferWriterWithPipeWriterTests.cs Updates existing PipeWriter tests to reflect that Span is empty after Commit() and renames the test class accordingly.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions feature-kestrel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Kestrel writes response body into a transport PipeWriter buffer it has already Advanced past (no intervening GetSpan/GetMemory)

2 participants