Recover stale restored SSH PTY sessions
Summary
- Treat missing restored SSH PTY sessions as a distinct attach outcome.
- Fall back to a fresh restored remote shell when a persisted PTY is gone after app restart/update.
- Keep existing retry behavior for transient bridge closes and preserve same-shell reattach when the PTY still exists.
Testing
xcodebuild -project cmux.xcodeproj -scheme cmux-unit -configuration Debug -destination 'platform=macOS' -derivedDataPath /tmp/cmux-stalepty-tests test -only-testing:cmuxTests/TabManagerSessionSnapshotTests/testSessionSnapshotRestoresSplitPersistentSSHPTYWithoutDefaultAttachScaffold -only-testing:cmuxTests/CLINotifyProcessIntegrationRegressionTests/testSSHPTYAttachRequireExistingSessionNotFoundFailsWithoutWaitRetrypassed.xcodebuild -project cmux.xcodeproj -scheme cmux-unit -configuration Debug -destination 'platform=macOS' -derivedDataPath /tmp/cmux-stalepty-tests test -only-testing:cmuxTests/TabManagerSessionSnapshotTests/testSessionSnapshotRestoresPersistentSSHPTYSessionAfterRelaunchpassed.- Docker-backed
tests_v2/test_ssh_remote_detachable_pty.pypassed against tagged buildsptyand/tmp/cmux-debug-spty.sock.
Issues
- Related: user-reported stale persistent SSH PTY after restarting/updating cmux Nightly.
Need help on this PR? Tag /codesmith with what you need. Autofix is disabled.
[!NOTE]
Medium Risk
Touches remote SSH PTY attach exit semantics, restore startup scripts, and reverse-relay recovery—important for session continuity but scoped with tests and distinct exit codes.
Overview
Recovers stale persistent SSH PTY sessions after restart/update by treating a missing PTY as a distinct outcome and optionally starting a fresh remote shell instead of failing or looping on reconnect.
ssh-pty-attach now exits with 253 when the persisted PTY is gone (via remotePTYErrorIndicatesMissingSession), and restored startup scripts handle that code: they show a user message and run a fallback attach without --require-existing, using an embedded --command-b64 remote shell when a relay port is available. Transient bridge closes still retry on 254/255 only.
Restored panel attach commands now pass restoredRemoteShellCommand(relayPort:) as remoteCommand so the fallback can bootstrap a new interactive shell on the same relay.
Reverse SSH relay startup detects “remote port forwarding failed for listen port N” as a stale remote listener, runs cleanupStaleRemoteRelayListenerLocked, uses a shorter retry when cleanup succeeds, and skips publishing a noisy error status when cleanup handled the stale case.
Tests and the detachable-PTY integration test were updated for exit 253, fallback shell wiring, stale-listener detection, and more reliable surface_id resolution after attach.
Reviewed by Cursor Bugbot for commit bdf59442a16e74836c6c636d3714ab148bcb722d. Bugbot is set up for automated code reviews on this repo. Configure here.
Summary by cubic
Detects and recovers from stale restored SSH PTY sessions and stale SSH relay listeners. Falls back to a new remote shell when the persisted PTY is gone, cleans stuck relay ports on relaunch, and keeps the retry loop for transient bridge closures with same-session reattach when the PTY still exists.
- Bug Fixes
ssh-pty-attachreturns exit code 253 when the PTY session is missing and shows a clearer error.- Startup attach script falls back to a new remote shell on 253 using
--command-b64; retries only on 254/255. - Detects stale SSH relay listeners (“remote port forwarding failed for listen port X”), cleans the remote listener, shortens the retry delay, and avoids duplicate error toasts.
- Tests updated to cover missing-session fallback, stale relay listener detection, and more robust
surface_idresolution.
Summary by CodeRabbit
-
Bug Fixes
- Enhanced error handling for SSH PTY session attachment with distinct exit codes (253 for missing sessions, 1 for other errors).
- Improved detection and automatic recovery of stale SSH relay listeners.
- Added fallback attach mechanism when persistent SSH PTY sessions are unavailable.
-
Tests
- Updated test expectations to reflect new exit codes and session recovery behavior.