fix: pr-bug-scan findings from #1518 (#1575)

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 <bot@stably.ai>
This commit is contained in:
buf0-bot[bot]
2026-05-07 23:00:12 -07:00
committed by GitHub
co-authored by orca-bot
parent 390f9f52fc
commit 75ae6d9f6a
2 changed files with 20 additions and 3 deletions
@@ -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
+11 -3
View File
@@ -1377,9 +1377,17 @@ export const TerminalWebView = forwardRef<TerminalWebViewHandle, Props>(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<void>((resolve) => {
readyResolveRef.current = resolve
})