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
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:
@@ -255,11 +255,18 @@ while not shutdown:
|
||||
QS_ALLINPUT,
|
||||
MWMO_INPUTAVAILABLE)
|
||||
|
||||
while PeekMessage:
|
||||
repeat at most 64 times while PeekMessage:
|
||||
TranslateMessage
|
||||
DispatchMessage
|
||||
```
|
||||
|
||||
Each pump pass dispatches at most `StaMessagePump.MaxMessagesPerPass` (64)
|
||||
messages, then the loop stamps activity and returns to the command queue. An
|
||||
unbounded drain never returns while COM callbacks arrive at least as fast as
|
||||
they dispatch, so queued commands would not run and the activity stamp would
|
||||
freeze until the watchdog reported `StaHung`. `MWMO_INPUTAVAILABLE` makes the
|
||||
next wait return at once while input remains, so the cap loses no wakeups.
|
||||
|
||||
The command queue should signal a Win32 event or equivalent wait handle so the
|
||||
STA can wake without busy-waiting.
|
||||
|
||||
@@ -338,11 +345,14 @@ message wake is what carries it.
|
||||
|
||||
`MWMO_INPUTAVAILABLE` makes the drain contract load-bearing: the wait wakes on
|
||||
input that is merely *present*, including input an earlier `PeekMessage` saw but
|
||||
did not remove. A `pumpStep` that drains only part of the queue — or a no-op one
|
||||
— therefore leaves a message that satisfies the wake condition forever, and the
|
||||
did not remove. A `pumpStep` that removes nothing — a no-op, or a peek without
|
||||
`PM_REMOVE` — therefore leaves a message that satisfies the wake condition forever, and the
|
||||
loop spins at 100% CPU until its deadline (deadline and reply shape still hold;
|
||||
it is a CPU fault, not a correctness one). Every `pumpStep` must drain to empty,
|
||||
as `StaRuntime.PumpPendingMessages` does.
|
||||
it is a CPU fault, not a correctness one). Every `pumpStep` must remove messages
|
||||
on each call. `StaRuntime.PumpPendingMessages` removes up to 64 per call: a
|
||||
backlog larger than that makes the next wait return at once, and the next step
|
||||
dispatches the next slice, so the loop only stays busy while there is real
|
||||
dispatch work and re-checks its deadline between slices.
|
||||
|
||||
The wait slice is capped at 50 ms so `pumpStep` runs periodically even when
|
||||
nothing wakes the wait — a process with no STA message queue (unit tests drive
|
||||
|
||||
+7
-4
@@ -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`:
|
||||
|
||||
Reference in New Issue
Block a user