core: a cancel during a queued dispatch's notification stalls the message queue
With the message queue on, a queue subscriber that cancels while the queue is dispatching its next message leaves the queue stuck: that message runs, but nothing queued afterwards is ever sent.
it("keeps draining after a subscriber cancels while the queue dispatches", () => {
const run = vi.fn(() => queue.notifyBusy());
const queue = createMessageQueue({ run });
queue.adapter.enqueue(msg("first"));
queue.adapter.enqueue(msg("second"));
let cancelled = false;
queue.subscribe(() => {
if (cancelled || queue.adapter.items.length !== 0) return;
cancelled = true;
queue.notifyCancelled();
});
queue.notifyIdle(); // "first" settles, "second" dispatches
queue.notifyIdle(); // "second" settles
queue.adapter.enqueue(msg("third"));
expect(run).toHaveBeenCalledTimes(3); // main: 2
});Through the local runtime this is a thread.subscribe listener calling thread.cancelRun() when the queue empties, with unstable_queueClearOnCancel: false; the external-store runtime and useAgUiRuntime reach the same notifyCancelled.
Mechanism. advance() now sets running = true before the setLanes notification for the popped message. A notifyCancelled from a subscriber in that notification sees a running queue, so it sets paused and cancelSettles = 1 for a run the driver has not started. The dispatched run's notifyBusy turns that into suppressIdle, so its idle is swallowed, running stays true, and every later send buffers forever. Before, running was still false during the notification and the cancel was a no-op.
Regressed in #8452, which fixed the reentrant-enqueue ordering bug by reserving the run before the notification. advance() already tracks this window as dispatchPending, and #8452 skips steering and moves during it; notifyCancelled needs the same guard.
Source: assistant-ui/assistant-ui