fix(grpc-data): A3 verification r2 — N1 runner predecessor chain dropped after hand-over + stopped runner clears its record sets; correct the SentMessageQueue clone comment and pin the EventFieldList MemberwiseClone dependency

This commit is contained in:
Joseph Doherty
2026-11-24 22:26:09 -05:00
parent 5bece2a5a5
commit c8b35d494f
5 changed files with 94 additions and 14 deletions
@@ -455,11 +455,14 @@ internal sealed record ProjectedEventMarker(object? Identity, NodeId ConditionId
}
/// <summary>
/// A3 (§5.4): an <see cref="EventFieldList"/> carrying its <see cref="ProjectedEventMarker"/>. The SDK never clones a
/// queued list on its way to the wire (SDK 2.0.1: <c>QueueEvent(EventFieldList)</c> enqueues the instance,
/// <c>Publish</c> dequeues it, <c>ConstructMessage</c> places it, Republish returns the stored message), so
/// <c>ProjectedNotificationRewriter</c> sees this instance on every Publish and Republish and drops it when the re-check
/// fails.
/// A3 (§5.4): an <see cref="EventFieldList"/> carrying its <see cref="ProjectedEventMarker"/>. The rewriter does NOT see
/// the queued instance: SDK 2.0.1 <c>SentMessageQueue</c> stores the constructed message and returns a
/// <c>CoreUtils.Clone</c> of it on Publish and on Republish. The gate still works because
/// <see cref="EventFieldList.Clone"/> is <c>MemberwiseClone</c>, so every copy keeps this subclass, its
/// <see cref="Marker"/> and its handle, and <c>ProjectedNotificationRewriter</c> drops the copy when the re-check fails.
/// That clone dependency is pinned by
/// <c>ImportedConditionClassifierTests.Cloning_a_marked_event_list_the_way_the_SDK_does_keeps_the_subclass_and_the_marker</c>:
/// an SDK that cloned through the activator would turn it red.
/// </summary>
/// <remarks>
/// A3 review round 2 (root cause of the OI refresh failures): SDK 2.0.1 POOLS <see cref="EventFieldList"/> process-wide
@@ -143,7 +143,8 @@ public sealed partial class ProjectedConditionAccess
private readonly CancellationTokenSource _stop = new();
private readonly object _statusGate = new();
private readonly object _staleGate = new();
private readonly ConnectionRunner? _predecessor;
/// <summary>Verification r2 N1: dropped right after the hand-over, so runners never chain (R3 → R2 → R1 …).</summary>
private ConnectionRunner? _predecessor;
private Task _loop = Task.CompletedTask;
private Task _stopped = Task.CompletedTask;
private MappingState[] _states = [];
@@ -177,17 +178,27 @@ public sealed partial class ProjectedConditionAccess
public void Start() => _loop = Task.Run(async () =>
{
if (_predecessor is { } previous)
{
// S3/S4: never overlap the previous runner of this connection — its in-flight Publish or its removal would
// otherwise race this runner's materialisation (an orphaned key, or a fresh key removed by the old continuation).
try { await previous.Stopped.ConfigureAwait(false); } catch (Exception ex) { _owner._log.LogDebug(ex, "Imported conditions: predecessor stop faulted"); }
_materialised = previous._materialised;
previous._materialised = new HashSet<string>(StringComparer.Ordinal);
}
await HandOverAsync().ConfigureAwait(false);
await RunAsync(_stop.Token).ConfigureAwait(false);
});
/// <summary>
/// S3/S4: never overlap the previous runner of this connection — its in-flight Publish or its removal would otherwise
/// race this runner's materialisation (an orphaned key, or a fresh key removed by the old continuation). The
/// materialised set moves over only after the predecessor stopped. Verification r2 N1: the predecessor reference is
/// dropped once the hand-over is done (its own method, so no hoisted local outlives it either) — otherwise every
/// linger expiry or mapping change would add a link to a chain that keeps each old runner reachable.
/// </summary>
private async Task HandOverAsync()
{
var previous = _predecessor;
if (previous is null) return;
try { await previous.Stopped.ConfigureAwait(false); } catch (Exception ex) { _owner._log.LogDebug(ex, "Imported conditions: predecessor stop faulted"); }
_materialised = previous._materialised;
previous._materialised = new HashSet<string>(StringComparer.Ordinal);
_predecessor = null;
}
public void Stop(bool removeConditions)
{
_stop.Cancel();
@@ -195,6 +206,8 @@ public sealed partial class ProjectedConditionAccess
lock (_staleGate) { _staleGeneration++; _stale?.Dispose(); _stale = null; }
_stopped = _loop.ContinueWith(_ =>
{
// Verification r2 N1: a stopped runner keeps no stream's record sets (it may sit in _stopping until the next demand).
lock (_statusGate) _states = [];
if (!removeConditions) return;
var keys = _materialised.ToArray();
if (keys.Length > 0 && _owner._sink() is { } sink) sink.RemoveConditions(ConnectionId, keys);
@@ -290,6 +290,13 @@ public sealed partial class ProjectedConditionAccess : IProjectedConditionAccess
lock (_gate) return _runners.ContainsKey(connectionId);
}
/// <summary>Test observation (verification r2 N1): a weak reference to the connection's current (or still stopping) runner.</summary>
public WeakReference? RunnerReference(string connectionId)
{
lock (_gate)
return _runners.TryGetValue(connectionId, out var runner) || _stopping.TryGetValue(connectionId, out runner) ? new WeakReference(runner) : null;
}
/// <summary>Test observation: snapshot barriers a connection completed.</summary>
public int BarriersCompleted(string connectionId)
{