Why does paramiko sporadically raise an exception?
<p>For convenience I have written a small wrapper class to login on a remote host, execute a command, end retrieve the data:</p>

<pre><code>def MySSHClient:

    def connect(self, remoteHost, remotePort, userName, password):
        self.__s = paramiko.SSHClient()
        self.__s.load_system_host_keys()
        self.__s.connect(remoteHost, remotePort, userName, password)

    def exec_command(self, command):
        bufsize = -1
        chan = self.__s.get_transport().open_session()
        chan.exec_command(command)
        stdin = chan.makefile('wb', bufsize)
        stdout = chan.makefile('r', bufsize)
        stderr = chan.makefile_stderr('r', bufsize)
        stdin.close()
        exitcode = chan.recv_exit_status()
        r = MySSHCommandResult(command, stdin, stdout, stderr, exitcode)
        chan.close()
        return r

    def close(self):
        self.__s.close()
</code></pre>

<p>This code is adapted from the original paramiko python implementation. I just added the last 5 lines.</p>

<p>(FYI: MySSHCommandResult reads all data from stdout and strerr during construction and stores it for further use.)</p>

<p>The class MySSHClient is used within a simple python program:</p>

<pre><code>....

exitCode = 0
s = None
try:
    ....
    exitCode = 3
    s = MySSHClient()
    s.connect(host, port, login, password)
    exitCode = 4
    result = s.exec_command(myCommand)
    exitCode = 5
    if not result.isSuccess():
        raise Exception("Failed to execute command!")
    result.dump()    # for current debugging purposes
    exitCode = 0
except:
    pass

if s is not None:
    s.close()
sys.exit(exitCode)
</code></pre>

<p>(Through these exit codes the python program tells the caller if everything succeeded. As you can see a variety of exit codes is used in order to allow a bit of error diagnosis on failure.)</p>

<p>So far so good. Basically this works. But what I do not understand is that sometimes my python program gives additional ouput like this:</p>

<pre><code>Exception ignored in: &lt;bound method BufferedFile.__del__ of &lt;paramiko.ChannelFile from &lt;paramiko.Channel 0 (closed) -&gt;     &lt;paramiko.Transport at 0x74300588 (unconnected)&gt;&gt;&gt;&gt;
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/paramiko/file.py", line 61, in __del__
  File "/usr/local/lib/python3.5/dist-packages/paramiko/file.py", line 79, in close
  File "/usr/local/lib/python3.5/dist-packages/paramiko/file.py", line 88, in flush
TypeError: 'NoneType' object is not callable
</code></pre>

<p>Or like this:</p>

<pre><code>Exception ignored in: &lt;object repr() failed&gt;
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/paramiko/file.py", line 61, in __del__
  File "/usr/local/lib/python3.5/dist-packages/paramiko/file.py", line 79, in close
  File "/usr/local/lib/python3.5/dist-packages/paramiko/file.py", line 88, in flush
TypeError: 'NoneType' object is not callable
</code></pre>

<p>Everything works fine all the time, but at about 10% to 20% of the time I see these error messages. Has anyone an idea why sometimes the cleanup fails on program termination? How can I avoid these error messages?</p>