Repository navigation
Conversation
The back-reference from Transport to SSHClient was added because the SSHClient had a destructor that would close the Transport, and some users did not want the Transport closed when the SSHClient was garbage collected. The SSHClient destructor was not a normal destructor, it was implemented with the ResourceManager singleton. Together with the back-reference, this prevented the GC cycle detector from freeing the SSHClient and Transport.
It was only recently added, and it's not really needed after the ResourceManager removal. Removing it allows the SSHClient to be garbage-collected if only the Transport (and Channels) are still in use.
| self.active = False | ||
| self.packetizer.close() | ||
| # This line is key. With it, the channel can go out of scope and shut everything down | ||
| chan = None |
There was a problem hiding this comment.
But self._channels also has references to the channels ... but now that I look into it, I see it uses a WeakValueDictionary. So that's the key here, along with your weakref.ref used for transport_proxy - "weak refs"
There was a problem hiding this comment.
(when I was describing why various strategies would not work in the previous PR, I was not considering use of weak refs, but used as they are here they seem somewhat reasonable)
| multiplexed across a single session (and often are, in the case of port | ||
| forwardings). | ||
|
|
||
| Instances of this class may be used as context managers. |
There was a problem hiding this comment.
For this comment to be true, this Transport class should sub-class ClosingContextManager (and TransportThread probably no longer needs to sub-class it).
| will never be garbage collected if it is not explicitly stopped. | ||
| When this goes out of scope it can collect the TransportThread | ||
| """ | ||
| print('Killed') |
There was a problem hiding this comment.
looks like a debug print left in
|
I think that most of the pydoc strings should not be duplicated between |
|
I missed a few methods and properties from the tests. Removed duplicate docstrings as well. I also forgot about a print method I added. Trying to get the updates merged in. Also, yeah I forgot about the context manager thing. |
|
As far as making everything on Transport private, I did consider prefixing every method with an underscore but decided against it. |
|
I agree, renaming all the methods on TransportThread is not a good idea, I just wonder if there's a way to make the whole TransportThread class not show up in the generated docs. |
|
this would probably be a good time to rebase and resolve conflicts |
|
This bug is actively making me sad. Is this still going to be merged in eventually? 2017 was a while ago. |
|
I'm really out of the loop on this. Has it been fixed? I don't notice any more paramiko memory leaks in my code. |
|
There has been no change regarding the need to call |
|
Is this MR in motion because our use case is hitting the memory leak issue |
This is the changes that were discussed in pull request #952 and bug #949.
I moved all public Transport methods and properties to a helper class that closes the transport when it falls out of scope. The class is called Transport as to not break existing code. It's purpose is to act on the TransportThread which was previously the Transport class. The TransportThread keeps a weak reference to Transport just so it can give a strong reference to the Channel.
Once I did this I found one small reference holding the Channel in memory, preventing the Transport from closing. In the Transport's run method it uses a variable called chan. The method is running for the lifetime of the Transport so that variable never goes out of scope. The Tansport is a thread which puts it at the top of the stack, keeping everything it has a reference to in scope. When this variable is no longer needed, it is now set to None, allowing the Channel to close the Transport. The SSHClient or the user's code has a strong reference to Channel, so doing this will not cause it to be lost prematurely.
By setting the chan variable to None when it is no longer needed, the Channel is not kept in scope by the Transport, and the Channel can trigger the closing of the Transport.
During my testing I found that this was sufficient to eliminate all of the memory leak issues.