fix(worker): bound each STA message-pump pass to 64 dispatches (WIP-001, #246)
ci / portable (push) Failing after 13m57s
ci / windows-x86 (push) Successful in 12m20s
ci / java (push) Failing after 40m28s

An unbounded PeekMessage drain never returned while inbound COM callbacks
arrived at least as fast as they dispatched, so the STA loop stopped running
queued commands and stopped stamping LastActivityUtc; the heartbeat watchdog
then faulted a busy apartment as StaHung. The record-info pin raises the
dispatch ceiling a lot but is best-effort, and the drain was still unbounded.

Each pump pass now dispatches at most StaMessagePump.MaxMessagesPerPass (64),
then the loop stamps activity and services the command queue. MWMO_INPUTAVAILABLE
makes the next wait return at once while input remains, so no wakeup is lost.
StaRuntime.PumpPendingMessages (the pump step of ReadBulk/write waits) uses the
same cap so those loops re-check their deadline between slices.

Tests: a fake message flood with a queued command (command runs, activity stays
fresh, no pass exceeds the cap); a real-queue test that a pass stops at the cap.
This commit is contained in:
Joseph Doherty
2026-12-03 09:40:52 -05:00
parent c065b3678a
commit 078b9c9840
10 changed files with 272 additions and 24 deletions
+7 -4
View File
@@ -48,7 +48,7 @@ while (!IsShutdownRequested())
{
ProcessQueuedCommands();
messagePump.WaitForWorkOrMessages(commandWakeEvent, idlePumpInterval);
messagePump.PumpPendingMessages();
messagePump.PumpPendingMessages(StaMessagePump.MaxMessagesPerPass);
MarkActivity();
}
```
@@ -57,14 +57,15 @@ while (!IsShutdownRequested())
### The message pump
`StaMessagePump.WaitForWorkOrMessages` calls `MsgWaitForMultipleObjectsEx` with `QS_ALLINPUT` and `MWMO_INPUTAVAILABLE`, so the wait wakes for either a signal on `commandWakeEvent` or any new message in the thread's Windows queue. `PumpPendingMessages` then drains the queue with `PM_REMOVE`:
`StaMessagePump.WaitForWorkOrMessages` calls `MsgWaitForMultipleObjectsEx` with `QS_ALLINPUT` and `MWMO_INPUTAVAILABLE`, so the wait wakes for either a signal on `commandWakeEvent` or any new message in the thread's Windows queue. `PumpPendingMessages` then dispatches pending messages with `PM_REMOVE`, at most `MaxMessagesPerPass` (64) per pass:
```csharp
public int PumpPendingMessages()
public int PumpPendingMessages(int maxMessages)
{
int pumpedMessages = 0;
while (PeekMessage(out NativeMessage message, IntPtr.Zero, 0, 0, PmRemove))
while (pumpedMessages < maxMessages
&& PeekMessage(out NativeMessage message, IntPtr.Zero, 0, 0, PmRemove))
{
TranslateMessage(ref message);
DispatchMessage(ref message);
@@ -77,6 +78,8 @@ public int PumpPendingMessages()
`DispatchMessage` is what causes the COM marshaler to deliver event sink calls onto the STA thread; without this loop, MXAccess events never surface to managed code.
The pass is bounded because an unbounded drain never returns while inbound COM callbacks arrive at least as fast as they dispatch. The loop then stops running queued commands (including an `UnAdvise` a client sends to shed load) and stops stamping `LastActivityUtc`, and the heartbeat watchdog reports the busy apartment as `StaHung` after `HeartbeatGrace`. Bounding the pass loses no wakeups: `MWMO_INPUTAVAILABLE` makes the next wait return at once while input remains, so under a flood the loop alternates one command-queue pass and one 64-message slice, stamping activity after each slice. `StaRuntime.PumpPendingMessages()`, which commands that hold the STA call from their wait loops, uses the same cap so those loops re-check their deadline between slices.
## Work Items and the Command Queue
`StaRuntime` exposes two `InvokeAsync` overloads. Both wrap the delegate in an internal `StaWorkItem<T>`, enqueue it on a `ConcurrentQueue<IStaWorkItem>`, and signal `commandWakeEvent`: