From dc45f79465121e379ac093248367dd44c3b4fd8b Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 27 Jul 2026 18:07:57 -0700 Subject: [PATCH] fix(runtime): drain the session-tabs coalescer when a listener subscribes (#11022) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit multi-client-navigation-isolation.integration.test.ts fails intermittently on clean main — measured at 4-8/24 with a flush-window sweep — and has been taxing unrelated PRs across the repo. Mechanism, confirmed by instrumented trace (schedule t=23412, listener REGISTER 23460/23461, coalescer fire 23462): a ~4ms race between the 50ms session-tabs notify coalescer and listener registration in onMobileSessionTabsChanged. When the pending timer fires after the listener registers but before session.tabs.activate is handled, it emits a stale {type:'updated', activeTabId:'host-tab'} that the test consumes. flushAll() at the top of onMobileSessionTabsChanged, before the listener joins the set — mirroring the flushAll() its own unsubscribe closure already performs. DRAINING rather than cancelling is load-bearing: cancel/dispose has no emit, so it would silence the stale frame but DELETE the pending update for subscribers already registered, leaving them stale until an unrelated next schedule(). That would trade a flaky test for a real lost-update bug. The newcomer cannot miss an update by having it drained — listMobileSessionTabs and the coalesced emit read the same mobileSessionTabsByWorktree map, and snapshot-read to listener-add is macrotask-atomic. Sweep: 4-5/24 before, 0/24 after. The regression test uses fake timers and is two-sided — it fails if flushAll is removed (stale frame delivered) and fails if flush is swapped for dispose (nothing drained), on different assertions, so it pins the specific choice rather than merely the presence of a change. Reviewed independently and returned clean with both mutations re-run by the reviewer. A latent flushAll re-entrancy double-emit exists in principle — fire() does not re-check pending.has the way flush() does — but is unreachable: both production callers' listeners only invoke the dispatcher reply, a shape that has shipped since #8141 on the symmetric unsubscribe flush. Not verified: live paired-device mobile/relay behaviour (no device available); the mobile conclusion rests on a code trace. --- .../graph-sync-mobile-snapshot-gating.test.ts | 22 +++++++++++++++++++ src/main/runtime/orca-runtime.ts | 5 +++++ 2 files changed, 27 insertions(+) diff --git a/src/main/runtime/graph-sync-mobile-snapshot-gating.test.ts b/src/main/runtime/graph-sync-mobile-snapshot-gating.test.ts index dcf89894f..338535562 100644 --- a/src/main/runtime/graph-sync-mobile-snapshot-gating.test.ts +++ b/src/main/runtime/graph-sync-mobile-snapshot-gating.test.ts @@ -325,6 +325,28 @@ describe('graph-sync mobile snapshot gating', () => { expect(events).toHaveLength(0) }) + it('drains a pending notify to existing subscribers instead of replaying it to a new one', () => { + const { runtime, events, sync } = createRuntime(makeSession()) + + sync([makeRendererSnapshot({ version: 1 })]) + // Mid-window: the notify is armed but has not fired. + vi.advanceTimersByTime(20) + expect(events).toHaveLength(0) + + const lateEvents: RuntimeMobileSessionTabsResult[] = [] + runtime.onMobileSessionTabsChanged((snapshot) => lateEvents.push(snapshot)) + const drainedOnSubscribe = events.length + + vi.advanceTimersByTime(500) + // The new subscriber's initial snapshot already folded that state in, so it + // must never see the armed notify — otherwise the timer lands as a stale + // `updated` frame carrying pre-subscribe state. + expect(lateEvents).toEqual([]) + // Drained on subscribe, not cancelled: no existing subscriber loses it. + expect(drainedOnSubscribe).toBe(1) + expect(events).toHaveLength(1) + }) + it('forces an emit by the starvation cap under sustained sync churn', () => { const { events, sync } = createRuntime(makeSession()) diff --git a/src/main/runtime/orca-runtime.ts b/src/main/runtime/orca-runtime.ts index 124ed6dc4..f06a01d20 100644 --- a/src/main/runtime/orca-runtime.ts +++ b/src/main/runtime/orca-runtime.ts @@ -7404,6 +7404,11 @@ export class OrcaRuntimeService { listener: (snapshot: RuntimeMobileSessionTabsResult) => void, clientNavigationId?: string ): () => void { + // Why: a notify coalesced before this subscriber existed is already folded + // into the initial snapshot it was just sent. Draining it here — before the + // listener joins — keeps that pending timer from landing as a redundant + // `updated` frame carrying pre-subscribe state. Mirrors the unsubscribe flush. + this.mobileSessionTabsNotifyCoalescer.flushAll() const subscription = { listener, clientNavigationId } this.mobileSessionTabListeners.add(subscription) return () => {