Skip to content

v4: RespRequestBuilderHolder - hold a part-built request across an await - #3422

Merged
mgravell merged 1 commit into
v4from
marc/v4-builder-store
Dec 4, 2026
Merged

mgravell merged 1 commit into
v4from
marc/v4-builder-store

Conversation

@mgravell

@mgravell mgravell commented Dec 4, 2026

Copy link
Copy Markdown
Member

RespRequestBuilder is a ref struct, so a command built with Compose can't be kept across an await. That was raised on #3364. This adds a holder for it:

using var holder = new RespRequestBuilderHolder();

var cmd = ctx.Compose($"{Set} {key} {value}");
holder.Store(ref cmd);                 // cmd is now empty

TimeSpan ttl = await GetTtlAsync();

holder.Take(out cmd);
cmd.AppendFormatted((RedisValue)"EX");
cmd.AppendFormatted((long)ttl.TotalSeconds);
bool ok = await ctx.SendAsync<bool>(ref cmd);

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, and using can't cover that. A single holder is one using target for the whole operation.

Store and Take are moves. Store resets the local to default, the same idiom Append already uses. Take leaves 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 Store until 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 a using variable because it's passed by ref. So a taken builder keeps a reference back to its holder:

  • when it grows, it hands the new buffer to the holder;
  • when it's sent or disposed, it releases the buffer through the 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:

  • taking from an empty holder;
  • storing an empty builder;
  • storing a new builder while one taken from this holder is still out, which would leave the one that's out with no owner;
  • storing a builder taken from a different holder.

A builder whose holder was disposed while it was out throws ObjectDisposedException when 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 var local is read-only, so calling Store on 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 struct local in an async method as long as no live value crosses an await; Store is what guarantees that. On C# 12 and earlier, the builder steps go in small synchronous helpers, as Compose already requires there. The docs cover both.

Docs

  • docs/Execute.md has a new section, "Holding a command across an await".
  • design/interpolated-resp-writer.md has a new §4.2 explaining the shape.

Tests

RespRequestBuilderHolderTests adds 15 tests, covering:

  • the inline async shape;
  • a growth while the builder is taken out, including a growth inside Append;
  • an exception while it's taken out;
  • one holder reused across a loop;
  • every misuse case.

As a mutation check, I removed each of five guards in turn: reporting a growth, the check in Complete, the check in Grow, the different-holder check, and the reset in Store. Each removal made at least one test fail.

Checks

Run Total Failed
net10.0 Debug 8248 0
net10.0 Release 8240 0
Holder tests on net481 15 0
Build.Tests 248 0

SanityChecks passes, and src builds clean on every TFM.

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.
@mgravell
mgravell merged commit d90c33c into v4 Dec 4, 2026
3 checks passed
@mgravell
mgravell deleted the marc/v4-builder-store branch December 4, 2026 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant