fix(galaxy): #991 review — a re-advise pass never dials on a swapped session; dropped-event resync is rate-limited and coalesced

- ReadviseSubscriptionAsync: a reconnect completing between two subscriptions of a pass
  (dropped-event resync loop had no per-iteration check) sent the next SubscribeBulk to the
  NEW gateway session under the OLD session number; TryRebind refused it (SessionChanged)
  and the live new-session advises were never bound or released, then duplicated by the
  replay. The pass now refuses to dial once its session moved. Releasing afterwards was
  rejected: once the session moved, which session issued the reply's handles is ambiguous and
  old numbers are reused (#861); not dialling is the unambiguous fix.
- Dropped-event resync passes are at least 5 s apart (DropResyncMinInterval); drops reported
  meanwhile coalesce into the next pass, so repeated overflow bursts no longer churn
  re-advises into the overflowing channel.
- Test seams: ResyncDroppedHandlesForTestsAsync, NotifyEventsDroppedForTests,
  DropResyncMinIntervalForTests; AdvisingSubscriber gained after-subscribe/unsubscribe hooks.
This commit is contained in:
Joseph Doherty
2026-12-03 12:07:28 -05:00
parent 5dcb766fd1
commit eb78875c24
3 changed files with 137 additions and 1 deletions
@@ -1650,6 +1650,16 @@ public sealed class GalaxyDriver
IReadOnlyList<SubscribeResult> results;
using (_subscriptions.BeginPendingSubscribe())
{
// #991 review: a pass whose session is gone must not dial. A reconnect that completes between two
// subscriptions of one pass (the dropped-event resync loop, or the replay loop between its own check and
// here) left this SubscribeBulk going to the NEW gateway session under the OLD session number: the rebind
// below refused it (SessionChanged) and the fresh, live advises were never bound or released — leaked on
// the gateway, then duplicated by the new session's replay. Releasing them afterwards is not an option in
// general: once the session has moved, which session issued a reply's handles is ambiguous, and an
// old-session number may be reused by the new one (#861). Not dialling is the only unambiguous fix. The
// check and the dial are synchronous back to back; ReopenAsync's session swap awaits network I/O between
// its two fences, so a swap cannot complete in between.
if (_subscriptions.CurrentSession != session) return (false, 0);
results = await _subscriber!
.SubscribeBulkAsync(refs, _options.MxAccess.PublishingIntervalMs, cancellationToken)
.ConfigureAwait(false);
@@ -2942,6 +2952,41 @@ public sealed class GalaxyDriver
private int _droppedSession;
private int _dropResyncRunning;
/// <summary>
/// #991 review: the floor between two dropped-event resync passes. Repeated overflow bursts used to start a
/// pass as soon as the previous one finished — re-advising (and pushing initial values into) the very channel
/// that was overflowing. Drops reported inside the window coalesce into the next pass.
/// </summary>
internal static readonly TimeSpan DefaultDropResyncMinInterval = TimeSpan.FromSeconds(5);
private TimeSpan _dropResyncMinInterval = DefaultDropResyncMinInterval;
// Stopwatch timestamp of the last resync pass start (0 = none yet). Worker-owned.
private long _lastDropResyncTimestamp;
/// <summary>Test seam: the floor between two dropped-event resync passes.</summary>
internal TimeSpan DropResyncMinIntervalForTests
{
get => _dropResyncMinInterval;
set => _dropResyncMinInterval = value;
}
/// <summary>Test seam: report dropped events for <paramref name="itemHandles"/> as the current pump would.</summary>
/// <param name="itemHandles">The handles whose events were dropped.</param>
internal void NotifyEventsDroppedForTests(IReadOnlyCollection<int> itemHandles)
{
EventPump? pump;
lock (_pumpLock) pump = _eventPump;
OnEventPumpEventsDropped(pump, itemHandles);
}
/// <summary>Test seam: one dropped-event resync pass, run inline.</summary>
/// <param name="itemHandles">The dropped handles.</param>
/// <param name="session">The session that issued them.</param>
/// <returns>A task completing when the pass does.</returns>
internal Task ResyncDroppedHandlesForTestsAsync(IReadOnlyCollection<int> itemHandles, int session) =>
ResyncDroppedHandlesAsync(itemHandles, session);
// Serialises the same-session re-advise passes (replay-gap re-snapshot, dropped-event resync): both rebind a
// subscription from a snapshot of its bindings, so two interleaved passes could rebind over each other and
// release handles the other one bound.
@@ -2976,6 +3021,15 @@ public sealed class GalaxyDriver
{
while (true)
{
// Rate limit (#991 review): at most one pass per DropResyncMinInterval; drops reported while waiting
// join the pending set and ride the next pass together.
var last = Volatile.Read(ref _lastDropResyncTimestamp);
if (last != 0)
{
var wait = _dropResyncMinInterval - System.Diagnostics.Stopwatch.GetElapsedTime(last);
if (wait > TimeSpan.Zero) await Task.Delay(wait).ConfigureAwait(false);
}
int[] batch;
int batchSession;
lock (_droppedLock)
@@ -2990,6 +3044,7 @@ public sealed class GalaxyDriver
_droppedPending.Clear();
batchSession = _droppedSession;
}
Volatile.Write(ref _lastDropResyncTimestamp, System.Diagnostics.Stopwatch.GetTimestamp());
try
{