Skip to content

Fix #440 - #441

Closed
lwcolton wants to merge 7 commits into
paramiko:masterfrom
lwcolton:master
Closed

lwcolton wants to merge 7 commits into
paramiko:masterfrom
lwcolton:master

Conversation

@lwcolton

Copy link
Copy Markdown

Fix issue #440.

SSHClient did not need to worry about closing transport, as there is no cyclic reference. Transport can be responsible for closing itself when it's reference count is 0. The following code verifies that Transport.close() is called properly.

import gc

import paramiko
from paramiko.transport import Transport

orig_close = Transport.close
def new_close(*args, **kwargs):
        print("close called")
        orig_close(*args, **kwargs)
Transport.close = new_close

def testp():
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(
            paramiko.AutoAddPolicy())
        ssh.connect("testserver")
        stdin, stdout, stderr = ssh.exec_command("sleep 5; echo works")
        return stdout

def test_gc():
        stdout = testp()

not_real = test_gc()
gc.collect()

Output:
$ python test3p.py
close called

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling 493c569 on lwcolton:master into 838ab5b on paramiko:master.

@lwcolton

Copy link
Copy Markdown
Author

There was an issue with test_6_cleanup in test_client failing. I fixed that by adding a "del" to transport's close. Transport has a cyclic reference with packetizer. This means that letting transport fall out of scope is not enough to have it GC'd and close must be explicitly called.

@lwcolton

Copy link
Copy Markdown
Author

I also added a test in 1ce8b69 to test that #440 will no longer occur

@lndbrg

lndbrg commented Nov 19, 2014

Copy link
Copy Markdown
Contributor

@lwcolton could you see if your fix fixes the issue for python3 too?

if not PY2:

@lwcolton

Copy link
Copy Markdown
Author

Yes I discovered the issue in python 3 and using my patched version have
had no problems
On Nov 19, 2014 6:51 AM, "Olle Lundberg" [email protected] wrote:

@lwcolton https://github.com/lwcolton could you see if your fix fixes
the issue for python3 too?

if not PY2:

—
Reply to this email directly or view it on GitHub
#441 (comment).

@lndbrg

lndbrg commented Nov 19, 2014

Copy link
Copy Markdown
Contributor

Could you push a commit with that if-return thingy removed and see if we fail or succeed on travis?

@lwcolton

Copy link
Copy Markdown
Author

I ran the tests locally and it fails for python 3 so maybe there is an issue. Here is the output for python 3:
http://pastebin.com/NGvDf4sz
Some people suggest that a circular reference is rarely justified, can you point me towards what in Packetizer keeps a reference to the Transport and maybe I can resolve it via an architectural change?

@lndbrg

lndbrg commented Dec 3, 2014

Copy link
Copy Markdown
Contributor

@lwcolton sorry for the late reply! If I remember correctly it is not Packetizer <-> Transport that has a circular reference but Channel <-> Transport.

A transport can have multiple channels (self._channels is an instance of ChannelMap that keeps a reference to a channel and its id for the current transport). A channel is also created from the transport in question and then the transport is injected into the channel (see the _set_transport method and call).

Something like this, graphwise:

Client -> Transport(sock) -> Packetizer(sock)
              |
              v
          ChannelMap([1, Channel(Transport)],
                     [2, Channel(Transport)],
                     ...
                    )

There is also a prior pull request that I wanted to investigate more that fixes a reference cycle in Trasport/Channel: #367 but i would prefer a solution without the need for a weakref.

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