Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
channels don't work unless a reference to SSHClient is explicitly kept #44
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
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.