Merge #994 fix: origin session waits for a delivery-window slot instead of resetting the stream when command acks meet a full window (cohort double-open race)

This commit is contained in:
Joseph Doherty
2026-12-03 12:30:13 -05:00
2 changed files with 102 additions and 9 deletions
@@ -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)
{