Skip to content

Clean up the terminal and reconnect when SSH connections drop - #6661

Merged
dhh merged 4 commits into
quattrofrom
ssh-dropped-connections
Aug 9, 2026
Merged

dhh merged 4 commits into
quattrofrom
ssh-dropped-connections

Conversation

@dhh

@dhh dhh commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

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

ssh shell wrapper (default/bash/fns/ssh-reconnect)

  • After every ssh exit at a terminal, disarms the modes a dead remote session leaves behind: mouse tracking, focus reporting, the alternate screen, and a hidden cursor.
  • When an established interactive session drops (exit 255 after ≥30s), reconnects automatically until the server is back — surviving fast refusals while it reboots — with Ctrl-C to stop.
  • Never reconnects for remote commands (positional or RemoteCommand via ssh -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.conf with ServerAliveInterval 15 / ServerAliveCountMax 3 / ConnectTimeout 10, so dead connections surface in ~45 seconds instead of hours. ~/.ssh/config is read first and wins.

Review & testing

  • Reviewed with codex xhigh across three rounds; all five findings (remote-command replay, retry abandonment while the server is down, piped-stdin replay, umask-unreadable drop-in, Ctrl-C not honored during retries) fixed and regression-tested.
  • New test/shell.d/ssh-reconnect-test.sh covers 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

dhh and others added 2 commits August 9, 2026 18:18
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]>
Copilot AI balanced review requested due to automatic review settings August 9, 2026 16:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread default/bash/fns/ssh-reconnect Outdated
# 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 '

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 768e259: ssh -G failure now fails closed — the invocation is treated as non-interactive when the effective config cannot be resolved.

Comment thread test/shell.d/ssh-reconnect-test.sh Outdated
Comment on lines +93 to +97
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"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
Copilot AI review requested due to automatic review settings August 9, 2026 16:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Comment thread default/bash/fns/ssh-reconnect Outdated
# since an undetected RemoteCommand must not replay.
local resolved
resolved=$(command ssh -G "${argv[@]}" 2>/dev/null) || return 1
! grep -qi '^remotecommand ' <<<"$resolved"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
Copilot AI review requested due to automatic review settings August 9, 2026 17:13
@dhh
dhh merged commit 6ddc395 into quattro Aug 9, 2026
4 checks passed
@dhh
dhh deleted the ssh-dropped-connections branch August 9, 2026 17:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 -G fail-closed finding is regression-tested, but every _ssh_interactive case here lets ssh -G succeed. A regression that again treats configuration-resolution errors as interactive would pass this suite. Add a case where ssh -G rejects the effective configuration and assert _ssh_interactive fails.
run_interactive -F /dev/null -o "RemoteCommand=none" host ||
  fail "explicit RemoteCommand none stays interactive"
pass "explicit RemoteCommand none stays interactive"

nick1udwig pushed a commit to nick1udwig/omarchy that referenced this pull request Sep 9, 2026
…#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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants