Skip to content

fix: close the connection when keep-alives stop and allow per-connection settings - #8

Merged
EhabY merged 6 commits into
coder:masterfrom
EhabY:fix/keepalive-timeout-close-connection
Nov 24, 2026
Merged

EhabY merged 6 commits into
coder:masterfrom
EhabY:fix/keepalive-timeout-close-connection

Conversation

@EhabY

@EhabY EhabY commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

The server keep-alive had two gaps that let a silent client keep its connection open forever:

  • On timeout it closed only the session channel, so the connection and its context stayed open. Anything waiting on ctx.Done(), such as the Coder agent's connection reporting, never learned the client was gone.
  • The timer is per connection, but each session's request loop drove it, and the first session to end stopped it. Connections with only port forwards, or whose session had ended, were never probed.

The keep-alive now runs once per connection from HandleConn, sends a global [email protected] request, and, like OpenSSH, closes the connection after ClientAliveCountMax unanswered probes, (ClientAliveCountMax+1) × ClientAliveInterval after the last reply. The previous check fired one interval earlier, so ClientAliveCountMax: 1 closed healthy clients before probing them. Clients reply to the global request with success or failure; either counts as alive. The previously skipped subtest covers it: the session ends, then the client drops all data, and the connection must close.

Servers can override the keep-alive for one connection with SessionKeepAlive.SetClientAlive(interval, countMax), like an sshd_config Match block; a value of zero or less disables it. SessionKeepAlive.TimedOut() reports whether the keep-alive closed the connection, so a server can tell a dead client from a clean close. The loop therefore starts for every connection and waits on a stopped ticker until a keep-alive is set; partial settings disable it instead of closing at the first tick. It reads ctx.Done() and the keep-alive once, before it starts, because SetValue (for example on agent forwarding) rewrites the context without a lock. Part of fixing stale "Ongoing" SSH connection logs; see coder/coder#32353. coder/coder#32521 bumps the fork and coder/coder#32444 uses SetClientAlive.

Verification
$ go test -race -count=30 -run TestSessionKeepAlive .
ok  	github.com/gliderlabs/ssh	36.331s
$ go test -race -count=10 .
ok  	github.com/gliderlabs/ssh	19.549s
# fails with the previous count (CountMax instead of CountMax+1), without the closed check in SetClientAlive, without setting TimedOut, or with Lock instead of TryLock:
$ go test -count=1 -run 'TestSessionKeepAlive' .
FAIL

coder/coder ./agent/... (with -race for agentssh) and the cli SSH tests pass with this fork replaced in.

Written with Coder Agents and reviewed by @EhabY.

Comment thread session.go Resolved
Comment thread session.go Resolved
@EhabY EhabY changed the title fix: close the connection when keep-alive replies stop fix: close the connection when per-connection keep-alives stop Nov 20, 2026
@EhabY
EhabY marked this pull request as ready for review November 20, 2026 09:59
@EhabY
EhabY requested a review from ethanndickson November 20, 2026 10:00
@EhabY EhabY changed the title fix: close the connection when per-connection keep-alives stop fix: close the connection when keep-alives stop and allow per-connection settings Nov 20, 2026
EhabY added a commit to coder/coder that referenced this pull request Nov 20, 2026
…orten the keep-alive

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.

- Divide CODER_AGENT_SSH_MAX_TIMEOUT by CountMax+1 to keep the 72h default
  with the fork's OpenSSH count.
- Report connections closed by the keep-alive as network_error.
- Let clients shorten the keep-alive for their connection with
  CODER_SSH_CLIENT_ALIVE_INTERVAL and CODER_SSH_CLIENT_ALIVE_COUNT_MAX.
EhabY added a commit to coder/coder that referenced this pull request Nov 20, 2026
…live

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.

- Divide CODER_AGENT_SSH_MAX_TIMEOUT by CountMax+1 to keep the 72h default
  with the fork's OpenSSH count.
- Report connections closed by the keep-alive as network_error.
- Let clients shorten the keep-alive for their connection with
  CODER_SSH_CLIENT_ALIVE_INTERVAL and CODER_SSH_CLIENT_ALIVE_COUNT_MAX.
Comment thread session_test.go Resolved
EhabY added a commit to coder/coder that referenced this pull request Nov 23, 2026
…live

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.

- Divide CODER_AGENT_SSH_MAX_TIMEOUT by CountMax+1 to keep the 72h default
  with the fork's OpenSSH count.
- Report connections closed by the keep-alive as network_error.
- Let clients shorten the keep-alive for their connection with
  CODER_SSH_CLIENT_ALIVE_INTERVAL and CODER_SSH_CLIENT_ALIVE_COUNT_MAX.
EhabY added a commit to coder/coder that referenced this pull request Nov 23, 2026
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 closed by the keep-alive as network_error.
- Document when a vanished client's connection closes.
EhabY added a commit to coder/coder that referenced this pull request Nov 23, 2026
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 added a commit to coder/coder that referenced this pull request Nov 23, 2026
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 merged commit 467fa35 into coder:master Nov 24, 2026
EhabY added a commit to coder/coder that referenced this pull request Nov 24, 2026
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 added a commit to coder/coder that referenced this pull request Nov 24, 2026
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. Connections whose session had ended, or
that only carried port forwards, were never probed and could stay Ongoing in
the connection log indefinitely.

- Keep probing every third of CODER_AGENT_SSH_MAX_TIMEOUT (24h by default)
- Report connections that end with a read error, such as the tailnet TCP
  retry limit after a client vanishes, or that the keep-alive closes, as
  network_error with the read error as disconnect_detail
- Keep reporting a clean client close as client_closed
- Wrap the agent's connection to record its first read error while still
  forwarding the client session ID
- Document when a vanished client's connection closes and the tradeoff of
  lowering the timeout

Closes CLIENT-1113
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