Skip to content

NewClient leaves the SSH session open when it returns an error #669

Closed

Description

@christhomas

NewClient opens an SSH session and returns early on several errors without closing it. Each failed call leaves a session channel open on the connection until the connection itself closes.

Versions: v1.13.11, and master at 16147c5.

Cause: in NewClient (client.go):

s, err := conn.NewSession()
...
if err := s.RequestSubsystem("sftp"); err != nil {
	return nil, err
}
return newClientPipe(pr, perr, pw, s.Wait, opts...)
  • When the server refuses the sftp subsystem, the error is returned and s stays open.
  • The same happens if any of the three pipe calls fails.
  • When newClientPipe fails, for example because an option is rejected or the handshake fails, it closes the session's stdin but not the session.

This shows up in a program that tries SFTP first and falls back to something else when a host has no SFTP subsystem: every attempt on such a host leaves one more open session behind on a long-lived connection.

Possible fix: close the session on every error path in NewClient, including when newClientPipe returns an error, for example:

if err := s.RequestSubsystem("sftp"); err != nil {
	s.Close()
	return nil, err
}
c, err := newClientPipe(pr, perr, pw, s.Wait, opts...)
if err != nil {
	s.Close()
	return nil, err
}
return c, nil

Activity

  1. puellanivis commented on Oct 14, 2026

    @puellanivis
    Collaborator

    Sounds good. 👍 Would you like to open the PR yourself, so credit is easily marked?

  2. added a commit that references this issue on Nov 5, 2026
    9a410c8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions