Repository navigation
fix(codex): serialize session restart after PTY shutdown - #18190
itisvincent wants to merge 1 commit into
Conversation
📝 WalkthroughWalkthroughThe PTY transport now supports asynchronous shutdown and retains the PTY ID after a failed kill for retry. Cleanup runs only after successful shutdown. Codex pane restarts await transport destruction, preserve newer replacements, clear pane-specific layout bindings, and reopen the restart prompt after failure. Concurrent restarts are deduplicated. Tests cover shutdown timing, retry behavior, restart ordering, replacement preservation, and failed shutdown recovery. Merge Risk: 🟡 Moderate · up to Restart cleanup can currently notify the application before the old terminal process has fully stopped, and a failed shutdown can lose the callbacks needed for retry. This may allow stale-session behavior or leave restart recovery incomplete, so the PR is not merge-ready until shutdown cleanup is deferred until successful termination. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Description checkExplanation The description is complete and follows the repository template. It explains the problem and solution, links issue Full details: Linked Issues checkExplanation The changes satisfy issue
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
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Team
Run ID: bbf1dedb-1f8c-4a45-b2be-c7f93baef577
📒 Files selected for processing (5)
src/renderer/src/components/terminal-pane/pty-transport-detach-attach-handoff.test.tssrc/renderer/src/components/terminal-pane/pty-transport-types.tssrc/renderer/src/components/terminal-pane/pty-transport.tssrc/renderer/src/components/terminal-pane/use-terminal-pane-codex-restart-order.test.tsxsrc/renderer/src/components/terminal-pane/use-terminal-pane-process-exit-actions.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.
| } finally { | ||
| handlers.unregisterAll(id) | ||
| storedCallbacks.onDisconnect?.() | ||
| if (disconnectsLivePty) { | ||
| handlers.unregisterAll(id) | ||
| storedCallbacks.onDisconnect?.() | ||
| } |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Defer disconnect cleanup until kill succeeds.
The finally block runs when killPromise.then(...) is returned. It does not wait for the kill promise to settle. Lines 207-208 unregister handlers and emit onDisconnect while shutdown is still pending, and also after a failed shutdown.
A consumer can react to onDisconnect before the old PTY stops. A rejected kill also removes the handlers needed while the retained PTY ID waits for retry. Move this cleanup into the fulfillment path. Preserve a cleanup-required flag so a successful retry also unregisters the original PTY handlers.
Proposed fix
+ const cleanupAfterSuccessfulShutdown =
+ disconnectsLivePty || pendingShutdownPtyId === id
if (id) {
try {
const killPromise = Promise.resolve(window.api.pty.kill(id))
return killPromise.then(
() => {
if (pendingShutdownPtyId === id) {
pendingShutdownPtyId = null
}
+ if (cleanupAfterSuccessfulShutdown) {
+ handlers.unregisterAll(id)
+ storedCallbacks.onDisconnect?.()
+ }
},
(error: unknown) => {
pendingShutdownPtyId = id
throw error
}
)
} catch (error) {
pendingShutdownPtyId = id
throw error
- } finally {
- if (disconnectsLivePty) {
- handlers.unregisterAll(id)
- storedCallbacks.onDisconnect?.()
- }
}
}There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes — a focused renderer-side fix that serializes the mounted Codex account-switch restart until the outgoing local PTY shutdown completes, plus regression coverage for shutdown ordering, concurrent ownership changes, duplicate-restart suppression, and failed-shutdown retry.
- Awaitable local PTY shutdown —
PtyTransport.disconnect/destroynow returnvoid | Promise<void>; the IPC transport resolvesdisconnectonly afterpty.killsettles and retains apendingShutdownPtyIdon failure so a retry targets the same process.handlers.unregisterAll/onDisconnectstill fire synchronously (gated ondisconnectsLivePtyso a retry doesn't double-fire them). - Serialized restart transaction —
runRestartCodexPaneawaitsdestroy, reopens the restart prompt on failure instead of clearing state as if it succeeded, clears only the captured old leaf binding viaclearExitedPanePtyLayoutBindingForLeaf, and rechecks pane ownership before reconnecting. - Duplicate-restart suppression — a per-pane in-flight
useRefmap collapses concurrent restart requests for the same pane onto one transaction. - Tests — new
use-terminal-pane-codex-restart-order.test.tsx, plus shutdown-ordering and failed-shutdown-identity cases inpty-transport-detach-attach-handoff.test.ts.
The type changes are backward compatible (disconnect/destroy accept sync implementations; getPendingShutdownPtyId is optional), so the remote and unresolved-owner transports are unaffected. Non-restart callers of destroy/disconnect (e.g. terminal-pane-pane-closed.ts) discard the returned promise, but that is the same floating-rejection class as the pre-existing unawaited pty.kill, not a regression.
DeepSeek Pro (free via Pullfrog for OSS) | 𝕏
|
Closing this older implementation as superseded by merged #25485, which adopts #24350's host-owned replacement transaction. Shutdown is awaited inside the pane reservation before spawning the replacement, and the stopped process cannot be reattached. @itisvincent is credited in the merged PR and commit trailers for the restart-ordering diagnosis. The separate failed-shutdown prompt/retry behavior proposed here was not adopted. This closure marks the ordering implementation as superseded; it does not claim that additional behavior landed. |

ELI5
Restart now waits for the old Codex terminal process to finish shutting down before Orca starts its replacement. This prevents the replacement from accidentally reconnecting to the same outdated process and showing the setup-change dialog again.
What Changed
Why
Mounted Codex restarts previously launched the replacement immediately after requesting shutdown of the old PTY. Because shutdown is asynchronous, the replacement could reclaim the stable pane while the old PTY was still available, reattach the stale session, and trigger the same setup-change notice again.
Linked Issue
Fixes #18174
Visual Proof
N/A. This changes PTY lifecycle ordering and failure recovery; it does not change dialog layout or styling.
Testing
Validated on Windows:
tsc --noEmit -p config/tsconfig.tc.web.jsonoxlintand React Doctor lint passed on all 5 changed filesoxfmt --writecompleted with no remaining changesgit diff upstream/main...HEAD --checkpassedFull cross-platform coverage is left to CI. The behavior is transport-agnostic and preserves the existing SSH/remote transport contract; only the local IPC transport exposes the optional failed-shutdown identity.
AI Disclosure
OpenAI Codex was used to diagnose, implement, review, and verify this change.
Review
Pre-landing and adversarial review found no concrete issues. The main residual test limitation is that the failure retry is triggered through a hook rerender rather than a literal dialog click; the store prompt lifecycle is covered separately.
Agent skill upstream boundary
docs/reference/agent-skill-sharing-upstream-boundary.mdand copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.Notes
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)