fix(grpc-data): a command ack that meets a full delivery window waits for the reader instead of resetting the stream
Root cause of the ~1-in-20 flake in GrpcDataCohortScaleTests.R2_a_cohort_above_1024_items_opens_on_one_origin_stream (Opens = 2): a production race in the origin DataSubscriptionSession, not a fixture artifact. The session's bounded outbound window (MaxPendingFrames) is shared by values, snapshot controls and command acks. The snapshot pump leaves exactly one slot "for command acknowledgements", but a peer may have up to MaxCommands commands in flight (the #994 projected cohort keeps 4 outstanding). When the reader was a few frames behind, an open/add's snapshot filled the window, the next add's ack took the reserved slot, and the following ack's TryWrite failed; EnqueueAsync then CloseWithReset("Pending control capacity exhausted.") the whole stream, and the cohort re-opened all 1100 items on a second origin stream. Instrumented repro: the failing run's trace shows exactly that reset on the 1100-item cohort's first stream, then a second COHORT OPEN with epoch=2; the reset never appeared in passing runs. Fix: EnqueueAsync (production callers all on the command worker) now awaits the window when it is full instead of resetting — backpressure on the worker, never on the reader. A reader that never drains is still bounded by the ack deadline, the session lifetime and the bounded command queue. The values cap (MaxPendingFrames - 2) still resets. Regression: GrpcDataDeliveryWindowTests drives an 8-frame window with an unread open + 3 adds on a frozen clock; the OutboundFullForTests seam proves an ack met a full window. Before the fix the stream ended with that reset; after it every ack, SnapshotEnd and baseline arrives with no Reset. Verified: GrpcDataCohortScaleTests + GrpcDataDeliveryWindowTests 30/30 consecutive passes (plus 30/30 instrumented with zero "Pending control capacity" resets); full Runtime.Tests (Category!=SlowUnit) 2283 passed / 0 failed.
This commit is contained in:
@@ -51,6 +51,8 @@ internal sealed partial class DataSubscriptionSession : IDataSubscriptionTranspo
|
||||
internal Action? SnapshotEndCommittedForTests { get; set; }
|
||||
internal Action<int, int, int>? BeforeDirtyTickForTests { get; set; }
|
||||
internal Action? AfterTickForTests { get; set; }
|
||||
/// <summary>Test seam: a frame met a full delivery window (the worker is about to wait for the reader).</summary>
|
||||
internal Action? OutboundFullForTests { get; set; }
|
||||
internal Action<DataItemHandle, long>? OnCandidateQueuedForTests { get; set; }
|
||||
|
||||
private sealed class SharedSessionLease(IDisposable lease) : IDisposable
|
||||
@@ -982,7 +984,8 @@ internal sealed partial class DataSubscriptionSession : IDataSubscriptionTranspo
|
||||
{ CloseWithReset("Snapshot catch-up expired; retry later."); return; }
|
||||
if (!job.Started)
|
||||
{
|
||||
// Leave a control slot available for command acknowledgements. A dequeued
|
||||
// Leave a control slot available for command acknowledgements (an optimisation: an acknowledgement that
|
||||
// still finds the window full waits for the reader in EnqueueAsync). A dequeued
|
||||
// frame queues another tick, so a blocked boundary remains resumable.
|
||||
if (_outbound.Reader.Count >= Math.Max(1, _limits.MaxPendingFrames - 1)) return;
|
||||
await EnqueueAsync(Control(DataStreamControlKind.SnapshotBegin, job.Watermarks, job.Snapshot,
|
||||
@@ -1214,18 +1217,26 @@ internal sealed partial class DataSubscriptionSession : IDataSubscriptionTranspo
|
||||
return Math.Min(valueSlots, channelSlots);
|
||||
}
|
||||
|
||||
private Task EnqueueAsync(PendingDelivery delivery)
|
||||
// Production callers all run on the command worker (commands, ticks, the snapshot pump), so waiting here backpressures
|
||||
// that worker — never the reader, which drains the window independently.
|
||||
private async Task EnqueueAsync(PendingDelivery delivery)
|
||||
{
|
||||
if (Volatile.Read(ref _closed) != 0 || Volatile.Read(ref _closing) != 0) { delivery.Dispose(); return Task.CompletedTask; }
|
||||
if (Volatile.Read(ref _closed) != 0 || Volatile.Read(ref _closing) != 0) { delivery.Dispose(); return; }
|
||||
if (delivery.IsValue && Interlocked.Increment(ref _queuedValues) > Math.Max(1, _limits.MaxPendingFrames - 2))
|
||||
{
|
||||
Interlocked.Decrement(ref _queuedValues); delivery.Dispose(); CloseWithReset("Pending value capacity exhausted."); return Task.CompletedTask;
|
||||
Interlocked.Decrement(ref _queuedValues); delivery.Dispose(); CloseWithReset("Pending value capacity exhausted."); return;
|
||||
}
|
||||
if (!_outbound.Writer.TryWrite(delivery))
|
||||
{
|
||||
Dequeued(delivery); delivery.Dispose(); CloseWithReset("Pending control capacity exhausted.");
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
if (_outbound.Writer.TryWrite(delivery)) return;
|
||||
// A full window is a reader a few frames behind, not a fault. The snapshot pump leaves only one slot for command
|
||||
// acknowledgements, but the peer may have up to MaxCommands commands in flight (a projected cohort keeps four), so a
|
||||
// second acknowledgement produced while a snapshot filled the window found it full and this used to reset the whole
|
||||
// stream ("Pending control capacity exhausted.") — the cohort then re-opened every item on a second origin stream.
|
||||
// Wait for the reader instead: every dequeue frees a slot, and a reader that never drains is still bounded by the
|
||||
// acknowledgement deadline, the session lifetime and the command queue (each of which Stops the session).
|
||||
OutboundFullForTests?.Invoke();
|
||||
try { await _outbound.Writer.WriteAsync(delivery, _stop.Token).ConfigureAwait(false); }
|
||||
catch (Exception ex) when (ex is OperationCanceledException or ChannelClosedException)
|
||||
{ Dequeued(delivery); delivery.Dispose(); }
|
||||
}
|
||||
private void Dequeued(PendingDelivery delivery)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user