fix(grpc-data): #964 rich write releases its late-write slot before replying / freeing the tag

The late-write observer released the physical slot on its own continuation of
the driver task, while RunRichWriteAsync replied and told WriteCompleted on
another. A next write to the same target (queued, or sent right after the
reply) could reach TryReserve before the release and be rejected with
'Outstanding driver write capacity exhausted' although nothing was
outstanding. LateWriteReservation.Observe now returns a task that completes
after the slot is released; the actor awaits it (when the physical write has
finished) before replying and freeing the tag. The flaky unit test waited on
the lifetime counter, which is disposed just before the release - it now
awaits the settled task.

Repro: 200 sequential same-target actor writes failed 3/3 runs (no load);
0 failures in 15 runs at load avg ~468. Unit signal: lifetime-wait 22-39 /
2M false 'slot busy', settled-wait 0/2M.
This commit is contained in:
Joseph Doherty
2026-12-02 13:10:20 -05:00
parent 366a92d768
commit bcd4a9aa29
5 changed files with 69 additions and 26 deletions
@@ -256,6 +256,24 @@ public sealed class GrpcDataDriverOutcomeTests : RuntimeActorTestBase
secondClaim.IsDispatched.ShouldBeFalse();
}
[Fact]
public async Task Sequential_writes_to_one_target_never_see_the_previous_writes_physical_slot()
{
// #964: the reply and WriteCompleted used to race the late-write observer's slot release, so a
// caller that wrote the same tag again right after its previous reply could be rejected with
// "Outstanding driver write capacity exhausted" although nothing was outstanding any more.
var driver = new RichDriver();
var actor = Connect(driver, new RecordingGate(), new LateWriteObserver(1, TimeSpan.FromMinutes(1)), maxWrites: 1);
for (var i = 0; i < 200; i++)
{
var (ticket, claim) = NewWrite($"seq-{i}", i);
var outcome = await actor.Ask<DataWriteOutcome>(new DriverInstanceActor.WriteAttributeWithOutcome(ticket, claim),
Timeout, TestContext.Current.CancellationToken);
outcome.State.ShouldBe(DataWriteState.DriverConfirmedSuccess, $"write {i}: {outcome.Diagnostic}");
}
driver.Calls.ShouldBe(200);
}
[Fact]
public async Task Legacy_good_without_an_explicit_completion_guarantee_is_unconfirmed()
{
@@ -30,10 +30,14 @@ public sealed class GrpcDataLateWriteTests
var source = new TaskCompletionSource<DataWriteOutcome>(TaskCreationOptions.RunContinuationsAsynchronously);
var lifetime = new CountingLifetime();
observer.TryReserve(out var reservation).ShouldBeTrue();
reservation!.Observe(source.Task, claim, lifetime);
var settled = reservation!.Observe(source.Task, claim, lifetime);
observer.TryReserve(out _).ShouldBeFalse();
source.SetResult(Success);
await WaitUntil(() => claim.CurrentOutcome == Success && lifetime.Count == 1);
// #964: the settled task, not the lifetime, is the "slot is free" signal — the slot is released after
// the lifetime, on the same continuation.
await settled.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
claim.CurrentOutcome.ShouldBe(Success);
lifetime.Count.ShouldBe(1);
(await claim.Completion).ShouldBe(Unknown);
observer.TryReserve(out var next).ShouldBeTrue();
next!.Dispose();
@@ -48,12 +52,14 @@ public sealed class GrpcDataLateWriteTests
var source = new TaskCompletionSource<DataWriteOutcome>(TaskCreationOptions.RunContinuationsAsynchronously);
var lifetime = new CountingLifetime();
observer.TryReserve(out var reservation).ShouldBeTrue();
reservation!.Observe(source.Task, claim, lifetime);
var settled = reservation!.Observe(source.Task, claim, lifetime);
clock.Advance(TimeSpan.FromSeconds(11));
observer.TryReserve(out _).ShouldBeFalse();
lifetime.Count.ShouldBe(0);
settled.IsCompleted.ShouldBeFalse();
source.SetResult(Success);
await WaitUntil(() => lifetime.Count == 1);
await settled.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
lifetime.Count.ShouldBe(1);
claim.CurrentOutcome.ShouldBe(Unknown);
observer.TryReserve(out var next).ShouldBeTrue();
next!.Dispose();
@@ -66,9 +72,11 @@ public sealed class GrpcDataLateWriteTests
var claim = DispatchedClaim();
var lifetime = new CountingLifetime();
observer.TryReserve(out var reservation).ShouldBeTrue();
reservation!.Observe(Task.FromResult(Success), claim, lifetime);
var settled = reservation!.Observe(Task.FromResult(Success), claim, lifetime);
reservation.Dispose();
await WaitUntil(() => lifetime.Count == 1 && claim.CurrentOutcome == Success);
await settled.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
lifetime.Count.ShouldBe(1);
claim.CurrentOutcome.ShouldBe(Success);
observer.TryReserve(out var next).ShouldBeTrue();
next!.Dispose();
next.Dispose();
@@ -86,22 +94,17 @@ public sealed class GrpcDataLateWriteTests
var source = new TaskCompletionSource<DataWriteOutcome>(TaskCreationOptions.RunContinuationsAsynchronously);
var lifetime = new CountingLifetime();
observer.TryReserve(out var reservation).ShouldBeTrue();
reservation!.Observe(source.Task, claim, lifetime);
var settled = reservation!.Observe(source.Task, claim, lifetime);
clock.Advance(TimeSpan.FromMinutes(2));
source.SetResult(Success);
await WaitUntil(() => lifetime.Count == 1);
await settled.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
lifetime.Count.ShouldBe(1);
var duplicate = registry.Claim("p", "op", "inc", new byte[32]);
duplicate.Kind.ShouldBe(WriteClaimKind.Existing);
duplicate.Claim!.CurrentOutcome.ShouldBe(Success);
(await duplicate.Claim.Completion).ShouldBe(Unknown);
}
private static async Task WaitUntil(Func<bool> condition)
{
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(5));
while (!condition()) await Task.Delay(1, timeout.Token);
}
private sealed class CountingLifetime : IDisposable
{
private int _count;
@@ -541,7 +541,7 @@ public sealed class GrpcDataWriteAdmissionTests
var operation = ticket.Proof.Admission.OperationId;
_observer.TryReserve(out var reservation).ShouldBeTrue();
var physical = new TaskCompletionSource<DataWriteOutcome>(TaskCreationOptions.RunContinuationsAsynchronously);
reservation.ShouldNotBeNull().Observe(physical.Task, claim, ticket);
var settled = reservation.ShouldNotBeNull().Observe(physical.Task, claim, ticket);
_dispatched.Enqueue(operation);
claim.TryMarkDispatched().ShouldBeTrue();
Entered.TrySetResult();
@@ -550,6 +550,8 @@ public sealed class GrpcDataWriteAdmissionTests
? new DataWriteOutcome(DataWriteState.DriverConfirmedFailure, 0x80000000u, "device rejected")
: new DataWriteOutcome(DataWriteState.DriverConfirmedSuccess, 0, null);
physical.TrySetResult(outcome);
// As the driver host does (#964): the slot is back before the outcome is returned.
await settled;
return outcome;
}
}