fix(grpc-data): #1080 a held connection's stale mark is timed from the hold start, not the loop unwind

The held runner's stale timer was created only after its loop had unwound, so under load the
BadNoCommunication mark came a full stale timeout after a slow unwind - late, or skipped when the
MaxHold release (timed from the hold) ended the hold first. The timer now starts with the hold; a
mark due before the unwind waits for it (no late Good from that loop can follow the mark) and lands
at the unwind.

Tests (fake clock, ProjectedConditionAccess on ManualTimeProvider; the fixture can gate the runner's
lease release to model a slow unwind): the mark lands at the unwind when it fell due earlier, still
before MaxHold (failed before the fix: no mark, the release came first); and lands exactly at hold
start + stale timeout with a prompt unwind.
This commit is contained in:
Joseph Doherty
2026-12-03 16:47:41 -05:00
parent fe4bbbe256
commit 2ff8767487
3 changed files with 100 additions and 21 deletions
@@ -395,27 +395,32 @@ public sealed partial class ProjectedConditionAccess
/// <summary>
/// A4 review r2 S2 (under the owner's _gate, right after <see cref="Stop"/> without removal): the hold is bounded.
/// <paramref name="release"/> fires at <c>MaxHold</c> (the owner releases); after the loop has stopped, the held
/// conditions go <c>BadNoCommunication</c> on the connection's stale timeout — a hold is a stream gap (§5.1 T2: unknown,
/// never known-inactive). <see cref="EndHold"/> (takeover / release) disarms both.
/// <paramref name="release"/> fires at <c>MaxHold</c> (the owner releases); the held conditions go
/// <c>BadNoCommunication</c> on the connection's stale timeout — a hold is a stream gap (§5.1 T2: unknown, never
/// known-inactive). <see cref="EndHold"/> (takeover / release) disarms both.
/// <para>
/// #1080: the stale timeout runs from the hold's START, as MaxHold does. It used to be armed only once the loop had
/// unwound, so under load (a slow unwind) the mark came late, or the MaxHold release ended the hold first and the
/// mark never came. The mark still never lands before the unwind (a late Good from that loop could follow it): one
/// due earlier waits for the unwind and lands then.
/// </para>
/// </summary>
public void Hold(ITimer release)
{
lock (_staleGate) { _holding = true; _holdRelease = release; }
_ = _stopped.ContinueWith(_ =>
var stopped = _stopped;
var timeout = TimeSpan.FromMilliseconds(Math.Max(1, _mappings.Min(x => x.Mapping.ConditionStaleTimeoutMilliseconds)));
lock (_staleGate)
{
lock (_staleGate)
_holding = true; _holdRelease = release;
_holdStale = _owner._clock.CreateTimer(_ => _ = stopped.ContinueWith(_ =>
{
if (!_holding || _materialised.Count == 0) return;
var timeout = TimeSpan.FromMilliseconds(Math.Max(1, _mappings.Min(x => x.Mapping.ConditionStaleTimeoutMilliseconds)));
_holdStale = _owner._clock.CreateTimer(_ =>
{
// Under the same gate EndHold takes before any takeover, so a mark never lands after the successor began.
lock (_staleGate)
if (_holding) _owner._sink()?.MarkConnectionStale(ConnectionId);
}, null, timeout, Timeout.InfiniteTimeSpan);
}
}, TaskScheduler.Default);
// Under the same gate EndHold takes before any takeover, so a mark never lands after the successor began.
// The loop has unwound here, so its materialised set is settled; nothing held ⇒ nothing to mark.
lock (_staleGate)
if (_holding && _materialised.Count > 0) _owner._sink()?.MarkConnectionStale(ConnectionId);
}, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default),
null, timeout, Timeout.InfiniteTimeSpan);
}
}
/// <summary>A4 review r2 S2: the hold ended (taken over, released, disposed) — no stale mark or release fires after this.</summary>