fix(gateway): wake the worker-ready wait on a state-change signal instead of a 25 ms poll (mxaccessgw#189)

GetReadyWorkerClientAsync re-read the session and worker states every 25 ms
until the WorkerReadyWaitTimeoutMs deadline. It now waits on two
TaskCompletionSource signals - one completed by every GatewaySession _state /
_workerClient write, one by every WorkerClient _state write - and re-evaluates
only when one fires or the remaining budget elapses. Both signals are captured
under the lock before the state is evaluated, so a transition between the
evaluation and the await is not lost.

IWorkerClient gains GetStateChangedSignal(), a default interface member that
returns null; a client without a signal (the test fakes) is re-read on the old
25 ms interval, so the fakes need no change. The production WorkerClient always
publishes one. Default behaviour (WorkerReadyWaitTimeoutMs = 0, fail fast) is
unchanged.

Regression tests (SessionManagerTests): ...WakesWithoutPollTimer and
...FailsFastWithoutPollTimer never advance the FakeTimeProvider, so they hang
and fail against the poll loop; WorkerClientTests.GetStateChangedSignal_
CompletesOnEachTransition pins the worker-side signal.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 12:32:43 -05:00
parent 11d8f76c5f
commit 101bda7073
5 changed files with 267 additions and 29 deletions
@@ -34,6 +34,34 @@ public sealed class WorkerClientTests
Assert.Equal(WorkerProcessId, client.ProcessId);
}
/// <summary>
/// mxaccessgw#189: every state transition completes the signal captured before it, and a
/// signal captured after the transition is a fresh, pending one. The session's
/// worker-ready wait relies on this instead of polling <see cref="WorkerClient.State"/>.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task GetStateChangedSignal_CompletesOnEachTransition()
{
await using PipePair pipePair = await PipePair.CreateAsync();
await using WorkerClient client = CreateClient(pipePair);
Task createdSignal = client.GetStateChangedSignal();
Assert.False(createdSignal.IsCompleted);
await CompleteHandshakeAsync(client, pipePair);
Assert.Equal(WorkerClientState.Ready, client.State);
await createdSignal.WaitAsync(TestTimeout);
Task readySignal = client.GetStateChangedSignal();
Assert.NotSame(createdSignal, readySignal);
Assert.False(readySignal.IsCompleted);
client.Kill("test");
await readySignal.WaitAsync(TestTimeout);
Assert.NotEqual(WorkerClientState.Ready, client.State);
}
/// <summary>
/// The <c>GatewayHello</c> carries the negotiated worker-frame maximum so the worker adopts the
/// configured limit instead of its own default; a regression to sending <c>0</c> means