Repository navigation
v4: RespRequestBuilderHolder - hold a part-built request across an await - #3422
Merged
Merged
Conversation
RespRequestBuilder is a ref struct, so a composition that needs an await mid-way had nowhere to park. RespRequestBuilderHolder takes it with Store(ref builder) and gives it back with Take(out builder); both are moves, so exactly one place owns the pooled buffer at a time. The holder owns the buffer from the first Store until the send, including while the builder is taken out: a taken builder reports a growth to its holder and releases through it, so one `using` on the holder covers a throw anywhere in between, on either side of a Take. Misuse throws rather than sharing a buffer.
This was referenced Dec 4, 2026
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.
RespRequestBuilderis aref struct, so a command built withComposecan't be kept across anawait. That was raised on #3364. This adds a holder for it:Shape
One holder per operation. I considered a dehydrated object created at each
await, but in a loop that would be a different object on each pass, andusingcan't cover that. A single holder is oneusingtarget for the whole operation.StoreandTakeare moves.Storeresets the local todefault, the same idiomAppendalready uses.Takeleaves the holder empty. So only one place owns the pooled buffer at any moment, and a stale copy on the stack is just an empty builder. That makes a generation token unnecessary.The holder owns the buffer from the first
Storeuntil the send, even while the builder is taken out. The time the builder is out is when exceptions happen (a failed append, a cancelled send), and the taken builder can't be ausingvariable because it's passed byref. So a taken builder keeps a reference back to its holder:Disposing the holder therefore returns the current buffer exactly once, whether or not the builder is out at the time.
Misuse throws instead of letting two places share a buffer. These throw
InvalidOperationException:A builder whose holder was disposed while it was out throws
ObjectDisposedExceptionwhen it's sent, or when it next grows. A plain write into the returned array between those two points isn't detected, because that would need a check on every append. The docs say to dispose the holder last.The holder is a class, not a struct. A
using varlocal is read-only, so callingStoreon a struct holder would mutate a defensive copy instead of the holder.Language version
The inline shape above compiles from C# 13, which allows a
ref structlocal in anasyncmethod as long as no live value crosses anawait;Storeis what guarantees that. On C# 12 and earlier, the builder steps go in small synchronous helpers, asComposealready requires there. The docs cover both.Docs
docs/Execute.mdhas a new section, "Holding a command across anawait".design/interpolated-resp-writer.mdhas a new §4.2 explaining the shape.Tests
RespRequestBuilderHolderTestsadds 15 tests, covering:Append;As a mutation check, I removed each of five guards in turn: reporting a growth, the check in
Complete, the check inGrow, the different-holder check, and the reset inStore. Each removal made at least one test fail.Checks
SanityChecks passes, and
srcbuilds clean on every TFM.