New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

channels don't work unless a reference to SSHClient is explicitly kept #44

Closed
w31rd0 opened this Issue Sep 30, 2011 · 2 comments

Comments

Projects
None yet
3 participants
@w31rd0

w31rd0 commented Sep 30, 2011

Using an instance of Channel received from somewhere without a reference to SSHClient being explicitly kept (to prevent it from being garbage collected) sometimes gives unexpected results. For instance, a call to exec_command() raises an exception about the channel being closed, while recv_exit_status() just returns -1.

Here's a synthetic test case.

import paramiko

def exec_remote_command(command, host, username=None, password=None):
    channel = _exec_remote_command(command, host, username=username, password=password)
    return channel.recv_exit_status()

def _exec_remote_command(command, host, username=None, password=None):
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy())
    client.connect(host, username=username, password=password)
    channel = client.get_transport().open_session()
    channel.exec_command(command)
    return channel

if __name__ == '__main__':
    print exec_remote_command('ls', '127.0.0.1') # prints -1
@glasserc

This comment has been minimized.

Contributor

glasserc commented Mar 25, 2013

Thanks for this bug report -- my SFTP session was mysteriously getting closed and I couldn't figure out why!

Is this the intended design for paramiko? This is caused by the SSHClient being registered with the ResourceManager to close its transport when it gets collected. This certainly surprised me, but if it's the intended behavior, maybe it should be documented somewhere.

@bitprophet

This comment has been minimized.

Member

bitprophet commented Apr 28, 2013

This design well predates my involvement so I've got no ready answers. If somebody wants to dig into the Git history for clues or suggest either a doc or API addition to help make this less confusing/surprising, I'd consider a merge.

omeranson added a commit to omeranson/paramiko that referenced this issue Feb 9, 2017

Add back-reference from Transport to the SSHClient that created it
In some cases, the SSH client is created, the command is executed, the
streams are extracted, and the explicit reference to SSHClient is then
discarded (since it was e.g. created in a function that only returns the
streams). In this case, the SHSClient may be garbage collected, and the
connection's state is undefined.

This fix adds a reference from Transport to the SSHClient that created
it. The streams have a reference to the Channel, which references the
Transport. Now that the Transport references the SSHClient, it won't be
garbage collected until it is closed.

Closes-Bug: paramiko#44
Related-Bug: paramiko#344

bitprophet added a commit that referenced this issue Feb 20, 2017

Add back-reference from Transport to the SSHClient that created it
In some cases, the SSH client is created, the command is executed, the
streams are extracted, and the explicit reference to SSHClient is then
discarded (since it was e.g. created in a function that only returns the
streams). In this case, the SHSClient may be garbage collected, and the
connection's state is undefined.

This fix adds a reference from Transport to the SSHClient that created
it. The streams have a reference to the Channel, which references the
Transport. Now that the Transport references the SSHClient, it won't be
garbage collected until it is closed.

Closes-Bug: #44
Related-Bug: #344

bitprophet added a commit that referenced this issue Feb 20, 2017

bitprophet added a commit that referenced this issue Feb 20, 2017

Add back-reference from Transport to the SSHClient that created it
In some cases, the SSH client is created, the command is executed, the
streams are extracted, and the explicit reference to SSHClient is then
discarded (since it was e.g. created in a function that only returns the
streams). In this case, the SHSClient may be garbage collected, and the
connection's state is undefined.

This fix adds a reference from Transport to the SSHClient that created
it. The streams have a reference to the Channel, which references the
Transport. Now that the Transport references the SSHClient, it won't be
garbage collected until it is closed.

Closes-Bug: #44
Related-Bug: #344

dkhapun pushed a commit to cyberx-labs/paramiko that referenced this issue Jun 7, 2018

Add back-reference from Transport to the SSHClient that created it
In some cases, the SSH client is created, the command is executed, the
streams are extracted, and the explicit reference to SSHClient is then
discarded (since it was e.g. created in a function that only returns the
streams). In this case, the SHSClient may be garbage collected, and the
connection's state is undefined.

This fix adds a reference from Transport to the SSHClient that created
it. The streams have a reference to the Channel, which references the
Transport. Now that the Transport references the SSHClient, it won't be
garbage collected until it is closed.

Closes-Bug: paramiko#44
Related-Bug: paramiko#344

dkhapun pushed a commit to cyberx-labs/paramiko that referenced this issue Jun 7, 2018

Add back-reference from Transport to the SSHClient that created it
In some cases, the SSH client is created, the command is executed, the
streams are extracted, and the explicit reference to SSHClient is then
discarded (since it was e.g. created in a function that only returns the
streams). In this case, the SHSClient may be garbage collected, and the
connection's state is undefined.

This fix adds a reference from Transport to the SSHClient that created
it. The streams have a reference to the Channel, which references the
Transport. Now that the Transport references the SSHClient, it won't be
garbage collected until it is closed.

Closes-Bug: paramiko#44
Related-Bug: paramiko#344
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment