fix: ssh clients do not close when underlying connection writing errors

This commit is contained in:
wwqgtxx committed 2026-10-28 19:15:53 +08:00
1 parent ff179c43a1
commit ca8d7c41bf
1 file changed
+19
+19
View File
@@ -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()