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
@@ -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;