Repository navigation
Stop new terminal tabs from closing right after they open - #6801
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📥 CommitsReviewing files that changed from the base of the PR and between 54f12ce6325283ba6bd20db6a432dfcd7b2c0d62 and c53872d. 📒 Files selected for processing (5)
🚧 Files skipped from review as they are similar to previous changes (5)
📝 WalkthroughWalkthroughThe dead-session reconciliation logic now uses timestamps to distinguish stale snapshots from newer PTY bindings. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/renderer/src/components/terminal-pane/terminal-dead-session-reconcile.test.ts (1)
174-189: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winMake this test fail on an after-await timestamp capture.
The current
before <= requestedAt <= afterassertion still passes ifrequestedAtis captured afterlistSessions()resolves. Pin a deterministic clock across the callback so the test asserts the exact pre-listSessionsvalue.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 990d5be1-d54e-4cda-b84a-5c96d3c0d710
📥 Commits
Reviewing files that changed from the base of the PR and between 7caa582 and 54f12ce6325283ba6bd20db6a432dfcd7b2c0d62.
📒 Files selected for processing (5)
src/renderer/src/components/terminal-pane/pty-connection.test.tssrc/renderer/src/components/terminal-pane/pty-connection.tssrc/renderer/src/components/terminal-pane/terminal-dead-session-reconcile.test.tssrc/renderer/src/components/terminal-pane/terminal-dead-session-reconcile.tssrc/renderer/src/components/terminal-pane/use-terminal-pane-lifecycle.test.ts
The dead-session reconcile compared a pane's live PTY id against a listSessions() snapshot without regard to when that snapshot was taken. When the snapshot was requested before a freshly-created tab's PTY bound, the new PTY was absent from it, so a healthy newborn pane was mistaken for a reaped session and torn down — closing the tab ~1s after it opened. The window widened as a session accumulated terminals (slower enumeration, more reconcile passes), which is why it worsened over time and cleared on relaunch. Make the reconcile freshness-aware: stamp each binding's bind time and the snapshot's request time (both renderer performance.now()), and skip reconcile when the binding is newer than the snapshot request — a snapshot taken before a PTY existed cannot prove it dead. Genuinely dead sessions still reconcile on the next resume/keystroke pass against a fresher snapshot. SSH and remote-runtime panes are unaffected (they short-circuit before the new guard). Co-authored-by: Orca <[email protected]>
54f12ce to
c53872d
Compare
) Co-authored-by: Orca <[email protected]>
Summary
New terminal tabs sometimes "pop in for a split second, then close" — and once it starts, it keeps happening until Orca is quit and relaunched.
Root cause is a TOCTOU race in the renderer's dead-session reconcile.
reconcileDeadSessions/recheckLivenessAfterInputcallwindow.api.pty.listSessions()(a renderer→main→daemon round-trip) and then ask, for each pane, whether its live PTY id is in the returned set (shouldReconcileDeadSession). If a freshly-created tab's PTY binds after that snapshot was requested but before it resolves, the new live id is absent from the stale snapshot — so a perfectly healthy newborn pane is judged dead and torn down. For a single-pane tab that teardown closes the whole tab (onExit → onPtyExitRef → closeTab).Why it worsens over a session and clears on relaunch: the race window is how long
listSessions()takes relative to a fresh bind. As a session accumulates terminals, the daemon enumerates more sessions and more resume/keystroke reconcile passes fire, widening exposure; relaunch resets to ~0 sessions and the window collapses.Approach
Make the reconcile decision freshness-aware: a snapshot requested before a binding existed cannot prove that binding dead.
activePanePtyBindingBoundAt = performance.now()at the bind chokepoint (bindActivePanePty), reset inclearPanePtyFitBinding.requestedAt = performance.now()immediately before eachlistSessions()call (reconcileDeadSessionsandrecheckLivenessAfterInput) and thread it through to the decision.shouldReconcileDeadSessionnow skips reconcile whenptyBoundAt >= snapshotRequestedAt(inclusive). Both timestamps are renderer-localperformance.now()— same monotonic clock, no cross-process comparison. When either is omitted, behavior is exactly as before (back-compat).Genuinely dead sessions are unaffected: a session reaped while hidden was bound long before the resume's snapshot request, so
ptyBoundAt < requestedAtand reconcile still fires; a skipped round is re-evaluated by the next resume/keystroke pass against a fresher snapshot. SSH (connectionId != null) and remote-runtime (remote:) panes short-circuit before the new guard, so those paths are byte-for-byte unchanged.Design review
Ran
auto-design-review-fix(Claude + Codex in parallel, 2 rounds) on a scratch design doc; converged clean. The review independently traced the timestamp ordering to the daemon and confirmed the renderer-localperformance.now()comparison is provably correct (the renderer→daemon ordering only ever makes the guard more conservative). Substantive doc hardening: a full happens-before correctness proof, correcting a "single chokepoint" overstatement (reattach-completion binds viasetPanePtyFitBinding, intentionally left with nullboundAt→ guard disabled → fail-safe), and fail-open test-coverage requirements.Implementation & deviations
Implemented exactly as designed across 5 renderer files (
terminal-dead-session-reconcile.ts,pty-connection.ts, + their tests, and ause-terminal-pane-lifecycle.test.tsassertion update). No deviations. Intentional non-change: reattach-completed panes keepboundAt === null(guard disabled, pure-membership fallback) — they reuse a pre-existing daemon session id so membership already protects them.Completeness verification
A read-only verification pass confirmed every design requirement, edge case, and validation item is satisfied, with no scope creep and no
max-linesdisables.Code-review loop
Two independent reviewers per round (
code-review, high effort), run in parallel:ptyBoundAt === snapshotRequestedAtboundary test (guards the inclusive>=), and trim two over-length "why" comments per AGENTS.md. Both applied.printWidth=100format failure on a reflowed test line. Fixed withoxfmt.brennan-test-changes (merge confidence): LAND
Validated in a fresh dev build launched from this worktree (identity verified before interacting); test target was a local non-orca repo, with the session already holding 16 tabs (the long-running condition).
listSessions()snapshot captured before the tab was created.listSessionsas a frozen contextBridge fn, so it can't be wrapped from the renderer console) — binding-level proof is instead carried by the integration tests that drive the realreconcileIfSessionDeadwith explicit timestamps.brennan-test-sshnot required — SSH/remote short-circuit before the guard, so they cannot be affected.Checks run
tsgoweb typecheck: clean (exit 0)oxlint(touched files): cleanoxfmt --check(all 5 files): cleannpx vitest run terminal-dead-session-reconcile.test.ts use-terminal-pane-lifecycle.test.ts: 35/35 passpty-connection.test.ts -t "reconcileIfSessionDead": 12/12 pass (incl. two new binding-level integration tests that drive the real reconcile body and fail if the guard is removed — verified by temporary revert)git diff --check: cleanTests
===) and back-compat (omitted-args) cases coveredlistSessionscall sites threadrequestedAtRisks / notes
pty-connection.test.tsfailure ("does NOT re-assert while a mobile-fit override parks the PTY at phone dims") fails identically on the clean pre-diff tree (confirmed viagit stash) — amobile-fit-overridesglobal-state leak, not touched here.Made with Orca 🐋
Relationship to #6796 (just merged)
While this PR was in review, #6796 "Keep new terminal tabs open during startup" merged to
mainand targets the same area (fixes #6773). The two changes are complementary, at different layers — verified by rebasing ontomain(which includes #6796) and confirming the regression test here still fails when this PR's guard is reverted:shouldReconcileDeadSession): even when a reconcile pass legitimately fires — e.g. a tab created while its worktree is hidden, then resumed, or whose first post-resume keystroke probes liveness — alistSessions()snapshot requested before the PTY bound can no longer prove that PTY dead.So #6796 narrows the trigger; this PR closes the residual TOCTOU inside the trigger that still survives. Rebased cleanly on top of #6796; all focused tests, typecheck, lint, and format pass on the merged tree.