Skip to content

fix: close silent ssh connections when keep-alives stop - #32601

Merged
EhabY merged 2 commits into
mainfrom
fix/agentssh-connection-keepalive
Nov 24, 2026
Merged

EhabY merged 2 commits into
mainfrom
fix/agentssh-connection-keepalive

Conversation

@EhabY

@EhabY EhabY commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Bumps the gliderlabs/ssh fork to coder/ssh#8 (coder/ssh@467fa35). Before, the agent's keep-alive closed only the session channel on timeout and stopped with the first session, so connections whose session had ended, or that only carried port forwards, were never probed and their connection log rows could stay "Ongoing" indefinitely. The fork now runs one keep-alive per connection and closes the whole connection.

  • Same schedule: the agent still probes every third of CODER_AGENT_SSH_MAX_TIMEOUT (24h by default). The fork now closes after CountMax+1 unanswered intervals like OpenSSH, but over tailnet a vanished client is dropped by the netstack's TCP retry limit about 10 to 30 seconds after the agent's next write, so the probe interval is what matters.
  • Disconnect reason: a connection that ends with a read error, such as the tailnet TCP retry limit after the client vanished, or that the keep-alive closes (SessionKeepAlive.TimedOut()), is reported as network_error instead of client_closed. The read error is logged as disconnect_detail (for example read tcp ...: operation timed out), like the reconnecting PTY's network errors. A clean close by the client is still client_closed. The agent's ConnCallback wraps the connection to record its first read error; the wrapper forwards the client session ID, and a test covers the failed-handshake log that reads it back.
  • Docs: connection-logs.md notes that a vanished client's connection stays Ongoing until the agent next sends it data, and the tradeoff of lowering the timeout.

Refs CLIENT-1113. Lands ahead of #32444 (client opt-in), as suggested in #32444 (comment).

Verification
  • TestSSHServer_KeepAliveClosesSilentConnection (MaxTimeout 3s, client stops answering while its TCP keeps acknowledging) reports network_error; it reports client_closed without the TimedOut mapping.
  • TestSSHServer_ReadErrorReportsNetworkError (the agent's reads start failing) reports network_error with the error as disconnect_detail; it fails without the wrapper, without the detail, or if networkError returns nil.
  • TestSSHShutdownReason now requires client_closed for a client that closes, so it fails if networkError treated a clean close as a network error.
  • TestSSHServer_ConnectionFailedLogsClientSessionID fails if the wrapper drops the client session ID.

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 main this PR
Agent MaxTimeout 2m 51s, client_closed 51s, network_error
Client sends [email protected] every 10s, default MaxTimeout open at 90s open at 90s
Client closes cleanly client_closed client_closed
$ go test -race -count=3 -run 'TestSSHServer_|TestSSHShutdownReason' ./agent/agentssh/
ok  	github.com/coder/coder/v2/agent/agentssh	16.244s
$ go test -race -count=1 ./agent/agentssh/
ok  	github.com/coder/coder/v2/agent/agentssh	7.280s
$ go test -count=1 ./agent/
ok  	github.com/coder/coder/v2/agent	17.662s
$ go test -count=1 -run SSH ./cli/
ok  	github.com/coder/coder/v2/cli	19.593s
$ 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
  • Keep dividing CODER_AGENT_SSH_MAX_TIMEOUT by 3, not CountMax+1: the first unanswered probe ends the connection over tailnet, so dividing by 4 would shorten the effective timeout from 24h to 18h.
  • TimedOut is read inside the sync.Once report, so both report paths (connection close and process exit) agree. It is set before the connection closes.
  • The read error is recorded by a wrapper from the agent's ConnCallback rather than read with sshConn.Wait(), which blocks until the read side fails and could delay the report. The wrapper records the error before the fork cancels the connection context, so the report always sees it. io.EOF and net.ErrClosed (a close by either side) are not network errors.

Written with Coder Agents and reviewed by @EhabY.

@linear-code

linear-code Bot commented Nov 23, 2026

Copy link
Copy Markdown

CLIENT-1113

@EhabY
EhabY force-pushed the fix/agentssh-connection-keepalive branch from b43f01c to 98fe08c Compare November 23, 2026 11:12
@EhabY
EhabY added this pull request to stack #32603 November 23, 2026 11:21
@EhabY
EhabY force-pushed the fix/agentssh-connection-keepalive branch 2 times, most recently from a6f8954 to 3b5dc66 Compare November 23, 2026 11:37
@EhabY
EhabY marked this pull request as ready for review November 23, 2026 12:39

@ethanndickson ethanndickson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

approving for after the coder/ssh PR is merged

Bump the gliderlabs/ssh fork to coder/ssh#8, which runs one keep-alive per
connection and closes the whole connection when the client stops
answering, instead of only the session channel.

- Report connections that end with a read error, such as the tailnet TCP
  retry limit, or that the keep-alive closes, as network_error.
- Document when a vanished client's connection closes.
@EhabY
EhabY force-pushed the fix/agentssh-connection-keepalive branch from 512639c to 6a50934 Compare November 24, 2026 11:57
@EhabY
EhabY merged commit 326a0bd into main Nov 24, 2026
38 checks passed
@EhabY
EhabY deleted the fix/agentssh-connection-keepalive branch November 24, 2026 12:17
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 24, 2026

This branch had an error being deployed

1 failed deployment
docs-preview — 6a509347 Deployed Nov 24, 2026 by EhabY via cleanup #401
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