From ca8d7c41bf620dbe41c8d221f35dc5435216def5 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Wed, 28 Oct 2026 19:10:30 +0800 Subject: [PATCH] fix: ssh clients do not close when underlying connection writing errors --- adapter/outbound/ssh.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/adapter/outbound/ssh.go b/adapter/outbound/ssh.go index 601dfc84..325e7239 100644 --- a/adapter/outbound/ssh.go +++ b/adapter/outbound/ssh.go @@ -74,6 +74,13 @@ func (s *Ssh) connect(ctx context.Context, addr string) (client *ssh.Client, err defer done(&err) } + // Force the SSH transport to shut down when the underlying connection write + // fails. handshakeTransport caches this error without closing the transport, + // which can leave the shared client permanently unusable while Wait remains + // blocked. Closing the connection lets the read and mux loops exit so the + // cached client can be discarded and replaced. + c = closeOnWriteErrorConn{Conn: c} + clientConn, chans, reqs, err := ssh.NewClientConn(c, addr, s.config) if err != nil { return nil, err @@ -95,6 +102,18 @@ func (s *Ssh) connect(ctx context.Context, addr string) (client *ssh.Client, err return client, nil } +type closeOnWriteErrorConn struct { + net.Conn +} + +func (c closeOnWriteErrorConn) Write(p []byte) (int, error) { + n, err := c.Conn.Write(p) + if err != nil { + _ = c.Conn.Close() + } + return n, err +} + // ProxyInfo implements C.ProxyAdapter func (s *Ssh) ProxyInfo() C.ProxyInfo { info := s.Base.ProxyInfo()