Skip to content

SFTPClient does not keep a reference to the Client #344

Open

Description

@FiloSottile

SFTPClient does not keep (properly) a reference to the parent, so when it falls out of scope and gets closed, the sftp object becomes unusable.

>>> import paramiko
>>>
>>> username = "xxx"
>>> key_filename = 'xxx'
>>> hostname = 'xxx'
>>> port = 22

>>> def connect():
...     client = paramiko.SSHClient()
...     client.load_system_host_keys()
...     client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
...     client.connect(hostname, port, username, key_filename=key_filename)
...     sftp = client.open_sftp()
...     return sftp
...
>>> sftp = connect()
>>> sftp.listdir()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/paramiko/sftp_client.py", line 153, in listdir
    return [f.filename for f in self.listdir_attr(path)]
  File "/usr/local/lib/python2.7/site-packages/paramiko/sftp_client.py", line 174, in listdir_attr
    t, msg = self._request(CMD_OPENDIR, path)
  File "/usr/local/lib/python2.7/site-packages/paramiko/sftp_client.py", line 646, in _request
    num = self._async_request(type(None), t, *arg)
  File "/usr/local/lib/python2.7/site-packages/paramiko/sftp_client.py", line 668, in _async_request
    self._send_packet(t, msg)
  File "/usr/local/lib/python2.7/site-packages/paramiko/sftp.py", line 170, in _send_packet
    self._write_all(out)
  File "/usr/local/lib/python2.7/site-packages/paramiko/sftp.py", line 135, in _write_all
    raise EOFError()
EOFError
>>>
>>> client = paramiko.SSHClient()
>>> client.load_system_host_keys()
>>> client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> client.connect(hostname, port, username, key_filename=key_filename)
>>> sftp = client.open_sftp()
>>> sftp.listdir()
[u'xxx']

Activity

  1. Viggor commented on Feb 2, 2015

    @Viggor

    It's because you must return the client variable, if you don't return it the garbage collect it immediatly when the scope change.

  2. FiloSottile commented on Feb 2, 2015

    @FiloSottile
    Author

    Yeah, I know why that happens. My point is that that's completely unexpected in a idiomatic Python environment.

    SFTPClient should keep a reference to Client so that it doesn't get garbage collected while it's in use.

  3. costela commented on Aug 30, 2017

    @costela

    Is this ticket still relevant? The example provided seems to work with e58df62 and #44 was closed by it. Any reason this was was just referenced?

  4. ploxiln commented on Aug 30, 2017

    @ploxiln
    Contributor

    This situation is a bit complicated. There was an intentional mechanism which closed the Transport when the SFTPClient was garbage collected, called a "ResourceManager". It was added to prevent connection leaks. The "Add back-reference" commit was not a good idea, it just defeated the "ResourceManager". So I removed both, in #952. That also fixes this, but leaves the possibility of Transport leaking and never closing the connection (if you don't explicitly close it).

    So yes I think this issue can be closed. (I don't see any particularly new references, unless these old commits being cherry-picked or rebased onto new branches is doing it.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions