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
@@ -87,13 +87,22 @@ public sealed class LateWriteReservation : IDisposable
(string Driver, string Reference)? target)
{ _owner = owner; _retention = retention; _clock = clock; _target = target; }
public void Observe(Task<DataWriteOutcome> actualDriverTask, WriteClaim claim, IDisposable? lifetime = null)
/// <summary>
/// Transfers the reservation to <paramref name="actualDriverTask"/>. The returned task completes only after
/// the observation has fully settled: the late outcome recorded, <paramref name="lifetime"/> disposed, and
/// the physical slot (and target) released back to the observer. Anything that reacts to the physical
/// write finishing and may then reserve again — the actor replying to its caller or draining its write
/// queue — must wait for this task, not for <paramref name="actualDriverTask"/>; the two complete on
/// different continuations, so the driver task finishing does not mean the slot is free yet (#964).
/// The task can fault only if <paramref name="lifetime"/>'s dispose throws (the slot is still released).
/// </summary>
public Task Observe(Task<DataWriteOutcome> actualDriverTask, WriteClaim claim, IDisposable? lifetime = null)
{
ArgumentNullException.ThrowIfNull(actualDriverTask);
ArgumentNullException.ThrowIfNull(claim);
if (Interlocked.CompareExchange(ref _state, 1, 0) != 0) throw new InvalidOperationException("Reservation already consumed.");
var started = _clock.GetTimestamp();
_ = ObserveCore(actualDriverTask, claim, lifetime, started);
return ObserveCore(actualDriverTask, claim, lifetime, started);
}
private async Task ObserveCore(Task<DataWriteOutcome> task, WriteClaim claim, IDisposable? lifetime, long started)
@@ -1482,7 +1482,7 @@ public sealed class DriverInstanceActor : ReceiveActor, IWithTimers
var cancellation = new CancellationTokenSource(timeout, clock);
// This task represents the entire physical invocation, including synchronous work before a driver
// returns its task. Its permit and payload live until that invocation really exits.
reservation.Observe(physical.Task, msg.Claim, new WriteLifetime(msg.Ticket, cancellation));
var settled = reservation.Observe(physical.Task, msg.Claim, new WriteLifetime(msg.Ticket, cancellation));
var attempt = Task.Run(async () =>
{
try
@@ -1527,14 +1527,25 @@ public sealed class DriverInstanceActor : ReceiveActor, IWithTimers
});
try
{
var outcome = await attempt.WaitAsync(timeout + WriteAbandonGrace(timeout), clock).ConfigureAwait(false);
replyTo.Tell(outcome);
}
catch
{
var outcome = msg.Claim.Complete(RichRejected) ? RichRejected : RichUnknown;
if (outcome == RichRejected) physical.TrySetResult(outcome);
else msg.Claim.Complete(outcome);
DataWriteOutcome outcome;
try
{
outcome = await attempt.WaitAsync(timeout + WriteAbandonGrace(timeout), clock).ConfigureAwait(false);
}
catch
{
outcome = msg.Claim.Complete(RichRejected) ? RichRejected : RichUnknown;
if (outcome == RichRejected) physical.TrySetResult(outcome);
else msg.Claim.Complete(outcome);
}
// #964: when the physical write has finished, its late-write slot must be back BEFORE the caller
// hears the outcome or the actor frees the tag — both let a next write to the same target reach
// TryReserve, which otherwise could still see this write's slot (the observer releases it on its
// own continuation) and spuriously reject with "Outstanding driver write capacity exhausted".
// Settling after physical completion is short and bounded (record outcome, dispose, release). An
// abandoned, still-running physical write is deliberately NOT awaited: it keeps its slot and target.
if (physical.Task.IsCompleted)
await settled.ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);
replyTo.Tell(outcome);
}
finally { self.Tell(new WriteCompleted(msg.Ticket.Proof.DriverReference, incarnation)); }