[WIP-001] Unbounded STA message-pump drain starves commands; sustained event load reported as StaHung #246

Closed
opened 2026-12-02 16:07:35 -05:00 by dohertj2 · 1 comment
Owner

WIP-001: Unbounded message-pump drain starves STA commands and the activity stamp

  • Severity: High
  • Category: Bug
  • Confidence: Plausible. The mechanism is confirmed in code; the trigger rate is estimated from perf.md.
  • Locations: Sta/StaMessagePump.cs:44-56, Sta/StaRuntime.cs:335-341, Sta/StaRuntime.cs:404-423, Ipc/WorkerPipeSession.cs:1445-1493

Description. StaMessagePump.PumpPendingMessages loops while (PeekMessage(PM_REMOVE)) with no message or time bound. The STA loop calls it and only afterwards calls MarkActivity() and returns to ProcessQueuedCommands(). Nothing on the event path (MxAccessBaseEventSink.EnqueueEvent) stamps activity. While inbound messages arrive at least as fast as they dispatch, the pump never returns, so:

  1. LastActivityUtc freezes even though the STA is doing useful work.
  2. Queued InvokeAsync work items do not run. commandWakeEvent is signalled but nothing looks at the queue. This includes the alarm poll and any UnAdvise/RemoveItem a client sends to shed load.

The watchdog (ReportWatchdogFaultIfNeededAsync) sees staleness above HeartbeatGrace (15 s) with no command in flight, sets _state = Faulted and writes WorkerFault{StaHung}. The gateway faults the session on any WorkerFault (Server/Workers/WorkerClient.cs:682-686).

Failure scenario. perf.md measures about 1.4 ms of STA time per OnDataChange on vd03 at HEAD (the record-info pin is not merged, see WIP-002). That caps dispatch near 700 events/s. A client that advises enough fast-changing tags to exceed that rate for more than 15 s gets its session killed with "STA activity is stale", a diagnosis that points at a hung COM call. The event queue never overflows because the drain loop keeps up, so the designed QueueOverflow fail-fast does not fire first. With a command in flight the same load is tolerated only until HeartbeatStuckCeiling (75 s), and that command does not start in the meantime.

Suggested fix. Bound each pump pass (for example 64 messages or about 10 ms), call MarkActivity() inside the pump loop, and interleave ProcessQueuedCommands() between slices. MWMO_INPUTAVAILABLE already makes the next wait return immediately when messages remain, so a bounded pass loses no wakeups.

Test idea. Drive StaRuntime with a fake IStaMessagePump whose PumpPendingMessages simulates a backlog that refills for N seconds. Assert that LastActivityUtc advances during the backlog and that an InvokeAsync queued mid-backlog completes before it ends.


Source: code-reviews/2026-12-02-perf-bugs/README.md and code-reviews/2026-12-02-perf-bugs/PLAN.md (commit f200bda6 on local branch docs/review-2026-12-02-perf-bugs, not yet pushed). Reviewed at 54367e74.

## WIP-001: Unbounded message-pump drain starves STA commands and the activity stamp - **Severity:** High - **Category:** Bug - **Confidence:** Plausible. The mechanism is confirmed in code; the trigger rate is estimated from `perf.md`. - **Locations:** `Sta/StaMessagePump.cs:44-56`, `Sta/StaRuntime.cs:335-341`, `Sta/StaRuntime.cs:404-423`, `Ipc/WorkerPipeSession.cs:1445-1493` **Description.** `StaMessagePump.PumpPendingMessages` loops `while (PeekMessage(PM_REMOVE))` with no message or time bound. The STA loop calls it and only afterwards calls `MarkActivity()` and returns to `ProcessQueuedCommands()`. Nothing on the event path (`MxAccessBaseEventSink.EnqueueEvent`) stamps activity. While inbound messages arrive at least as fast as they dispatch, the pump never returns, so: 1. `LastActivityUtc` freezes even though the STA is doing useful work. 2. Queued `InvokeAsync` work items do not run. `commandWakeEvent` is signalled but nothing looks at the queue. This includes the alarm poll and any `UnAdvise`/`RemoveItem` a client sends to shed load. The watchdog (`ReportWatchdogFaultIfNeededAsync`) sees staleness above `HeartbeatGrace` (15 s) with no command in flight, sets `_state = Faulted` and writes `WorkerFault{StaHung}`. The gateway faults the session on any `WorkerFault` (`Server/Workers/WorkerClient.cs:682-686`). **Failure scenario.** `perf.md` measures about 1.4 ms of STA time per `OnDataChange` on vd03 at HEAD (the record-info pin is not merged, see WIP-002). That caps dispatch near 700 events/s. A client that advises enough fast-changing tags to exceed that rate for more than 15 s gets its session killed with "STA activity is stale", a diagnosis that points at a hung COM call. The event queue never overflows because the drain loop keeps up, so the designed `QueueOverflow` fail-fast does not fire first. With a command in flight the same load is tolerated only until `HeartbeatStuckCeiling` (75 s), and that command does not start in the meantime. **Suggested fix.** Bound each pump pass (for example 64 messages or about 10 ms), call `MarkActivity()` inside the pump loop, and interleave `ProcessQueuedCommands()` between slices. `MWMO_INPUTAVAILABLE` already makes the next wait return immediately when messages remain, so a bounded pass loses no wakeups. **Test idea.** Drive `StaRuntime` with a fake `IStaMessagePump` whose `PumpPendingMessages` simulates a backlog that refills for N seconds. Assert that `LastActivityUtc` advances during the backlog and that an `InvokeAsync` queued mid-backlog completes before it ends. --- Source: `code-reviews/2026-12-02-perf-bugs/README.md` and `code-reviews/2026-12-02-perf-bugs/PLAN.md` (commit `f200bda6` on local branch `docs/review-2026-12-02-perf-bugs`, not yet pushed). Reviewed at `54367e74`.
dohertj2 added the area:workerpriority:p1type:bugtype:review-finding labels 2026-12-02 16:07:35 -05:00
Author
Owner

Merged to main at 68043411 (pushed 2026-12-03). Gateway suite 1610 passed / 0 failed (macOS); windev worker CI 836 passed / 0 failed on that SHA.

STA pump drains at most 64 messages per pass, then services commands and stamps activity. Flood test added.

Merged to `main` at `68043411` (pushed 2026-12-03). Gateway suite 1610 passed / 0 failed (macOS); windev worker CI 836 passed / 0 failed on that SHA. STA pump drains at most 64 messages per pass, then services commands and stamps activity. Flood test added.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: dohertj2/mxaccessgw#246