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
+2 -1
View File
@@ -210,7 +210,8 @@ Consequences, and how this sits with the existing failover/reconcile design:
per minute) `AlarmSnapshotTruncated` warning carrying the cap, the record
counts, and the running truncated-fetch total — identifiers and counts only,
never tag names, values, limits, or comments. Be honest about its reach: it
goes to the worker's console/stderr, which is captured on dev hosts but is
goes to the worker's stderr, which the gateway forwards to its own log
(since mxaccessgw#259; before that the line was lost), but it is
not a metric, not a dashboard tile, and not part of any session-status or
alarm-feed payload, so a production deployment can truncate indefinitely
without anyone noticing. The structural signal that fixes this landed
+4 -2
View File
@@ -508,8 +508,10 @@ read loop starts after readiness so the handshake has a single owner for its
ordered frames.
`WorkerReady.record_info_pin_status` carries the worker's `MXSTATUS_PROXY`
record-info pin outcome (see `MxAccessWorkerInstanceDesign.md`). The gateway does
not capture worker stderr, so `WorkerClient` is where operators see it: a status
record-info pin outcome (see `MxAccessWorkerInstanceDesign.md`). The worker's own
`WorkerRecordInfoPin` stderr line is forwarded to the gateway log at Information
(see `WorkerProcessLauncher.md`), so `WorkerClient` is where operators see the
outcome graded: a status
starting `pinned ` is logged at Information, anything else at Warning, because an
unpinned worker pays a type-library reload on every data change. It is kept as
`IWorkerClient.RecordInfoPinStatus` and shown on the dashboard's session details
+5 -4
View File
@@ -537,10 +537,11 @@ with the pin alone.
The pin only warms a cache. It changes no MXAccess behaviour, and a failure is never fatal. The
outcome is in `MxStatusProxyRecordInfoPin.Status`. During the handshake `WorkerPipeSession` logs it
as `WorkerRecordInfoPin` and copies it into `WorkerReady.record_info_pin_status`, unless the runtime
already filled that field. The copy is the one operators see: the worker's own log goes to stderr,
which the gateway's launcher does not redirect. The gateway logs the reported status (Warning when
it is not a successful pin) and shows it on the dashboard's session details page; see
`GatewayProcessDesign.md`.
already filled that field. The worker's own log goes to stderr, which the gateway forwards to its
log at the line's level (see `WorkerProcessLauncher.md`), so the `WorkerRecordInfoPin` line itself
is always Information. The copy in `WorkerReady` is the graded one: the gateway logs the reported
status (Warning when it is not a successful pin) and shows it on the dashboard's session details
page; see `GatewayProcessDesign.md`.
If COM creation fails, the worker should send a structured fault with:
+1 -1
View File
@@ -149,7 +149,7 @@ The production binding in `WorkerApplication.Run(string[])` is `EnvironmentVaria
## Logging
The worker writes structured key/value lines to standard error. Standard error is used rather than standard output because the gateway side reads worker stdout for diagnostic capture only, while stderr is reserved for log output that does not interfere with any future stdout-based channel.
The worker writes structured key/value lines to standard error, and nothing to standard output, so stdout stays free for any future stdout-based channel. The gateway's launcher redirects both streams and forwards every line to the gateway log under the `ZB.MOM.WW.MxGateway.Worker` category, at the level in the line's `level=` prefix. It also keeps the last stderr lines for the `StartupFailed` message of a worker that exits before connecting; see `WorkerProcessLauncher.md` "Worker Output Capture". Before mxaccessgw#259 the streams were not redirected and these lines were lost.
### The logger contract
+62 -3
View File
@@ -61,8 +61,63 @@ after that and before it connects is caught by the session factory instead:
so an early exit fails the open at once with `StartupFailed` and the worker's
exit code rather than holding the open (and its session slot) for the whole
startup timeout and reporting a timeout. Once the pipe is connected, a worker
exit surfaces as pipe end-of-stream in the handshake reads. The worker's stderr
is not redirected, so the failure carries the exit code only.
exit surfaces as pipe end-of-stream in the handshake reads. Both early-exit
failures (the probe's and the session factory's) end with the tail of the
worker's stderr, `Last worker stderr output:` followed by up to the last 50
lines / 4 KB. The factory first waits up to one second for stderr to reach
end-of-stream so the lines the worker wrote on its way out are included. The
`StartupFailed` message reaches the gateway log (`Failed to open gateway
session`) and the session's fault reason; a gRPC client still sees only
`Failed to open session <id>.`
## Worker Output Capture
The launcher starts the worker with `RedirectStandardError` and
`RedirectStandardOutput` set and hands both streams to `WorkerOutputCapture`
before anything else runs (mxaccessgw#259).
Before this change the worker's output was lost. It was started with
`CreateNoWindow` and no redirection, which gives the child a hidden console of
its own, so its `level=... event=...` log lines reached neither the gateway's
console (console run) nor the NSSM `AppStdout`/`AppStderr` files (service run).
This was checked on windev: a probe child's stderr did not appear in the
parent's redirected files, and the windev service's `stderr.log` and
`stdout.log` held no worker lines. Capturing the output therefore adds log lines
and loses none. Stdout is redirected as well because, once the child's standard
handles are set explicitly, an unredirected stdout would be the gateway's own
(the NSSM stdout log). The worker writes nothing to stdout itself.
Each line is forwarded to the gateway log under the category
`ZB.MOM.WW.MxGateway.Worker`, with the session id, worker process id and stream
as properties. It uses one category rather than one per session because the
logger factory caches categories for the life of the process. The level follows
the worker's own `level=` prefix (`Information`, `Warning`, `Error`). Any other
stderr text, such as CLR or native crash output, is logged as `Warning`, and
other stdout text as `Information`. Lines longer than 2048 characters are cut
and marked `...[truncated]`. The capture also keeps the most recent stderr lines
in a bounded tail for the startup-failure messages above.
The capture cannot block the worker or leak:
- The pipes are always drained. A reader only appends the line to the tail and
offers it to a bounded drop-oldest queue (1024 lines), which never waits. A
separate task does the logging. When the log sink falls behind, the oldest
queued lines are dropped and a `Dropped N worker output lines` warning is
logged. Backpressure never reaches the worker's `Console.Error` writes, some
of which run on its STA thread. A throwing log sink is ignored.
- The readers run until end-of-stream and then dispose their own reader.
End-of-stream arrives when the worker exits, and the gateway always kills the
worker before disposing its process handle, so the capture needs no
cancellation and does not cut off the worker's last lines.
The worker redacts its own lines (`WorkerLogRedactor`: nonce, secret, password,
token, credential and API-key fields and assignments; the unhandled-exception
hook logs the exception type only). As defense in depth, the capture also
replaces any occurrence of the session's bootstrap nonce, the one secret the
gateway gives the worker, with `[redacted]` before a line is logged or kept.
There is no option to turn the capture off. Worker log volume is low unless
`MXGATEWAY_WORKER_LOG_COMMANDS` is set, and that variable is what turns on the
noisy per-command lines.
The startup probe runs once, bounded by `WorkerOptions.StartupTimeoutSeconds`;
it is not retried. The default probe only checks that the process is still
@@ -88,9 +143,13 @@ the same rule.
Run the focused launcher tests after changing process launch behavior:
```bash
dotnet test src/ZB.MOM.WW.MxGateway.Tests/ZB.MOM.WW.MxGateway.Tests.csproj --filter WorkerProcessLauncherTests
dotnet test src/ZB.MOM.WW.MxGateway.Tests/ZB.MOM.WW.MxGateway.Tests.csproj --filter "FullyQualifiedName~WorkerProcessLauncherTests|FullyQualifiedName~WorkerOutputCaptureTests"
```
`WorkerOutputCaptureTests` includes a real child process (`/bin/sh`, or `cmd.exe`
on Windows) that writes well past a pipe buffer to stderr, to show the drain
cannot block the child.
Run the gateway build because the launcher is part of `ZB.MOM.WW.MxGateway.Server`:
```bash