fix(workers): capture worker stderr/stdout, forward it to the gateway log, and attach the stderr tail to early-exit StartupFailed (WRK-002, #259)
ci / windows-x86 (push) Successful in 3m50s
ci / java (push) Failing after 10m14s
ci / portable (push) Failing after 19m55s

The worker was started with CreateNoWindow and no redirection, so its log lines
went to a hidden console of its own and were lost under both a console run and
the NSSM service (verified on windev). The launcher now redirects both streams
and WorkerOutputCapture drains them: each line is forwarded under the
ZB.MOM.WW.MxGateway.Worker category at the worker's own level, the session
nonce is scrubbed, and the last 50 lines / 4 KB of stderr are kept. The drain
never waits on the logger (bounded drop-oldest queue) and ends at EOF when the
worker exits. A worker that exits before connecting (session factory race) or
that the probe finds already exited now fails StartupFailed with the stderr tail.
This commit is contained in:
Joseph Doherty
2026-12-03 13:10:12 -05:00
parent 6804341152
commit ee0f836886
18 changed files with 995 additions and 26 deletions
@@ -79,7 +79,7 @@ public sealed class SessionWorkerClientFactory : ISessionWorkerClientFactory
.ConfigureAwait(false);
session.TransitionTo(SessionState.WaitingForPipe);
await WaitForPipeConnectionAsync(pipe, processHandle.Process, session.SessionId, startupCancellation.Token)
await WaitForPipeConnectionAsync(pipe, processHandle, session.SessionId, startupCancellation.Token)
.ConfigureAwait(false);
session.TransitionTo(SessionState.Handshaking);
@@ -213,18 +213,21 @@ public sealed class SessionWorkerClientFactory : ISessionWorkerClientFactory
/// timeout with no exit code. Racing the connect against the process exit reports the crash
/// as <see cref="WorkerProcessLaunchErrorCode.StartupFailed"/> with the exit code at once.
/// Once connected, a worker exit shows up as pipe EOF in the handshake reads.
/// The message ends with the tail of the worker's stderr when the launcher captured it
/// (mxaccessgw#259): a worker that dies this early usually said why on its way out.
/// </remarks>
/// <param name="pipe">The named pipe.</param>
/// <param name="process">The launched worker process.</param>
/// <param name="processHandle">The launched worker process.</param>
/// <param name="sessionId">Session id, for the failure message.</param>
/// <param name="cancellationToken">Cancellation token (the startup deadline and the caller's).</param>
/// <returns>A task that represents the asynchronous operation.</returns>
internal static async Task WaitForPipeConnectionAsync(
NamedPipeServerStream pipe,
IWorkerProcess process,
WorkerProcessHandle processHandle,
string sessionId,
CancellationToken cancellationToken)
{
IWorkerProcess process = processHandle.Process;
using CancellationTokenSource raceCancellation = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
Task connect = pipe.WaitForConnectionAsync(raceCancellation.Token);
Task exit = process.WaitForExitAsync(raceCancellation.Token).AsTask();
@@ -235,11 +238,21 @@ public sealed class SessionWorkerClientFactory : ISessionWorkerClientFactory
await raceCancellation.CancelAsync().ConfigureAwait(false);
await ObserveQuietlyAsync(connect).ConfigureAwait(false);
int? exitCode = process.ExitCode;
WorkerOutputCapture? outputCapture = processHandle.OutputCapture;
if (outputCapture is not null)
{
await outputCapture
.WaitForStandardErrorDrainedAsync(WorkerProcessLauncher.StandardErrorDrainWait)
.ConfigureAwait(false);
}
throw new WorkerProcessLaunchException(
WorkerProcessLaunchErrorCode.StartupFailed,
$"Worker process for session {sessionId} exited with code "
+ $"{(exitCode?.ToString(System.Globalization.CultureInfo.InvariantCulture) ?? "unknown")} "
+ "before it connected to its pipe.");
WorkerOutputCapture.AppendStandardErrorTail(
outputCapture,
$"Worker process for session {sessionId} exited with code "
+ $"{(exitCode?.ToString(System.Globalization.CultureInfo.InvariantCulture) ?? "unknown")} "
+ "before it connected to its pipe."));
}
// The connect won (or the exit wait itself failed, which says nothing about the worker):