fix(workers): bound the heartbeat skip by heartbeat age on the monotonic clock; deadline covers the enqueue; a reply that wins the timeout race is returned (WRK-001, WRK-003, WRK-004, WRK-005, #253)

This commit is contained in:
Joseph Doherty
2026-12-03 09:52:19 -05:00
parent 159524edd9
commit 2c22ae6bd4
6 changed files with 345 additions and 21 deletions
+1 -1
View File
@@ -153,7 +153,7 @@ launch CWD (SEC-01, SEC-33).
| `MxGateway:Worker:HeartbeatCheckIntervalSeconds` | `5` | How often the gateway checks the age of the worker's last heartbeat against `HeartbeatGraceSeconds`. This is the gateway-side check cadence only; the worker's send interval is `WorkerHeartbeatSendIntervalSeconds`. Must be greater than zero. |
| `MxGateway:Worker:HeartbeatIntervalSeconds` | *(unset)* | **Deprecated** alias of `HeartbeatCheckIntervalSeconds` (mxaccessgw#192). The old name implied the worker's send interval, but the value only ever set the gateway's check cadence, which is what it still sets. Setting both keys fails validation. |
| `MxGateway:Worker:HeartbeatGraceSeconds` | `15` | Maximum age of the last worker heartbeat before the gateway faults the worker. This must be greater than or equal to `HeartbeatCheckIntervalSeconds`, and strictly greater than `WorkerHeartbeatSendIntervalSeconds` — a grace at or below the worker's send interval declares a healthy worker dead between two heartbeats (mxaccessgw#218). |
| `MxGateway:Worker:HeartbeatStuckCeilingSeconds` | `75` | Ceiling on the gateway's in-flight-command heartbeat skip. While a command is pending the gateway tolerates a stale heartbeat, because the worker STA is busy inside a COM call; once the oldest pending command is older than this, the watchdog faults the worker anyway so a truly stuck COM call cannot hide a dead worker. Must be greater than or equal to `HeartbeatGraceSeconds`. The worker's own watchdog has its separate ceiling, `WorkerHeartbeatStuckCeilingSeconds`. |
| `MxGateway:Worker:HeartbeatStuckCeilingSeconds` | `75` | Ceiling on the gateway's in-flight-command heartbeat skip. While a command is pending the gateway tolerates a stale heartbeat, because the worker STA is busy inside a COM call; once the oldest pending command is older than this, or the last heartbeat is, the watchdog faults the worker anyway so a truly stuck COM call cannot hide a dead worker. The heartbeat-age bound is what stops a client that keeps retrying timed-out commands from holding the skip open: a command that times out leaves the pending set, so with the default 30 s command timeout no pending command ever reaches 75 s. Heartbeat age is measured on the monotonic clock, so a wall-clock step does not fault workers. Must be greater than or equal to `HeartbeatGraceSeconds`. The worker's own watchdog has its separate ceiling, `WorkerHeartbeatStuckCeilingSeconds`. |
| `MxGateway:Worker:WorkerHeartbeatSendIntervalSeconds` | `5` | Interval at which the worker sends heartbeats to the gateway. Must be between `1` and `86400`, and below `HeartbeatGraceSeconds`. Conveyed to the worker via `MXGATEWAY_WORKER_HEARTBEAT_SEND_INTERVAL_MS`; a missing or invalid value leaves the worker on 5 s. |
| `MxGateway:Worker:WorkerHeartbeatGraceSeconds` | `15` | The worker's own STA-watchdog grace: how long STA activity may go stale before the worker faults `StaHung` (no command in flight). Must be between `1` and `86400` and below `WorkerHeartbeatStuckCeilingSeconds`; validation uses this configured value, not a copied constant (mxaccessgw#224). Conveyed via `MXGATEWAY_WORKER_HEARTBEAT_GRACE_MS`; a missing or invalid value, or a grace the ceiling does not exceed, leaves the worker on 15 s. |
| `MxGateway:Worker:WorkerHeartbeatStuckCeilingSeconds` | `75` | Ceiling on the worker's own STA watchdog: once STA activity has been stale this long, the worker faults `StaHung` even while a command is in flight. Must be greater than `WorkerHeartbeatGraceSeconds` and at most `86400`. Raise it together with `HeartbeatStuckCeilingSeconds` for deployments that run very long bulk operations. Conveyed via `MXGATEWAY_WORKER_HEARTBEAT_STUCK_CEILING_MS`; a missing or invalid value leaves the worker on 75 s. |
+15
View File
@@ -647,6 +647,21 @@ Pending command handling:
Timeouts should not assume the COM call stopped. A timed-out command may still
finish inside the worker.
The command timeout is one deadline for the whole call, the outbound enqueue
included. The outbound channel is bounded, and a write loop parked in a pipe
write the worker is not draining leaves it full; an enqueue bound only to the
caller's token then waited indefinitely for a caller with no deadline. The
common case (room in the channel) is a non-allocating `TryWrite`; only a full
channel waits, under the deadline, and the reply wait gets what is left of it.
An enqueue that runs out of time fails the command as `CommandTimeout` with no
`WorkerCancel`, because the worker never saw it.
When the deadline fires, the timeout arm owns the outcome only if it removes the
pending entry. If the read loop removed it first (the reply landed right at the
deadline), the caller gets that reply, which is what the metrics already counted,
instead of a `CommandTimeout` that would make it retry a write the worker
performed.
On timeout **and on caller cancellation** the client also forwards a
`WorkerCancel` carrying the abandoned correlation id, best-effort: the gateway
has stopped waiting, but the worker has not stopped working, and the worker owns