Skip to content

Retry SSH sessions a host refuses instead of logging in on their own - #605

Open
thiblahute wants to merge 1 commit into
powerfooI:mainfrom
thiblahute:upstream/ssh-sessions
Open

thiblahute wants to merge 1 commit into
powerfooI:mainfrom
thiblahute:upstream/ssh-sessions

Conversation

@thiblahute

Copy link
Copy Markdown
Collaborator

Summary

Retry an SSH command whose session the host refuses over the shared master, instead of letting OpenSSH log in on its own, and stop commands killed at their deadline from holding a host session for good.

Why

sshd's default MaxSessions 10 counts a session until its remote command ends, a moment after the client exits, and longer when a command killed here on a timeout still runs there. A burst of reads (a site's pages in the Files preview, for example) could therefore be refused below Roamgate's eight slots. OpenSSH then fell back to a login of its own for each command and printed mux_client_request_session: session request failed: Session open refused by peer, which became the first line of the error shown.

A command waiting on nothing it reads (a locked macOS keychain under glab auth status, a stalled network call) never notices its client is gone, so each one killed at its deadline kept one of the host's sessions until the master closed.

A host can also keep a connection's sessions taken for good: macOS's sshd has been seen to stop reaping its session processes, which stay zombies, so every session of that master stays counted and every command is refused.

What changed

  • server/src/bridge/ssh-command.ts: the process helpers run multiplexed commands with -o ProxyCommand=false ahead of the user's config (SSH_MUX_ONLY_ARGS), so a session the master cannot open fails instead of logging in on its own. Not on Windows clients (no POSIX false).
  • server/src/bridge/ssh-sessions.ts (runSshCommand):
    • A refused command has not run: it runs again over the master after 0.2, 0.5, 1 and 2 s, keeping its slot, then on a connection of its own.
    • For 5 s after a refusal the destination gives no new slot past the ones in use, less one.
    • A gone control socket runs the command cold at once (it never started, so writes are safe too).
    • OpenSSH's lines about the master are dropped from the stderr returned (withoutMuxNoise).
    • A command with a deadline runs under withRemoteDeadline: /bin/sh with a watchdog that kills the session's process group 2 s past the deadline and goes away when the command ends. PowerShell commands (Windows hosts) are left as they are. A client killed here frees its slot at once, but the host's session is counted against the destination until that remote deadline.
    • Eight refusals in a row over at least 15 s, none served in between, recycle the master (at most once a minute); commands go straight to their own connection until it is back.
  • server/src/bridge/ssh-master.ts: recycle() closes the ready master and opens a new one at once.
  • server/src/bridge/ssh-metrics.ts: copySshAnnotation keeps a retry argv's debug annotation.
  • docs/ARCHITECTURE.md: SSH transport section.

Callers that spawn the argv themselves (holdSshSession: uploads, search) keep OpenSSH's own fallback.

Verification

  • bun test server/src/bridge/ssh-sessions.test.ts server/src/bridge/ssh-refused.test.ts server/src/bridge/ssh-openssh.test.ts server/src/bridge/ssh-master.test.ts server/src/bridge/ssh-command.test.ts server/src/bridge/ssh-metrics.test.ts: 63 pass. New tests, which fail on main:
    • ssh-refused.test.ts "commands a host refuses past its sessions wait and run over the master" (a fake ssh refusing past a session count)
    • ssh-openssh.test.ts "runs over the master without OpenSSH's own fallback login", "runs a command whose master socket is gone on its own connection"
    • ssh-sessions.test.ts "retries a refused session over the master after a short wait", "runs a session refused every time on its own connection at last", "gives no new slot for a while after a refusal", "recycles a master only while its host serves no session at all", "withoutMuxNoise keeps the command's own lines", and the "remote deadlines" group
    • ssh-master.test.ts "recycles a master whose host keeps refusing its sessions"
  • bun run format:check, bun run lint (only the existing TerminalView.tsx warning), bun run typecheck:bun, bun run test:quick with an isolated HOME/TMPDIR.

Compatibility

Server only. Commands with a deadline now run on the host as exec /bin/sh -c '<watchdog>' roamgate-deadline <seconds> '<command>'; the commands Roamgate sends are POSIX sh, and the outer line parses alike in sh, bash, zsh and fish. A user ProxyCommand or ProxyJump still applies to the master and to cold runs; only the multiplexed attempt ignores it.

sshd's default MaxSessions 10 counts a session until its remote command
ends, a moment after the client exits (longer when a command killed here
on a timeout still runs there), so a burst of reads, such as a site's
pages in the Files preview, could be refused below Roamgate's eight
slots: OpenSSH then logged in on its own for each and printed
"mux_client_request_session: session request failed: Session open
refused by peer", which became the first line of the error shown.

The process helpers now run multiplexed commands with ProxyCommand=false
ahead of the user's config, so a refused session fails instead: the
command has not run, and runs again over the master after 0.2, 0.5, 1
and 2 seconds, keeping its slot, then on a connection of its own. For
five seconds after a refusal the destination gives no new slot past the
ones in use, less one. A gone socket runs the command cold at once.
OpenSSH's lines about the master are left out of the stderr returned.

A command run with a deadline also ends on the host two seconds after
it: the remote line runs it under /bin/sh with a watchdog that kills the
session's process group, so a command waiting on nothing it reads no
longer holds one of the host's sessions for good. Until then its session
is still counted against the destination's slots.

When the host refuses eight sessions in a row over at least fifteen
seconds, none served in between (an sshd that stopped reaping its
session processes keeps them all counted), the master is closed and
opened again at once, at most once a minute, and commands go straight to
a connection of their own until it is back.
@github-actions github-actions Bot added the enhancement New feature or request label Nov 20, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant