I use an SSHChannel over a "persistent" SSHConnection. Occasionally a previously closed localChannel will attempt to be used and results in a KeyError because the localChannel is not in the SSHConnection.channels[].
Here are some log snippets:
2017-07-05 07:54:50-0500 [-] openChannel localChannelID=2
2017-07-05 07:54:50-0500 [-] opening channel 2 with 65536 32768
2017-07-05 07:54:50-0500 [SSHChannel session (2) on SSHService 'ssh-connection' on ClientTransport,client] sending close 2
2017-07-05 07:54:50-0500 [SSHService 'ssh-connection' on ClientTransport,client] channelClosed id=2
So now channel 2 is closed. Later (after several new channels), I see this:
2017-07-05 08:54:55-0500 [-] openChannel localChannelID=2
2017-07-05 08:54:55-0500 [-] opening channel 2 with 65536 32768
2017-07-05 08:54:55-0500 [SSHService 'ssh-connection' on ClientTransport,client] Unhandled Error Traceback (most recent call last):
<snipped irrelevant>
File "/usr/local/lib/python2.7/dist-packages/twisted/conch/ssh/connection.py", line 180, in ssh_CHANNEL_OPEN_CONFIRMATION channel = self.channels[localChannel]
exceptions.KeyError: 2
Note that I have timers on the connections but I reset these before using.
I'm lost and can't determine why it works "most of the time" but then randomly fails. How can I test to prevent since it seems to open without any noticeable error.
After talking to the twisted guys who offered to look at my code, I made a minimal single-file.py app to perform test runs and never had the issue. Observer altered reality?
Then I realized there are a couple of threads in the application which were there before moving to twisted a long time ago. They never manifested themselves in any other way.
I removed the python threads and now it's running without the channel problem. (As a side note, I had overcome the channel problem by sending deferreds to a point where I could re-start the channel open and it would work but I'm glad to have that work-around out of the code)