fix(galaxy): probe advises start the event stream first so initial ScanState arrives (#865)

The gateway's StreamEvents with after_worker_sequence = 0 is a fresh stream
and never replays, and the event pump started only on the first
Read/Subscribe, so platform ScanState probes advised before then lost their
initial value (and a driver with no authored tags never got any host status).

PerPlatformProbeWatcher now awaits an ensure-event-stream hook before every
probe SubscribeBulk (seed, discover sync, reconnect ResubscribeStaleAsync).
The driver's hook starts the pump (refused while a teardown is in progress)
and waits, bounded, on the new EventPump.StreamRequested signal - completed
once the stream loop's first MoveNext has issued the StreamEvents RPC, or
when the loop ends - so the advise never precedes the stream request.

Claude-Session: https://claude.ai/code/session_01GT67AD99zQQqpLCVm3p7xk
This commit is contained in:
Joseph Doherty
2026-11-11 12:24:08 -05:00
parent c4bc1ef309
commit e35f2a4e93
5 changed files with 320 additions and 4 deletions
@@ -148,6 +148,17 @@ internal sealed class EventPump : IAsyncDisposable
private readonly int _session;
private readonly TaskCompletionSource _streamRequested = new(TaskCreationOptions.RunContinuationsAsynchronously);
/// <summary>
/// Completes once the stream loop has issued its <c>StreamEvents</c> call (the first
/// <c>MoveNextAsync</c> runs the subscriber's iterator synchronously up to the RPC) — or once the
/// loop has ended without getting that far, so a waiter never hangs on a dead pump. #865: the
/// gateway does not replay a fresh stream, so a caller whose only expected event is the initial
/// value of an advise it is about to make waits on this first.
/// </summary>
public Task StreamRequested => _streamRequested.Task;
/// <summary>
/// Marker written into the channel to wake the dispatch loop after a registration promoted
/// parked early events (#859). Never dispatched itself; compared by reference.
@@ -179,10 +190,17 @@ internal sealed class EventPump : IAsyncDisposable
private async Task RunAsync(CancellationToken ct)
{
IAsyncEnumerator<MxEvent>? enumerator = null;
try
{
await foreach (var ev in _subscriber.StreamEventsAsync(ct).WithCancellation(ct).ConfigureAwait(false))
enumerator = _subscriber.StreamEventsAsync(ct).GetAsyncEnumerator(ct);
// Start the first MoveNext BEFORE awaiting it: that synchronous step issues the RPC, which is
// the moment StreamRequested reports (#865).
var next = enumerator.MoveNextAsync();
_streamRequested.TrySetResult();
for (; await next.ConfigureAwait(false); next = enumerator.MoveNextAsync())
{
var ev = enumerator.Current;
if (ct.IsCancellationRequested) break;
EventsReceived.Add(1, _clientTag);
@@ -241,6 +259,20 @@ internal sealed class EventPump : IAsyncDisposable
}
finally
{
// A loop that died before issuing the RPC must not strand a StreamRequested waiter.
_streamRequested.TrySetResult();
if (enumerator is not null)
{
try { await enumerator.DisposeAsync().ConfigureAwait(false); }
catch (Exception ex) when (ct.IsCancellationRequested || ex is OperationCanceledException)
{
// Disposing a cancelled stream may surface its cancellation — shutdown noise.
}
catch (Exception ex)
{
_logger.LogDebug(ex, "Galaxy EventPump stream enumerator dispose failed.");
}
}
// Tell the dispatch loop the producer is done so it drains and exits.
_channel.Writer.TryComplete();
}