paramiko/paramiko — pull request #441 by lwcolton (2014-11-18 03:08:28+00:00)
Title: Fix #440

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