Skip to content

feat(agent/agentssh): let clients shorten the ssh keep-alive interval - #32444

Closed
EhabY wants to merge 1 commit into
mainfrom
feat/agentssh-client-keepalive
Closed

EhabY wants to merge 1 commit into
mainfrom
feat/agentssh-client-keepalive

Conversation

@EhabY

@EhabY EhabY commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up to #32601, which bumped the fork to coder/ssh#8 and landed on its own, per #32444 (comment).

Over tailnet, a client that vanishes (crash, power loss, network loss) is only dropped once the agent writes to it: the netstack's TCP retry limit ends the connection about 10 to 30 seconds later. An idle session gets no writes until the agent's keep-alive, every third of CODER_AGENT_SSH_MAX_TIMEOUT (24h by default), so its connection log row stays "Ongoing" until then. The client's own ServerAliveInterval keep-alive does not help, because a vanished client sends nothing for the agent to answer.

Clients that reconnect on their own, such as VS Code and Cursor, can now send both CODER_SSH_CLIENT_ALIVE_INTERVAL (seconds, raised to 5 if lower) and CODER_SSH_CLIENT_ALIVE_COUNT_MAX in a session's environment. The agent applies them to that connection with the fork's SessionKeepAlive.SetClientAlive, so it probes on the client's schedule. Both variables are stripped from the session environment, and are ignored unless the interval is shorter than the agent's own. Clients that send nothing keep today's behavior. The Coder VS Code extension sends its ServerAliveInterval and ServerAliveCountMax (coder/vscode-coder#1203).

Refs CLIENT-1110.

Verification

TestSSHServer_KeepAliveClosesSilentConnection/ClientKeepAlive (client asks for 5s x 1, then stops answering) closes the connection; it never closes without the SetClientAlive call. TestExtractClientAlive covers parsing, the 5s floor and the shorter-than-agent rule.

A throwaway test with a real agent and client over DERP only, where DERP stops relaying without closing (not committed). Idle session, cut after 2s of quiet:

Setup main this PR
Client sends the env vars (10s x 3) open at 90s closed after 21s
Client sends [email protected] every 10s open at 90s (unchanged)
$ go test -race -count=3 -run 'TestSSHServer_KeepAlive|TestExtractClientAlive|TestSSHShutdownReason|TestNewServer' ./agent/agentssh/
ok  	github.com/coder/coder/v2/agent/agentssh	36.701s
$ go test -race -count=1 ./agent/agentssh/
ok  	github.com/coder/coder/v2/agent/agentssh	14.381s
$ go test -count=1 ./agent/
ok  	github.com/coder/coder/v2/agent	17.921s
$ golangci-lint run ./agent/agentssh/...
0 issues.
$ vale docs/admin/monitoring/connection-logs.md
✔ 0 errors, 0 warnings and 0 suggestions in 1 file.
Decision log
  • Client-driven rather than an agent setting: each client knows whether it recovers from a dropped connection. Plain SSH does not; VS Code and Cursor reconnect over a fresh SSH connection.
  • The client's ServerAlive* values only make the client probe the agent; the agent never sees them, so the client has to send them.
  • The fork owns the keep-alive (SetClientAlive) instead of a second loop in the agent, so a connection has one loop and one timer.
  • A client can only probe more often: the interval must be shorter than the agent's, since the first unanswered probe is what ends the connection over tailnet. The 5s floor is a constant; values above 65535 are ignored so the duration cannot overflow.
  • A later session on the same connection replaces the values and restarts the count.
  • Considered and parked: reporting a disconnect when tailnet removes a lost peer, without closing the connection. It needs a hook across tailnet, the agent, and agentssh.

Written with Coder Agents and reviewed by @EhabY.

@linear-code

linear-code Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

CLIENT-1110

Base automatically changed from fix/agentssh-report-disconnect-on-close to main November 20, 2026 10:04
@EhabY
EhabY force-pushed the feat/agentssh-client-keepalive branch from 2c3b2cc to dd04c11 Compare November 20, 2026 10:04
@EhabY
EhabY removed this pull request from stack #32445 November 20, 2026 11:18
@EhabY
EhabY force-pushed the feat/agentssh-client-keepalive branch from 7856c8c to 3be8358 Compare November 20, 2026 11:22
@EhabY EhabY changed the title feat(agent/agentssh): close connections whose client stops answering keep-alives feat(agent/agentssh): close silent ssh connections and let clients shorten the keep-alive Nov 20, 2026
@EhabY EhabY changed the title feat(agent/agentssh): close silent ssh connections and let clients shorten the keep-alive feat: close silent ssh connections and let clients shorten the keep-alive Nov 20, 2026
@EhabY
EhabY force-pushed the feat/agentssh-client-keepalive branch from 3be8358 to 29b28ad Compare November 20, 2026 11:28
@ethanndickson

Copy link
Copy Markdown
Member

I tried this against a real agent over tailnet (DERP only, then closed the DERP server so neither side sees a close), and I don't think the keep-alive count is what ends a dead client's connection. Our tailscale fork sets TCP max retries to 5 (wgengine/netstack/netstack.go), so the netstack drops the connection 10 to 30s after the agent writes anything that isn't acknowledged. The probe just supplies the write.

Setup Disconnect reported after Reason
Idle session, client sends 10s x 3 20s client_closed
Idle session, agent MaxTimeout 2m (30s x 3) 40s client_closed
No keep-alive, session prints every 5s (same on main) 15s client_closed
No keep-alive, idle session still open at 90s

A few things follow from that, as far as I can tell:

  • TimedOut() is never set on this path, so a vanished client is still logged as client_closed. The test here gets network_error because silentConn drops the SSH replies while the socket underneath keeps acknowledging.
  • VS Code's server sends its own keep-alive every 5s (KeepAliveSendTime in ipc.net.ts), so I'd expect it to look like the third row without the env vars.
  • With the default, an idle session on a sleeping laptop is dropped at the first probe, so dividing by 4 moves that from 24h to 18h, and the new docs line about 72h surviving sleep doesn't hold.

Have you seen a VS Code row stay Ongoing on a build with #32353 and only the fork bump? If not, could we land the bump on its own first and see whether the opt-in is still needed?
🤖

@EhabY
EhabY force-pushed the feat/agentssh-client-keepalive branch from 29b28ad to 78e48f3 Compare November 23, 2026 10:38
@EhabY
EhabY force-pushed the feat/agentssh-client-keepalive branch from 78e48f3 to 656dc39 Compare November 23, 2026 11:10
@EhabY
EhabY changed the base branch from main to fix/agentssh-connection-keepalive November 23, 2026 11:10
@EhabY EhabY changed the title feat: close silent ssh connections and let clients shorten the keep-alive feat(agent/agentssh): let clients shorten the ssh keep-alive interval Nov 23, 2026
@EhabY
EhabY force-pushed the feat/agentssh-client-keepalive branch from 656dc39 to 9ec39b8 Compare November 23, 2026 11:15
@EhabY
EhabY added this pull request to stack #32603 November 23, 2026 11:21
@EhabY
EhabY force-pushed the feat/agentssh-client-keepalive branch from 9ec39b8 to 40845bf Compare November 23, 2026 11:28
@EhabY
EhabY force-pushed the feat/agentssh-client-keepalive branch 3 times, most recently from 877b8ce to 25bc35d Compare November 23, 2026 12:24
Base automatically changed from fix/agentssh-connection-keepalive to main November 24, 2026 12:17
Clients that reconnect on their own can send CODER_SSH_CLIENT_ALIVE_INTERVAL
and CODER_SSH_CLIENT_ALIVE_COUNT_MAX so the agent probes their connection
more often than CODER_AGENT_SSH_MAX_TIMEOUT allows, applied with the fork's
SessionKeepAlive.SetClientAlive.
@EhabY
EhabY force-pushed the feat/agentssh-client-keepalive branch from 25bc35d to a428f10 Compare November 24, 2026 12:20
@EhabY EhabY closed this Nov 24, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 24, 2026
@EhabY

EhabY commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Closed: dogfood testing with #32601 (coder/ssh#8) shows dead IDE connections already close shortly after the client goes away, so the client-driven keep-alive isn't needed for now (CLIENT-1110 canceled). It can be reopened if fully idle sessions turn out to stay Ongoing.

Written with Coder Agents and reviewed by @EhabY.

This branch had an error being deployed

1 failed and 1 inactive (outdated) deployments
docs-preview — a428f10b Deployed Nov 24, 2026 by EhabY via cleanup #421
main — 29b28ad4 Deployed Nov 20, 2026 by EhabY via cla #260898
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants