Repository navigation
fix(ssh): let the gateway notice a client that has gone away - #3
Merged
Merged
Conversation
JulienGrv
force-pushed
the
fix/ssh-gateway-detect-dead-clients
branch
2 times, most recently
from
September 5, 2026 19:09
a644fb1 to
6132a09
Compare
A client that vanishes without closing its connection keeps the proxies it registered for as long as the operating system keeps the socket, which on Linux is eleven to fifteen minutes of retransmissions. A device that reboots is refused with `proxy [<name>] already exists` for the whole of that window, so embedded clients that update themselves lose reachability after every update. The heartbeat cannot notice, because it never asks for anything: SendRequest is called with wantReply false, so it only writes into the socket, and on a half-open connection that write keeps succeeding until TCP gives up. When it finally fails the goroutine returns without closing doneCh, so nothing is torn down even then. Add sshTunnelGateway.heartbeatInterval and heartbeatCountMax, which are sshd's ClientAliveInterval and ClientAliveCountMax under different names: the check now asks for a reply, and the connection is closed once that many checks have gone unanswered, releasing the proxy names. Defaults follow sshd - three unanswered checks - and the interval keeps the thirty seconds this code already used. A client that does not implement the request replies with a failure, which is an answer all the same: RFC 4254 requires one whenever want_reply is set. Setting heartbeatCountMax to zero restores the old fire-and-forget behaviour.
JulienGrv
force-pushed
the
fix/ssh-gateway-detect-dead-clients
branch
from
September 5, 2026 19:16
6132a09 to
2254240
Compare
JulienGrv
marked this pull request as ready for review
September 29, 2026 09:05
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem
A client that vanishes without closing its connection keeps the proxies it registered for as long as the operating system keeps the socket — on Linux, eleven to fifteen minutes of retransmissions. A device that reboots is refused for that whole window:
Measured repeatedly on embedded clients that reboot to install updates: back on the network in under a minute, unreachable through the tunnel for another eleven.
Why nothing notices
The heartbeat never asks for anything:
wantReplyis false, so this only writes into the socket — and on a half-open connection that write keeps succeeding, buffered and retransmitted until TCP gives up. When it finally does fail, the goroutine returns without closingdoneCh, so nothing is torn down even then.transport.tcpKeepalivedoes not cover it either: that is applied to the muxer only (svr.muxer.SetKeepAlive(...)), never to the gateway's own listener, so these connections keep the system default of 7200 s.The change
The check now asks for a reply, and the connection is closed once too many checks go unanswered — sshd's
ClientAlivemechanism, with its names and its defaults:sshTunnelGateway.heartbeatIntervalClientAliveInterval(30 keeps the interval this code already used)sshTunnelGateway.heartbeatCountMaxClientAliveCountMaxA client that does not implement the request replies with a failure, which is an answer all the same — RFC 4254 requires one whenever
want_replyis set — so any reply proves the client is alive. Theokvalue is therefore ignored.Setting
heartbeatCountMax = 0restores the old fire-and-forget behaviour.With the defaults, a client that goes away is noticed after ~2 minutes instead of ~13. Deployments that want it tighter can shorten the interval.
Notes
ssh -O exiton shutdown reconnect immediately after a planned reboot. This change is what covers a crash, a power cut or a dropped link, where nothing gets to close anything.🤖 Generated with Claude Code