From 75ae6d9f6ac41d09697287d4d965c55019c7f1cf Mon Sep 17 00:00:00 2001 From: "buf0-bot[bot]" <252831055+buf0-bot[bot]@users.noreply.github.com> Date: Thu, 7 May 2026 23:00:12 -0700 Subject: [PATCH] fix: pr-bug-scan findings from #1518 (#1575) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added seq + ref guards after awaits in post-scrollback re-subscribe path; awaitReady's prior in-flight promise now resolves before re-arming so old waiters don't pin 3s timers. Findings addressed: - mobile/app/h/[hostId]/session/[worktreeId].tsx:280 — Post-scrollback re-subscribe path lacks a seq guard after awaits - mobile/src/terminal/TerminalWebView.tsx:770 — awaitReady promise replaced without resolving the prior one Rebased onto current main; conflict in worktreeId.tsx resolved by preserving main's existing 'why: scrollback null viewport' comment alongside the new seq-recheck comment. Co-authored-by: orca-bot --- mobile/app/h/[hostId]/session/[worktreeId].tsx | 9 +++++++++ mobile/src/terminal/TerminalWebView.tsx | 14 +++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/mobile/app/h/[hostId]/session/[worktreeId].tsx b/mobile/app/h/[hostId]/session/[worktreeId].tsx index eddbc43f4..c1be03e96 100644 --- a/mobile/app/h/[hostId]/session/[worktreeId].tsx +++ b/mobile/app/h/[hostId]/session/[worktreeId].tsx @@ -383,9 +383,18 @@ export default function SessionScreen() { // phone dims. See log dump 2026-05-06 confirming the // race + measure-result null pattern. await getTerminalRef(handle)?.awaitReady() + if (subscribeSeqRef.current.get(handle) !== seq) return const dims = await getTerminalRef(handle)?.measureFitDimensions( terminalFrameHeightRef.current || undefined ) + // Why: re-check seq after the awaits — awaitReady (up to + // 3s) and measureFitDimensions can take hundreds of ms, + // during which a newer subscribe cycle may have armed + // its own subscription. Tearing it down here would reset + // the freshly-armed initialized flag and re-subscribe a + // stale generation. + if (subscribeSeqRef.current.get(handle) !== seq) return + if (!getTerminalRef(handle)) return // Why: we just got `scrollback` with cols=80 (server's // default fallback for null viewport). That means the // server-side subscriber record was registered before we diff --git a/mobile/src/terminal/TerminalWebView.tsx b/mobile/src/terminal/TerminalWebView.tsx index 21f438bdd..52e199d0e 100644 --- a/mobile/src/terminal/TerminalWebView.tsx +++ b/mobile/src/terminal/TerminalWebView.tsx @@ -1377,9 +1377,17 @@ export const TerminalWebView = forwardRef(function init(cols: number, rows: number, initialData?: string) { // Why: arm a fresh ready promise BEFORE posting init. The WebView // resolves it via the 'ready' notify at the end of its rAF chain. - // Re-init supersedes any prior in-flight ready (we don't bridge - // generations; the older promise just never resolves, and its - // callers have moved on by then). + // Resolve any prior in-flight ready first so awaiters from the + // previous generation don't sit on the 3s setTimeout fallback — + // each leaked timer + closure pinned an awaiting measure caller + // for the full 3s under rapid re-init (orientation change, + // multiple resubscribes), delaying cold-start fit chains. + const priorResolve = readyResolveRef.current + if (priorResolve) { + readyResolveRef.current = null + readyPromiseRef.current = null + priorResolve() + } readyPromiseRef.current = new Promise((resolve) => { readyResolveRef.current = resolve })