fix(grpc-data): #967 a rich write whose invoker fails before dispatch on an externally-rejected claim releases its slot

When the invoker threw without running the dispatch delegate (open breaker /
bulkhead) after the claim had already been completed RejectedBeforeDispatch
by someone else, Complete(RichRejected) returned false, the path answered
OutcomeUnknown and never completed the physical task. The late-write slot,
the (driver, reference) target and the ticket's byte lease stayed reserved
for the process lifetime, so every later write to that tag was refused as
'Outstanding driver write capacity exhausted'.

Both catch paths now go through FailRichAttempt: an undispatched terminal
claim can never dispatch (dispatch is a CAS from pending), so the physical
task completes with the claim's pre-dispatch outcome and the caller hears
RejectedBeforeDispatch. Only a dispatched claim stays Unknown and is left
to the driver's own completion.
This commit is contained in:
Joseph Doherty
2026-12-02 13:19:44 -05:00
parent 7c4a070cc1
commit 8d6b64393e
2 changed files with 60 additions and 7 deletions
@@ -231,6 +231,34 @@ public sealed class GrpcDataDriverOutcomeTests : RuntimeActorTestBase
driver.Calls.ShouldBe(0);
}
[Fact]
public async Task Invoker_failing_before_the_call_site_on_an_externally_rejected_claim_releases_the_slot_and_target()
{
// #967: the invoker (e.g. an open breaker) throws without ever running the dispatch delegate, after the
// claim was already completed RejectedBeforeDispatch by someone else. Nothing reached the driver, but the
// physical task was never completed, so the late-write slot, the target and the ticket stayed reserved
// for the process lifetime and every later write to the tag was refused as capacity exhausted.
var driver = new RichDriver();
var invoker = new PausedWriteInvoker { FailFirstWithoutCalling = true };
var actor = Connect(driver, new RecordingGate(), new LateWriteObserver(1, TimeSpan.FromMinutes(1)), invoker);
var (ticket, claim) = NewWrite("breaker-open", 1);
var response = actor.Ask<DataWriteOutcome>(new DriverInstanceActor.WriteAttributeWithOutcome(ticket, claim), Timeout, TestContext.Current.CancellationToken);
await invoker.Entered.Task.WaitAsync(Timeout, TestContext.Current.CancellationToken);
claim.Complete(new(DataWriteState.RejectedBeforeDispatch, 0x800A0000u, "deadline before dispatch")).ShouldBeTrue();
invoker.Release.TrySetResult();
// Nothing was dispatched, so the caller hears the claim's own pre-dispatch rejection, not "unknown".
(await response.WaitAsync(Timeout, TestContext.Current.CancellationToken)).State.ShouldBe(DataWriteState.RejectedBeforeDispatch);
claim.IsDispatched.ShouldBeFalse();
driver.Calls.ShouldBe(0);
var (nextTicket, nextClaim) = NewWrite("after-breaker", 2);
var next = await actor.Ask<DataWriteOutcome>(new DriverInstanceActor.WriteAttributeWithOutcome(nextTicket, nextClaim),
Timeout, TestContext.Current.CancellationToken);
next.State.ShouldBe(DataWriteState.DriverConfirmedSuccess, next.Diagnostic);
driver.Calls.ShouldBe(1);
}
[Fact]
public async Task Queued_write_revalidates_after_an_earlier_write_finishes()
{
@@ -688,6 +716,10 @@ public sealed class GrpcDataDriverOutcomeTests : RuntimeActorTestBase
public TaskCompletionSource Entered { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
public TaskCompletionSource Release { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
public bool SawIdempotent { get; private set; }
/// <summary>When set, the FIRST write call fails after release without ever invoking the call site — the
/// shape of an open circuit breaker / bulkhead rejection. Later calls pass straight through.</summary>
public bool FailFirstWithoutCalling { get; init; }
private int _writeCalls;
public ValueTask<TResult> ExecuteAsync<TResult>(DriverCapability capability, string hostName,
Func<CancellationToken, ValueTask<TResult>> callSite, CancellationToken cancellationToken) => callSite(cancellationToken);
public ValueTask ExecuteAsync(DriverCapability capability, string hostName,
@@ -696,8 +728,11 @@ public sealed class GrpcDataDriverOutcomeTests : RuntimeActorTestBase
Func<CancellationToken, ValueTask<TResult>> callSite, CancellationToken cancellationToken)
{
SawIdempotent = isIdempotent;
var first = Interlocked.Increment(ref _writeCalls) == 1;
if (FailFirstWithoutCalling && !first) return await callSite(cancellationToken);
Entered.TrySetResult();
await Release.Task;
if (FailFirstWithoutCalling) throw new InvalidOperationException("circuit open");
return await callSite(cancellationToken);
}
}