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
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user