Skip to content

fix(ssh): let the gateway notice a client that has gone away - #3

Merged
lucaa6i merged 1 commit into
homee:devfrom
JulienGrv:fix/ssh-gateway-detect-dead-clients
Sep 29, 2026
Merged

lucaa6i merged 1 commit into
homee:devfrom
JulienGrv:fix/ssh-gateway-detect-dead-clients

Conversation

@JulienGrv

@JulienGrv JulienGrv commented Sep 5, 2026 •

Copy link
Copy Markdown
Member

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:

proxy [<name>] already exists

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:

_, err := ch.SendRequest("heartbeat", false, nil)
if err != nil {
    return
}

wantReply is 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 closing doneCh, so nothing is torn down even then.

transport.tcpKeepalive does 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 ClientAlive mechanism, with its names and its defaults:

option default sshd equivalent
sshTunnelGateway.heartbeatInterval 30 ClientAliveInterval (30 keeps the interval this code already used)
sshTunnelGateway.heartbeatCountMax 3 ClientAliveCountMax

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 — so any reply proves the client is alive. The ok value is therefore ignored.

Setting heartbeatCountMax = 0 restores 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

  • Not built or tested where this was written (no Go toolchain); CI here is the first compile.
  • Intended for upstream once it has run in production for a while — kept small, configurable, and shaped like the ssh option it mirrors, with that in mind.
  • The client side of the same problem is fixed separately: clients that close their tunnel with ssh -O exit on 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

@JulienGrv
JulienGrv force-pushed the fix/ssh-gateway-detect-dead-clients branch 2 times, most recently from a644fb1 to 6132a09 Compare September 5, 2026 19:09
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
JulienGrv force-pushed the fix/ssh-gateway-detect-dead-clients branch from 6132a09 to 2254240 Compare September 5, 2026 19:16
@JulienGrv
JulienGrv marked this pull request as ready for review September 29, 2026 09:05
@lucaa6i
lucaa6i merged commit 78cad8d into homee:dev Sep 29, 2026
1 check passed
@JulienGrv
JulienGrv deleted the fix/ssh-gateway-detect-dead-clients branch October 2, 2026 15:06
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