Skip to content

fix(agent/agentssh): report ssh disconnect when the connection closes - #32353

Draft
EhabY wants to merge 1 commit into
mainfrom
fix/agentssh-report-disconnect-on-close
Draft

EhabY wants to merge 1 commit into
mainfrom
fix/agentssh-report-disconnect-on-close

Conversation

@EhabY

@EhabY EhabY commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Non-PTY SSH sessions (ssh -T, used by VS Code Remote-SSH and other editors) keep their process running after the client leaves, matching OpenSSH (#18519). The disconnect was only reported when that process exited, so connection log rows stayed "Ongoing" while the editor's server or a foreground loop kept running.

The session handler now also reports the disconnect (reason client_closed) when the SSH connection closes, whichever comes first. The report still happens at most once and before the handler returns. Process lifetime is unchanged.

Not covered here: a client that disappears without closing TCP (sleep, kill -9) is only detected by the server keep-alive, which currently closes the session but not the connection. That fix is in coder/ssh and will follow with a fork bump.

Verification

TestSSHShutdownReason/ClientDisconnectWhileProcessRuns (exec sleep 120, client closes) fails on main and passes with this change.

$ go test -race -count=1 ./agent/agentssh/
ok  	github.com/coder/coder/v2/agent/agentssh	5.491s
$ go test -race -count=10 -run 'TestSSHShutdownReason|TestNewServer' ./agent/agentssh/
ok  	github.com/coder/coder/v2/agent/agentssh	34.791s
$ go test -count=1 ./agent/
ok  	github.com/coder/coder/v2/agent	18.448s
$ golangci-lint run ./agent/agentssh/...
0 issues.

A throwaway repro (non-PTY bash session, client closes, disconnect reported within 3s):

Session script main this PR
idle bash reported reported
while true; do sleep 1; done never reported
sleep 1000 & (holds stdout) never reported
sleep 1000 </dev/null &>/dev/null & reported reported
Decision log
  • Report on connection close rather than killing the non-PTY process group, to keep the OpenSSH-compatible behavior from VS Code Desktop connection drops shortly after reconnecting #18519.
  • session.Context() is per connection, so a session closed on a still-open multiplexed connection still reports on process exit, as before.
  • Agent keep-alive is derived from CODER_AGENT_SSH_MAX_TIMEOUT (default 72h, so one probe per 24h). Shortening it is a separate change.

Written with Coder Agents and reviewed by @EhabY.

@EhabY
EhabY temporarily deployed to main November 17, 2026 22:56 — with GitHub Actions Inactive
@EhabY
EhabY temporarily deployed to main November 17, 2026 22:56 — with GitHub Actions Inactive

This branch was previously deployed

1 inactive deployment
main — 187b39c7 Deployed Nov 17, 2026 by EhabY via cla #259004
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.

1 participant