[paramiko] closing SFTP connections

Robey Pointer robey at lag.net
Fri Dec 15 14:23:15 PST 2006


On 15 Dec 2006, at 11:31, Robey Pointer wrote:

>
> On 10 Dec 2006, at 16:53, Michael Gratton wrote:
>
>> Hi guys,
>>
>> We're using Paramiko for some SFTP transfers and are occasionally
>> seeing
>> connections not getting closed down correctly. This results in the
>> python process seeming to hang. After doing an strace/lsof on such a
>> process, it is continually select'ing an SSH connection.
>>
>> We are closing the connection by calling SFTPClient.close. Looking
>> through the code base it seems that this calls Channel.close but not
>> Transport.close.
>>
>> I'm going to try to work around the problem by calling  
>> Transport.close
>> explicitly after closing the client, but is this something that  
>> should
>> happen in SFTPClient.close automatically?
>
> An SSH transport can host more than one open channel at a time.  For
> example, you could have two open SFTP sessions and a shell session
> all over the same transport.  So having Channel or SFTPClient close
> the Transport might prematurely close other sessions.
>
> (I think we used to have a __del__ method on Transport, that would
> automatically close itself if it was garbage-collected, but python
> interacts badly with __del__ in many cases and I think we decided it
> was safer to not have it.)
>
> Since all Channel and SFTPClient objects belong to a single
> Transport, if you close the Transport (or SSHClient), it should
> automatically close all of its channels.  That will let you still
> have only one "close()" call.

Responding to myself here:

It occurred to me later that SSHClient could attach the transport to  
a ResourceManager, so that when an SSHClient object is collected, the  
transport is closed.  This appears to work.  (It won't work for  
Transport objects themselves because they launch a thread.)  I  
checked it in as revision 421, so if you're interested, you can try  
that out.  It should mean that if you use SSHClient, when the  
SSHClient object is garbage collected, the underlying SSH transport  
is cleanly shutdown.

robey




More information about the paramiko mailing list