Skip to content

Remote calls reuse one SSH connection per host for ten minutes - #337

Merged
yourconscience merged 3 commits into
mainfrom
ssh-connection-reuse
Nov 27, 2026
Merged

yourconscience merged 3 commits into
mainfrom
ssh-connection-reuse

Conversation

@yourconscience

@yourconscience yourconscience commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

What and why

tackroom hosts, cron collect and 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 what ssh -G HOST resolves, so two aliases share a connection only when their whole configuration matches, and an edited ~/.ssh/config stops using the connection opened before the edit. The options are passed on the command line, so ~/.ssh/config is 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:

round trip hosts HOST --json cron collect --hosts HOST
loopback 141 → 106 ms 95 → 56 ms
20 ms 337 → 171 ms 289 → 124 ms
100 ms 1087 → 443 ms 1034 → 390 ms
  • The first call still takes the old time; the socket directory is created with mode 0700. ssh -G adds 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 ./... and go vet ./... pass

  • I 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:

  • Reuse one SSH connection per host for up to ten minutes across host checks, cron collection, and remote job actions.
  • Isolate multiplexed connections by the resolved SSH configuration and automatically reconnect when keepalives detect a silent network or configuration changes.

Bug Fixes:

  • Fall back to non-multiplexed SSH connections when the state socket path is too long or the socket setup cannot be used.

Documentation:

  • Document SSH connection reuse, keepalive behavior, and the long-path fallback for cron collection and related remote operations.

@sourcery-ai

sourcery-ai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Implementation

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

  • internal/app/crons.go L414
  • internal/app/crons_actions.go L152-158, L183
  • internal/app/hosts_act.go L234
  • internal/app/ssh.go L1-50

Documentation

Document the ten-minute SSH connection reuse, state socket location, reconnect behavior, and long-path fallback for cron users.

  • docs/crons.md L23

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 29835acf-0d8f-4236-ac89-46ea0e35a4dd
Commits

Reviewing files that changed from the base of the PR and between 4bf66cb and 427f799.

Files selected for processing: 5

  • docs/crons.md
  • internal/app/crons.go
  • internal/app/crons_actions.go
  • internal/app/hosts_act.go
  • internal/app/ssh.go
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

Comment thread internal/app/crons_actions.go Fixed

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread internal/app/ssh.go Resolved
Comment thread internal/app/ssh.go Resolved
Comment thread internal/app/ssh.go Resolved
Comment thread internal/app/crons_actions.go Fixed
@yourconscience
yourconscience merged commit a89b4e2 into main Nov 27, 2026
11 checks passed
@yourconscience yourconscience mentioned this pull request Nov 27, 2026
2 tasks done
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