fix(worker): bound shutdown alarm steps by the grace; watchdog stands down while shutting down; startup handshake deadline (MXA-003, WIP-007, WIP-009, #253)
ci / windows-x86 (push) Failing after 11m18s
ci / java (push) Failing after 11m43s
ci / portable (push) Failing after 12m6s

This commit is contained in:
Joseph Doherty
2026-12-03 10:07:41 -05:00
parent a474d3219b
commit 8e720bc727
11 changed files with 430 additions and 24 deletions
+1 -1
View File
@@ -147,7 +147,7 @@ launch CWD (SEC-01, SEC-33).
| `MxGateway:Worker:ExecutablePath` | `src\ZB.MOM.WW.MxGateway.Worker\bin\x86\Release\ZB.MOM.WW.MxGateway.Worker.exe` | Path to the x86 worker executable launched for each gateway session. The path must be valid and point to a `.exe` file. |
| `MxGateway:Worker:WorkingDirectory` | `null` | Optional working directory for the worker process. When set, it must be a valid filesystem path. |
| `MxGateway:Worker:RequiredArchitecture` | `X86` | Required Portable Executable architecture for the worker. Supported values are `X86` and `X64`; MXAccess parity uses `X86`. |
| `MxGateway:Worker:StartupTimeoutSeconds` | `30` | Total startup budget for process launch, startup probe, pipe connect, handshake, and worker readiness. |
| `MxGateway:Worker:StartupTimeoutSeconds` | `30` | Total startup budget for process launch, startup probe, pipe connect, handshake, and worker readiness. Also conveyed to the worker (`MXGATEWAY_WORKER_STARTUP_TIMEOUT_MS`) as the deadline for its own startup handshake, so a worker whose gateway died mid-launch exits instead of hanging in MXAccess initialization. |
| `MxGateway:Worker:PipeConnectAttemptTimeoutMilliseconds` | `2000` | Per-attempt timeout used by the worker named-pipe connect retry path. The overall pipe connection still stays under the startup budget. |
| `MxGateway:Worker:WriteCompletionWaitMilliseconds` | `1500` | Bounded wait the worker holds a unary write reply (`Write`/`Write2`/`WriteSecured`/`WriteSecured2`; bulk writes excluded) for the matching MXAccess `OnWriteComplete` callback, so the reply's `statuses` carry the real commit outcome. `0` disables the wait (pure fire-and-forget replies). Must be `>= 0`. The gateway conveys the value to the worker via the `MXGATEWAY_WORKER_WRITE_COMPLETION_WAIT_MS` environment variable. Consumers that time their own writes must budget above this wait: OtOpcUa's GalaxyDriver wraps gateway writes in a 2 s Tier A resilience timeout, so a deployment raising this option past ~2000 must raise that driver `ResilienceConfig` write timeout in step or slow-but-successful commits surface as consumer-side failures. |
| `MxGateway:Worker:EventQueueCapacity` | `10000` | Capacity, in events, of the worker's outbound MXAccess event queue. Must be between `1000` and `1000000`. This is burst headroom, not a throttle: the queue has no drop policy, so filling it records a `QueueOverflow` worker fault and faults the session. Raise it for sessions whose subscription set can outrun the drain loop (large advise sets, slow event consumers); the backing queue pre-allocates its slots, so the ceiling keeps a mistyped value from committing the 32-bit worker to an outsized allocation. The gateway conveys the value to the worker via the `MXGATEWAY_EVENT_QUEUE_CAPACITY` environment variable; a missing or unusable value leaves the worker on the 10000 default rather than failing the session. |
+30
View File
@@ -127,6 +127,20 @@ Startup sequence:
If validation fails before MXAccess creation, exit quickly with a non-zero exit
code. If MXAccess creation fails, send `WorkerFault` when possible and exit.
Steps 5–10 run under one deadline, `WorkerPipeSessionOptions.StartupHandshakeTimeout`,
which the launcher conveys as `MXGATEWAY_WORKER_STARTUP_TIMEOUT_MS` from
`MxGateway:Worker:StartupTimeoutSeconds` (default 120 s when absent). The worker
notices gateway death only through a pipe read returning end-of-stream or a
failed write, and the handshake has two windows with neither: a peer that
connects and never sends `GatewayHello` (net48 pipe reads ignore the
cancellation token), and MXAccess initialization, during which no read is
outstanding. A COM activation that hangs after the gateway died would otherwise
keep the x86 worker alive indefinitely. At the deadline the handshake throws
`TimeoutException` and the worker exits with `PipeConnectionFailed`; the stuck
step is abandoned, which is safe because the STA is a background thread. The
gateway applies the same budget to the whole launch and starts its clock first,
so a live gateway always gives up and kills the worker before this deadline.
The bootstrap layer returns structured exit codes before it creates pipes,
starts the STA, or touches MXAccess:
@@ -1155,6 +1169,22 @@ are rejected and queued commands that have not started receive
`ProtocolStatusCode.WorkerUnavailable`. The command already executing on the
STA is allowed to finish until the shutdown grace period expires.
The grace period covers the whole method, including the two alarm-side steps
that run before the data cleanup: joining the alarm poll loop and disposing the
alarm handler on the STA. Both wait on the STA, and neither can be cancelled
once its work item has started, so with the STA stuck in a COM call they used
to wait forever, the timeout below was never reached, and no shutdown-timeout
fault or `WorkerShutdownAck` was sent. Each step now gets what is left of the
grace, and running out raises the same `TimeoutException` as a slow data
cleanup.
The STA watchdog stands down once the session is `ShuttingDown` or `Stopped`.
The data cleanup runs as one STA work item, which leaves `LastStaActivityUtc`
frozen with no command correlation id on the heartbeat. With a grace longer than
`HeartbeatGrace`, the watchdog would otherwise overwrite `ShuttingDown` with
`Faulted` and report `StaHung` for a session that is closing cleanly. The
shutdown's own timeout bounds it instead.
After command dispatch is closed, cleanup runs on the STA in MXAccess handle
order: