Skip to content

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

Closed

Description

@w31rd0

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

Activity

  1. glasserc commented on Mar 25, 2013

    @glasserc
    Contributor

    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.

  2. bitprophet commented on Apr 28, 2013

    @bitprophet
    Member

    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.

  3. added a commit that references this issue on Nov 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions