Skip to content

Memory leaks with Paramiko / Transport wrapper class (Bug #949) - #964

Open
agronick wants to merge 12 commits into
paramiko:2.0from
agronick:sshclient_transport_refs
Open

agronick wants to merge 12 commits into
paramiko:2.0from
agronick:sshclient_transport_refs

Conversation

@agronick

@agronick agronick commented May 19, 2017 •

Copy link
Copy Markdown
Contributor

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.

agronick and others added 4 commits May 5, 2017 15:36
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.
Comment thread paramiko/transport.py
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(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)

Comment thread paramiko/transport.py
multiplexed across a single session (and often are, in the case of port
forwardings).

Instances of this class may be used as context managers.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this comment to be true, this Transport class should sub-class ClosingContextManager (and TransportThread probably no longer needs to sub-class it).

Comment thread paramiko/transport.py Outdated
will never be garbage collected if it is not explicitly stopped.
When this goes out of scope it can collect the TransportThread
"""
print('Killed')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like a debug print left in

@ploxiln

ploxiln commented May 19, 2017

Copy link
Copy Markdown
Contributor

I think that most of the pydoc strings should not be duplicated between Transport and TransportThread, most should just be on the Transport, and the TransportThread should be considered internal/private somehow.

@agronick

agronick commented May 19, 2017 •

Copy link
Copy Markdown
Contributor Author

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.

@agronick

Copy link
Copy Markdown
Contributor Author

As far as making everything on Transport private, I did consider prefixing every method with an underscore but decided against it.

@ploxiln

ploxiln commented May 19, 2017

Copy link
Copy Markdown
Contributor

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.

@ploxiln

ploxiln commented Jun 7, 2017 •

Copy link
Copy Markdown
Contributor

just leaving note of some older related attempts: #440 #441 #367

@ploxiln

ploxiln commented Jun 13, 2017

Copy link
Copy Markdown
Contributor

this would probably be a good time to rebase and resolve conflicts

@maludwig

Copy link
Copy Markdown

This bug is actively making me sad. Is this still going to be merged in eventually? 2017 was a while ago.

@agronick

agronick commented Jan 1, 2019

Copy link
Copy Markdown
Contributor Author

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.

@ploxiln

ploxiln commented Jan 3, 2019

Copy link
Copy Markdown
Contributor

There has been no change regarding the need to call close()

@stuartonyx

Copy link
Copy Markdown

Is this MR in motion because our use case is hitting the memory leak issue

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants