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 })