fix(dashboard): apply reconnect delay when the snapshot watch completes cleanly (mxaccessgw#222)

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 13:12:40 -05:00
parent b47a168a2c
commit bc28e1436b
2 changed files with 25 additions and 7 deletions
@@ -156,6 +156,15 @@ public sealed class DashboardSnapshotPublisher : BackgroundService
_logger.LogWarning(ex, "Snapshot broadcast failed; will retry on the next snapshot tick."); _logger.LogWarning(ex, "Snapshot broadcast failed; will retry on the next snapshot tick.");
} }
} }
// The watch completed cleanly (or the host is stopping). Apply the same
// reconnect delay as the fault path; without it a source that completes
// immediately turns this loop into a hot spin.
if (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation("Snapshot subscription completed; reconnecting in {DelaySeconds:F1}s.", _reconnectDelay.TotalSeconds);
await Task.Delay(_reconnectDelay, _timeProvider, stoppingToken).ConfigureAwait(false);
}
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
@@ -68,13 +68,16 @@ public sealed class DashboardSnapshotPublisherTests
} }
/// <summary> /// <summary>
/// Sanity: a normal completion of WatchSnapshotsAsync (no exception) /// A clean completion of WatchSnapshotsAsync (no exception) is re-subscribed only after
/// is re-subscribed; the publisher exits only on host shutdown. /// the reconnect delay; without it the publisher hot-spins re-opening a source that
/// completes immediately. Driven by a fake clock, so nothing sleeps.
/// </summary> /// </summary>
/// <returns>A task that represents the asynchronous operation.</returns> /// <returns>A task that represents the asynchronous operation.</returns>
[Fact] [Fact]
public async Task ExecuteAsync_WhenSnapshotServiceCompletes_Resubscribes() public async Task ExecuteAsync_WhenSnapshotServiceCompletes_ResubscribesOnlyAfterReconnectDelay()
{ {
TimeSpan reconnectDelay = TimeSpan.FromMilliseconds(50);
TickObservingTimeProvider clock = new() { WatchedDueTime = reconnectDelay };
CompleteImmediatelySnapshotService snapshotService = new(); CompleteImmediatelySnapshotService snapshotService = new();
RecordingHubContext hubContext = new(); RecordingHubContext hubContext = new();
DashboardSnapshotPublisher publisher = new( DashboardSnapshotPublisher publisher = new(
@@ -82,19 +85,25 @@ public sealed class DashboardSnapshotPublisherTests
hubContext, hubContext,
ConnectedCounter(), ConnectedCounter(),
NullLogger<DashboardSnapshotPublisher>.Instance, NullLogger<DashboardSnapshotPublisher>.Instance,
TimeSpan.FromMilliseconds(50), reconnectDelay,
IdlePollInterval, IdlePollInterval,
timeProvider: new FakeTimeProvider()); timeProvider: clock);
using CancellationTokenSource cts = new(); using CancellationTokenSource cts = new();
await publisher.StartAsync(cts.Token).WaitAsync(TestTimeout); await publisher.StartAsync(cts.Token).WaitAsync(TestTimeout);
await snapshotService.Subscribes.WaitForAsync(1);
await clock.WaitForWatchedTimersAsync(1, TestTimeout);
// Just short of the delay no re-subscribe may happen (a hot spin would have made many).
clock.Advance(reconnectDelay - TimeSpan.FromMilliseconds(1));
Assert.Equal(1, snapshotService.Subscribes.Count);
clock.Advance(TimeSpan.FromMilliseconds(1));
await snapshotService.Subscribes.WaitForAsync(2); await snapshotService.Subscribes.WaitForAsync(2);
await cts.CancelAsync(); await cts.CancelAsync();
await publisher.StopAsync(CancellationToken.None); await publisher.StopAsync(CancellationToken.None);
Assert.True(snapshotService.Subscribes.Count >= 2);
} }
/// <summary> /// <summary>