Repository navigation
Description
Edit: I found a solution to this. There is a bug in Paramiko. Skip to the last post for the important details.
I wrap all of my ssh connections inside the classes shown below. The classes are invoked like this:
s = SshTask()
s.timeout = s.run_timeout = 14
s.to_host = form.get_store('vmh01')
s.key = settings.KEY
s.command = " "
packages = s.run()
This should leave nothing behind. I scripted a page to repeatedly make the same request to the same server over and over again. I installed Dozer on my Django app. Debug mode is turned off. After a few hours of running my memory went form 100mb to 272mb. Paramiko seems to be the only thing that isn't cleaning up after itself.
Now that the app is idle this is what it is showing for counts:
paramiko.agent.Agent Min: 345 Cur: 345 Max: 345
paramiko.auth_handler.AuthHandler Min: 345 Cur: 345 Max: 345
paramiko.buffered_pipe.BufferedPipe Min: 690 Cur: 690 Max: 690
paramiko.channel.Channel Min: 345 Cur: 345 Max: 345
paramiko.client.AutoAddPolicy Min: 351 Cur: 351 Max: 351
paramiko.client.SSHClient Min: 351 Cur: 351 Max: 351
paramiko.ecdsakey._ECDSACurve Min: 3 Cur: 3 Max: 3
paramiko.ecdsakey._ECDSACurveSet Min: 1 Cur: 1 Max: 1
paramiko.hostkeys.HostKeyEntry Min: 345 Cur: 345 Max: 345
paramiko.hostkeys.HostKeys Min: 702 Cur: 702 Max: 702
paramiko.message.Message Min: 345 Cur: 345 Max: 345
paramiko.packet.Packetizer Min: 351 Cur: 351 Max: 351
paramiko.py3compat.long Min: 3 Cur: 3 Max: 3
paramiko.resource.ResourceManager Min: 1 Cur: 1 Max: 1
paramiko.rsakey.RSAKey Min: 690 Cur: 690 Max: 690
paramiko.ssh_exception.SSHException Min: 6 Cur: 6 Max: 6
paramiko.transport.ChannelMap Min: 351 Cur: 351 Max: 351
paramiko.transport.Transport Min: 351 Cur: 351 Max: 351
paramiko.util.PFilter Min: 1 Cur: 1 Max: 1
Edit: Narrowed this down more, see below post. Removed my implementation. Not relevant anymore.
Activity
agronick commentedon May 1, 2017
Here is a screenshot of all related objects from a transport object that isn't being cleaned up.
Edit: Seems like this might only happen when the following exception is thrown:
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner[Errno 104] Connection reset by peerClean exiting connections seem to be cleaned up fine.
Edit: More research, I seem to have gotten to the bottom of this. I'm seeing that sometimes things will loop in read_all in the packetizer until a rekey exception is thrown. Everything seems to exit normally. For some reason the weak references in the resource manager don't seem to work as intended. You can see that the only thing holding these transport objects in memory is a weak reference. Even calling gc.collect() doesn't make them go away.
All these transports sit in memory, as well as the client connected to them, as well as their last message, and an EOF exception from the last_exception key, and a stack trace on that exception. - A bunch of crap that doesn't seem to go away after any amount of time.
The whole resource manager object seems to be needlessly complex, all to get around the common idiom that you shouldn't rely on the
__del__method. Well the ResourceManager does exactly what the__del__method would do, except the__del__method actually works.If you get rid of the ResourceManager and just throw this on the bottom of Transport:
this whole problem goes away and everything is cleaned up as soon as the rekey exception is thrown. This is on vanilla Python 3.6.
These monkey patches resolve the problem for me:
agronick commentedon May 3, 2017
I found out why this is happening. It all has to do with the ResourceManager. The resource manager says when Client is deleted close the transport. The Client has a reference to transport. Transport has a reference to Client - thats ok. They cancel out.
But when an the transport is registered this method is created which references the Transport as resource:
(Transport == resource)
It assigns this as a callback to be called when the Client is deleted. Now here is the issue. This function references Transport bumping Transport's reference counter up by 1.
As long as this callback exists is memory the Transport's reference counter will never be 0. This callback will exist in memory as long as the Client exists in memory. The Client will exist as long as self._sshclient is set to the Client on the transport.
The only place that self._sshclient is set to None on the Transport is in the close method. So if close() is never called on the transport (because of an exception) you have a cyclical reference that never goes away.
I can think of a few ways to fix this. I still think the best way is to get rid of the ResourceManager entirely. Simple is better than complex. A
__del__method on the Transport does the same thing as this.It would be nice to hear back from someone. Multiple posts on this with no replies, I feel like I'm talking to myself.
[-]Paramiko has huge memory leaks[/-][+]Paramiko has huge memory leaks (Solution Found)[/+]ploxiln commentedon May 4, 2017
This change was recently merged (cherry-picked): #891
That fixed some problems, but I think might have inadvertently caused this leak.
ploxiln commentedon May 4, 2017
Thanks for the thorough analysis of the problem!
So this ResourceManager callback system is what caused the problem that #891 tried to fix: if a reference to the client was not being saved by the program using paramiko, but it was still using the transport, the client would be garbage collected, and then it would close the transport while the program was still using it. But the fix made the ResourceManager useless for this relationship. (And the ResourceManager isn't used anywhere else.)
It seems like the fix is to get rid of the ResourceManager, and figure out the GC situation anew. It's prudent to read why it was introduced in the first place: 029b898
EDIT: fixed link
ploxiln commentedon May 4, 2017
... I now notice your simple "monkey patch" fix, to just get rid of the ResourceManager and add a simple Transport destructor. That seems to make sense.
The ResourceManager does seem pointless. It seems to be nothing more than a SSHClient destructor, but with a complication which "tricks" the GC cycle detector. I hope you don't mind if I describe what happens here in my own way:
ResourceManagerResourceManagerhas reference to_tablemember_tablehas (normal/strong) reference toweakrefobjectweakrefhas a "weak" reference toSSHClient, does not contribute to SSHClient's reference countweakrefhas a (normal/strong) reference to the callback function!TransportSo the GC cycle detector doesn't see that the ResourceManager reference to the Transport is really due to the SSHClient object, it looks like an unrelated reference from the ResourceManager singleton, it looks independent.
Back to what makes sense as the fix. Assuming we remove the ResourceManager: If the program using the SSHClient calls
.close()on it, it will also.close()the transport - so correct programs will work correctly, right? We could leave the back-reference from Transport to SSHClient, either.close()or the cycle detector will clean up, but is the back-reference still needed for anything, can we actually remove that too? I think we can...agronick commentedon May 5, 2017
Yeah, that's exactly whats happening. It took me a day just thinking about how I got it to work right. When I was walking back to my car the second day it finally clicked why it was all happening. It was a real eureka moment.
So issue #44 was that originally the Client getting garbage collected caused close() to be called on the transport. If we just let transport close itself and don't do anything when the Client is garbage collected, we can get remove the ResourceManager and let the transport clean up on gc? Then everything is fine including #44.
ploxiln commentedon May 5, 2017
There's another aspect to this:
The back-reference
Transport._sshclientwas cleared in the body ofTransport.close(), but that would not run if the "SSHException: Error reading SSH protocol banner" was thrown, because that causesTransport.run()to clean up theTransportas it ends, and it clearsTransport.activeto preventclose()from having effect. But the.run()cleanup neglected to clear._sshclient.So if that exception was thrown, even if you called
.close(), the Transport / SSHClient would still leak due to the ResourceManager. But if you called.close()before.run()ends, it un-sets._sshclientand breaks the cycle.Anyway,
ResourceManagerand._sshclientshould still be removed.7 remaining items
update changelog with fix for paramiko#949
update changelog with fix for paramiko#949
update changelog with fix for paramiko#949
rustyscottweber commentedon May 10, 2018
Looking at this issues and many others like it.. I'm wondering if there is a test in that can be run to asses if there are still memory leaks and where the memory leaks are at.
Is there a test that already exists? Or should I potentially look at writing one?
nixit28 commentedon Feb 19, 2019
Is there any workaround to avoid this failure. we are hitting this issue very frequently.
Thanks,
Nixit
ploxiln commentedon Feb 19, 2019
If you ensure
Transport.close()orSSHClient.close()is always eventually called, e.g. using try/finally, then you can avoid leaks.The original issue still stands. If you never call
.close()then it is likely that there will be connections and memory that the python reference counting and garbage collector will never be able to clean up.rustyscottweber commentedon Mar 1, 2019
@ploxiln
Is the memory leak related to only Transport? For those users who are using only a single client which spawns processes, should those users be creating multiple clients, one for each process, instead of keeping a single client from which many processes are spawned?
Do the memory leaks affect long running processes at all?
ploxiln commentedon Mar 1, 2019
An SSHClient uses a Transport - usually just one. There should not be a problem using a client to do multiple things, or long-running things.
rustyscottweber commentedon Nov 5, 2020
It looks like this bug is totally exacerbated by the following python bug:
https://bugs.python.org/issue39074
IE. Not only does paramiko leak the thread, but the leaked threads themselves will also leak memory.
ALWAYS CALL CLOSE on YOUR CLIENTS
pkittenis commentedon Dec 15, 2020
Except calling
.closeonly leads to race conditions and infinite blocking due to cyclic references and other issues in the library.stuartonyx commentedon Nov 21, 2023
Does this still need resolution as of Nov 2023?
agronick commentedon Nov 21, 2023
I think the original issue was fixed in #952 but I haven't been paying attention to this in years.
stuartonyx commentedon Nov 21, 2023
We are still experencing a memory leak when running a deamon service that opens and closes 10s of paramiko connections every 10 minutes. For now the only solution we have is to bounce the deamon every night at midnight but we'd like a more robust solution
stuartonyx commentedon Nov 21, 2023
Python version 3.10, paramiko 3.3.1
agronick commentedon Nov 22, 2023
Does the monkey patch in this comment fix it? #949 (comment)
Back in 2017 I was only seeing it when exceptions were thrown.