Repository navigation
Remote calls reuse one SSH connection per host for ten minutes - #337
Conversation
Reviewer's GuideImplementationAdd shared SSH connection options based on the resolved host configuration, with bounded socket paths and keepalives, and use them for remote probes, job actions, and host actions.
DocumentationDocument the ten-minute SSH connection reuse, state socket location, reconnect behavior, and long-path fallback for cron users.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 36 minutes. View limit details
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. |
There was a problem hiding this comment.
Hey - I've found 3 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="internal/app/ssh.go" line_range="20" />
<code_context>
+ if err != nil {
+ return opts
+ }
+ socket := filepath.Join(tackroomStateDir(home), "ssh", "%C")
+ // ssh binds the socket under a 17-byte temporary suffix, %C expands to 40
+ // characters, and a socket path holds 104 bytes on macOS. A longer path
</code_context>
<issue_to_address>
**Aliases reuse the wrong connection**
When two SSH aliases have the same `%C` inputs but different routes or authentication settings, and one alias has an open master connection, `ControlPath` gives both aliases the same socket, so OpenSSH reuses the first alias's authenticated connection and bypasses the second alias's configured route or identity; operations can reach the wrong machine or use the wrong identity.
Include the alias or a hash of its connection settings in the control-socket path so aliases with different routes or identities cannot share a master.
Also at `internal/app/ssh.go:32`.
</issue_to_address>
### Comment 2
<location path="internal/app/ssh.go" line_range="20" />
<code_context>
+ if err != nil {
+ return opts
+ }
+ socket := filepath.Join(tackroomStateDir(home), "ssh", "%C")
+ // ssh binds the socket under a 17-byte temporary suffix, %C expands to 40
+ // characters, and a socket path holds 104 bytes on macOS. A longer path
</code_context>
<issue_to_address>
**Percent paths break SSH sockets**
When the home or `XDG_STATE_HOME` path contains an OpenSSH percent-token sequence, openSSH expands percent tokens in `ControlPath`, while `MkdirAll` creates the literal directory, so the control socket is unavailable and remote calls fail or reconnect instead of using multiplexing.
Escape percent characters in the state-directory portion of `ControlPath` while preserving the `%C` token.
Also at `internal/app/ssh.go:32`.
</issue_to_address>
### Comment 3
<location path="internal/app/ssh.go" line_range="24" />
<code_context>
+ // ssh binds the socket under a 17-byte temporary suffix, %C expands to 40
+ // characters, and a socket path holds 104 bytes on macOS. A longer path
+ // would fail the call, so such a machine connects each time.
+ if len(socket)-len("%C")+40+17 >= 104 {
+ return opts
+ }
</code_context>
<issue_to_address>
**Linux connections skip multiplexing**
When the program runs on Linux and the estimated socket path plus suffix is 104–107 bytes, `sshOptions` returns before adding the multiplexing options even though the socket path fits on Linux, so each call opens a fresh connection and misses the advertised reuse.
Use the platform's socket-path limit when deciding whether to enable multiplexing.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 3 findings to address first, and SSH commands can reuse an already authenticated connection for up to ten minutes, so a key or account change that should stop new connections may not stop commands sent through that master immediately; commands run during that window can have effects that reverting this change cannot undo. The window is bounded, and the socket is placed in a user-private directory.
Blocking findings: internal/app/ssh.go:20, internal/app/ssh.go:20, internal/app/ssh.go:24
…a percent sign in the state path is escaped
What and why
tackroom hosts,cron collectand the job actions on another machine opened a new SSH connection for every call, so each one paid the full handshake. The Herdr panel makes two such calls per host on every refresh.Now the first call to a host leaves its connection open for ten minutes behind a socket in
$XDG_STATE_HOME/tackroom/ssh(~/.local/state/tackroom/ssh), and later calls go through it. The socket is named after a hash of whatssh -G HOSTresolves, so two aliases share a connection only when their whole configuration matches, and an edited~/.ssh/configstops using the connection opened before the edit. The options are passed on the command line, so~/.ssh/configis not read differently or changed. Keepalives (5 s, 2 misses) close a connection whose network went silent, so a later call reconnects instead of waiting on it.Where the state directory's path is too long for a socket (ssh fails the whole call at 104 bytes including its own temporary suffix), tackroom leaves the options out and connects each time, as before. With the default state directory that is a home directory path longer than 43 characters.
One thing gets slower: the first call after the network silently drops fails after about 25 s with ssh's own "read from master failed" message, where it used to fail after 10 s. The calls after it fail in 10 s again.
How I checked it
A throwaway sshd on 127.0.0.1 with its own keys, a proxy that adds a fixed delay, and a temporary home on both sides. Released 3.1.0 against this branch, median of 7 runs:
hosts HOST --jsoncron collect --hosts HOSTThe first call still takes the old time; the socket directory is created with mode 0700.
ssh -Gadds about 7 ms to every call.A second alias with the same host, port and user but one different option gets its own socket.
A state path with a percent sign in it works.
Two processes starting at once with no open connection both succeed.
Network frozen with a connection open: the first call reported the host unreachable after 24.4 s, the second after 10.1 s; with the network back the next call took 1.1 s.
With a state path too long for a socket the branch connects each time and still works. The boundary was measured on macOS: a socket path of 86 bytes works, 87 fails.
go test ./...andgo vet ./...passI can explain this change without an AI tool
Summary by Sourcery
Reduce the overhead of repeated remote operations by enabling safe, short-lived SSH connection reuse with resilient fallbacks.
New Features:
Bug Fixes:
Documentation: