Repository navigation
Clean up the terminal and reconnect when SSH connections drop - #6661
Conversation
A remote tmux, herdr, or editor arms terminal modes over the SSH pipe (mouse tracking, focus reporting, the alternate screen) that only it can disarm. When the connection dies instead of exiting cleanly, those modes stay armed on the local terminal, and every mouse move floods the prompt with escape-sequence junk. Wrap ssh in a shell function that disarms those modes after every exit, and automatically reconnects when an established interactive session drops. Remote commands, configured RemoteCommands, and redirected stdin never reconnect, so their side effects cannot replay, and the retry loop runs in a subshell so Ctrl-C cancels both the in-flight attempt and the loop. Co-Authored-By: Claude Fable 5 <[email protected]>
Without keepalives, ssh does not notice a dead peer until TCP gives up, which can take hours of sitting on a hung terminal with remote-armed terminal modes stuck on. Ship a client keepalive default so drops are detected in about 45 seconds, letting the shell's ssh wrapper clean up and reconnect. ~/.ssh/config is read first and wins, so per-host overrides still apply. Co-Authored-By: Claude Fable 5 <[email protected]>
There was a problem hiding this comment.
Pull request overview
Adds SSH terminal cleanup, automatic reconnection, and system-wide keepalive defaults.
Changes:
- Adds an SSH wrapper that resets terminal modes and reconnects dropped sessions.
- Configures SSH keepalive and connection timeouts for new and existing installations.
- Adds shell tests for argument parsing and reconnection behavior.
Tip
If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
default/bash/fns/ssh-reconnect |
Implements cleanup and reconnection logic. |
test/shell.d/ssh-reconnect-test.sh |
Tests parsing, cleanup, and retries. |
install/config/ssh-keepalive.sh |
Installs keepalive defaults. |
install/config/all.sh |
Runs the new installer configuration. |
migrations/1786278735.sh |
Applies keepalive defaults to existing systems. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| # A RemoteCommand from ssh_config or -o replays on reconnect just like a | ||
| # positional command; ssh -G resolves the effective configuration for this | ||
| # exact invocation without connecting. | ||
| ! command ssh -G "${argv[@]}" 2>/dev/null | grep -qi '^remotecommand ' |
There was a problem hiding this comment.
Fixed in 768e259: ssh -G failure now fails closed — the invocation is treated as non-interactive when the effective config cannot be resolved.
| mouse_off=$(printf '\e[?1003l') | ||
|
|
||
| out=$(run_case "0 255" host) | ||
| [[ $out == *$mouse_off* ]] || fail "stray terminal modes are reset after ssh exits" "$out" | ||
| pass "stray terminal modes are reset after ssh exits" |
There was a problem hiding this comment.
Fixed in 768e259: the test now asserts the complete disarm sequence (1000/1002/1003/1006, 1004, 1049, cursor show).
| # Retry in a subshell: Ctrl-C reaches the whole foreground process group, | ||
| # so it cancels both the in-flight attempt and the loop itself. Keep | ||
| # retrying fast failures, since a rebooting server refuses connections too. | ||
| ( |
There was a problem hiding this comment.
Added in 768e259: a pty test drives a real interactive bash (job control on), types the ssh command, sends ^C mid-retry, and asserts the loop dies with rc=130 after ~3 attempts instead of draining the plan.
An unresolvable configuration could hide a RemoteCommand, so treat it as non-interactive rather than reconnectable. Also strengthen the tests from Copilot review: assert the complete disarm sequence, and verify on a real interactive pty that Ctrl-C during a retry attempt kills the reconnect loop itself, not just the in-flight attempt. Co-Authored-By: Claude Fable 5 <[email protected]>
| # since an undetected RemoteCommand must not replay. | ||
| local resolved | ||
| resolved=$(command ssh -G "${argv[@]}" 2>/dev/null) || return 1 | ||
| ! grep -qi '^remotecommand ' <<<"$resolved" |
There was a problem hiding this comment.
Fixed in bbf238a. Note: on the OpenSSH Omarchy ships (10.4p1), ssh -G emits no remotecommand line at all when unset (verified locally), so reconnect wasn't broken on target systems — but the explicit RemoteCommand none form (and versions that emit it when unset) would indeed have failed closed. The probe now treats remotecommand none as no remote command, still fails closed on real commands and unresolvable configs, and the fake ssh -G now emits the none form so the behavior tests cover exactly this.
The literal "none" is how ssh_config cancels a configured RemoteCommand, and some OpenSSH versions emit it even when unset, which would have silently disabled reconnecting entirely. Treat it as no remote command while still failing closed on real ones and unresolvable configs, and make the fake ssh -G emit the "none" form so the behavior tests cover it. Co-Authored-By: Claude Fable 5 <[email protected]>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.
Suppressed comments (1)
test/shell.d/ssh-reconnect-test.sh:43
- The PR description says the
ssh -Gfail-closed finding is regression-tested, but every_ssh_interactivecase here letsssh -Gsucceed. A regression that again treats configuration-resolution errors as interactive would pass this suite. Add a case wheressh -Grejects the effective configuration and assert_ssh_interactivefails.
run_interactive -F /dev/null -o "RemoteCommand=none" host ||
fail "explicit RemoteCommand none stays interactive"
pass "explicit RemoteCommand none stays interactive"
…#6661) * Clean up the terminal and reconnect when SSH connections drop A remote tmux, herdr, or editor arms terminal modes over the SSH pipe (mouse tracking, focus reporting, the alternate screen) that only it can disarm. When the connection dies instead of exiting cleanly, those modes stay armed on the local terminal, and every mouse move floods the prompt with escape-sequence junk. Wrap ssh in a shell function that disarms those modes after every exit, and automatically reconnects when an established interactive session drops. Remote commands, configured RemoteCommands, and redirected stdin never reconnect, so their side effects cannot replay, and the retry loop runs in a subshell so Ctrl-C cancels both the in-flight attempt and the loop. Co-Authored-By: Claude Fable 5 <[email protected]> * Detect dead SSH connections within a minute Without keepalives, ssh does not notice a dead peer until TCP gives up, which can take hours of sitting on a hung terminal with remote-armed terminal modes stuck on. Ship a client keepalive default so drops are detected in about 45 seconds, letting the shell's ssh wrapper clean up and reconnect. ~/.ssh/config is read first and wins, so per-host overrides still apply. Co-Authored-By: Claude Fable 5 <[email protected]> * Fail closed when ssh -G cannot resolve the effective config An unresolvable configuration could hide a RemoteCommand, so treat it as non-interactive rather than reconnectable. Also strengthen the tests from Copilot review: assert the complete disarm sequence, and verify on a real interactive pty that Ctrl-C during a retry attempt kills the reconnect loop itself, not just the in-flight attempt. Co-Authored-By: Claude Fable 5 <[email protected]> * Tolerate the explicit RemoteCommand none when probing ssh -G The literal "none" is how ssh_config cancels a configured RemoteCommand, and some OpenSSH versions emit it even when unset, which would have silently disabled reconnecting entirely. Treat it as no remote command while still failing closed on real ones and unresolvable configs, and make the fake ssh -G emit the "none" form so the behavior tests cover it. Co-Authored-By: Claude Fable 5 <[email protected]> --------- Co-authored-by: Claude Fable 5 <[email protected]>
Coming back to a terminal whose SSH session died mid-connection leaves it flooding the prompt with mouse-tracking escape junk: the remote tmux/herdr armed mouse reporting on the local terminal, and nothing ever disarmed it. On top of that, ssh itself doesn't notice a dead peer until TCP gives up, so the terminal just hangs.
Changes
sshshell wrapper (default/bash/fns/ssh-reconnect)RemoteCommandviassh -G), redirected stdin, or fast connect/auth failures, so side effects cannot replay. The retry loop runs in a subshell so Ctrl-C reaches both the in-flight attempt and the loop.Keepalive default (
install/config/ssh-keepalive.sh+ migration)/etc/ssh/ssh_config.d/20-omarchy-keepalive.confwithServerAliveInterval 15/ServerAliveCountMax 3/ConnectTimeout 10, so dead connections surface in ~45 seconds instead of hours.~/.ssh/configis read first and wins.Review & testing
test/shell.d/ssh-reconnect-test.shcovers argv parsing and the reconnect behaviors on a pty with a scripted fake ssh. Full./test/all: 142 test files pass.🤖 Generated with Claude Code