Skip to content

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

Merged
EhabY merged 2 commits into
mainfrom
fix/agentssh-report-disconnect-on-close
Nov 20, 2026
Merged

EhabY merged 2 commits 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 today only probes while a session is open and then closes just that session. coder/ssh#8 fixes both 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
  • sync.Once guards the report: a concurrent Do blocks until the first call finishes, so the handler never returns mid-report.
  • 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.

Comment thread agent/agentssh/agentssh.go Resolved

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

👍 after the mutex is replaced with a sync.once

@linear-code

linear-code Bot commented Nov 20, 2026

Copy link
Copy Markdown

VSC-57

@EhabY
EhabY merged commit 5d5d8b1 into main Nov 20, 2026
36 checks passed
@EhabY
EhabY deleted the fix/agentssh-report-disconnect-on-close branch November 20, 2026 10:04
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 20, 2026

This branch had an error being deployed

1 failed and 1 inactive deployments
docs-preview — 28e96184 Deployed Nov 20, 2026 by EhabY via cleanup #274
main — 28e96184 Deployed Nov 20, 2026 by EhabY via cla #260792
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