[paramiko] closing SFTP connections

Robey Pointer robey at lag.net
Sat Feb 10 19:31:51 PST 2007


On 17 Jan 2007, at 16:07, Michael Gratton wrote:

> Robey Pointer wrote:
>>
>> On 17 Dec 2006, at 15:03, Michael Gratton wrote:
>>> Yep, but should the transport also automatically close once all  
>>> channels
>>> are closed? We are seeing this happen most times, but for  
>>> something like
>>> 1 in 20 connections, if all channels are closed (we're only  
>>> opening one
>>> per transport) the transport isn't closed.
>>
>> It shouldn't ever happen.  You can close all channels but may  
>> still want
>> to open another channel over the same transport, and that should  
>> work.
>
> Oh! Okay, that's weird (the behaviour we're seeing, that is).
>
> It sounds pretty dumb, but we never were explicitly closing a  
> transport.
> Channels that get opened were explicitly closed, but the transport  
> never
> was.
>
> So during the course of this process running, a transport and channel
> would be opened, the channel used then closed and the reference to the
> transport lost. This would happen potentially multiple times during  
> the
> course of one program execution. Usually the connection associated  
> with
> the transport was closed - when the refcount for the transport got  
> to 0
> and it was GC'd, I assume. But sometimes the process would not exit
> after it completed - a symptom of this was a still-open connection  
> that
> the process was constantly select()ing (from memory).
>
> Anyway, it's not a problem anymore as we're now explicitly closing the
> transport instances, but it seems like there might be some race or  
> other
> condition when open transport instances are destroyed.
>
> Let me know if you want any more info.


Ah, I might be able to clear it up a little bit:  When a transport  
closes (even if only because it was GC'd), it closes all the  
channels.  It just doesn't happen in reverse (a channel doesn't cause  
a transport to be closed).

There are a few reasons why a transport might not be GC'd.  Python is  
very finicky about calling destructors, so it's always better to  
close the transport explicitly.

robey




More information about the paramiko mailing list