Respect IBufferWriter leases after commit - #69002
Open
DeagleGross wants to merge 3 commits into
Open
Conversation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 6750479f-a9b1-44b2-8f85-3fce698443e3
DeagleGross
requested review from
BrennanConroy,
SamMonoRT and
halter73
as code owners
September 2, 2026 16:49
Contributor
There was a problem hiding this comment.
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
_spanafterCommit()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.
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.
BufferWriter<T>retained the remainder of a span after committing bytes to the underlyingIBufferWriter<byte>. Subsequent writes could therefore use an expired buffer lease and callAdvanceagain without first callingGetSpanorGetMemory.Clear the cached span after committing and reacquire a buffer before the next non-empty write.
Fixes #68148