fix(grpc-data): #962 a modify's ack commit releasing a replaced item's interest mid-tick no longer resets the stream

The Runtime.Tests flake (Changing_the_filter_acknowledges_a_new_revision_before_its_snapshot) was a real
production race. DataSubscriptionSession.TickAsync captures the snapshot-complete interest-group leases
under the gate, awaits the snapshot/condition pumps, then reads each lease's changes. A Modify that replaces
an item is committed on the DELIVERY side (the ack's commit -> CommitStaged -> ReleaseItem), which disposes
the replaced item's SourceInterestLease. If that lands between the capture and the read, TryReadChanges
returns false exactly like a lost history and the session answered a successful modify with
Reset: "Source change history expired." (ack, then Reset instead of SnapshotBegin).

InterestGroup now carries Released (set under the gate in ReleaseItem before the lease is disposed); the
tick skips a failed read of a group the session itself released - the replacement has its own snapshot.
A failed read of a group the session still owns is still a reset.

Regression test holds a tick at the capture point (BeforeDirtyTickForTests), commits the modify under it
via a gated transport write, then lets the tick read: red 3/3 before, green after. Stress of the original
scenario (5000 iterations x 6 widths): 137/30000 Reset before, 0/30000 after.
This commit is contained in:
Joseph Doherty
2026-12-01 21:30:59 -05:00
parent 60e1328218
commit 23b28e001f
3 changed files with 148 additions and 4 deletions
@@ -102,6 +102,8 @@ internal sealed partial class DataSubscriptionSession : IDataSubscriptionTranspo
internal readonly Dictionary<BackingSourceKey, int> Sources = new();
internal LinkedListNode<InterestGroup>? Node;
internal bool SnapshotComplete;
/// <summary>Set under the session gate when the session itself releases the group (its last item went).</summary>
internal bool Released;
}
private sealed class SnapshotJob(SnapshotLease snapshot, IDisposable admission, InterestGroup group,
IReadOnlyList<Item> items, IReadOnlyList<PartitionWatermark> watermarks, bool temporaryInterest = false)
@@ -795,6 +797,7 @@ internal sealed partial class DataSubscriptionSession : IDataSubscriptionTranspo
if (_interestCursor == group.Node) _interestCursor = group.Node!.Next ?? _interests.First;
_interests.Remove(group.Node!);
if (_interestCursor == group.Node) _interestCursor = _interests.First;
group.Released = true;
group.Lease.Dispose();
}
}
@@ -857,23 +860,32 @@ internal sealed partial class DataSubscriptionSession : IDataSubscriptionTranspo
{ waiting.Ticket = null; waiting.WaitingNode = _waiting.AddLast(waiting.Handle.Handle); }
}
}
List<SourceInterestLease> interests = [];
List<InterestGroup> interests = [];
lock (_gate)
{
_interestCursor ??= _interests.First;
for (var count = 0; count < Math.Min(32, _interests.Count); count++)
{
var current = _interestCursor!;
if (current.Value.SnapshotComplete) interests.Add(current.Value.Lease);
if (current.Value.SnapshotComplete) interests.Add(current.Value);
_interestCursor = current.Next ?? _interests.First;
}
}
await PumpSnapshotsAsync().ConfigureAwait(false);
await PumpConditionsAsync().ConfigureAwait(false);
BeforeDirtyTickForTests?.Invoke(_outbound.Reader.Count, Volatile.Read(ref _queuedValues), interests.Count);
foreach (var interest in interests)
foreach (var group in interests)
{
if (!interest.TryReadChanges(32, out var changes)) { CloseWithReset("Source change history expired."); return; }
if (!group.Lease.TryReadChanges(32, out var changes))
{
// #962: a modify's ack commit runs on the delivery side and can release a group this tick captured
// above (the replaced item's interest); its disposed lease then reads like a reset. That group is gone
// by design and its replacement re-snapshots, so only a group the session still owns lost its history.
bool released;
lock (_gate) released = group.Released;
if (released) continue;
CloseWithReset("Source change history expired."); return;
}
foreach (var change in changes) MarkDirty(change.Ticket.Key);
}
var availableValues = AvailableValueSlots();