Skip to content

Commit

Permalink
Avoid spurious SessionCloseError when intentionally closing session.
Browse files Browse the repository at this point in the history
  • Loading branch information
Glenn Matthews committed Aug 20, 2018
1 parent 8ef20f2 commit ace0cae
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ncclient/transport/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def __init__(self, device_handler):
self._inendpos = 0
self._size_num_list = []
self._message_list = []
self._closing = threading.Event()

def _dispatch_message(self, raw):
logger.info("Received from %s session %s:\n%s", self.host, self.id, raw)
Expand Down Expand Up @@ -317,6 +318,7 @@ def load_known_hosts(self, filename=None):
self._host_keys.load(filename)

def close(self):
self._closing.set()
if self._transport.is_active():
self._transport.close()

Expand Down Expand Up @@ -444,6 +446,7 @@ def connect(self, host, port=830, timeout=None, unknown_host_cb=default_unknown_
self._auth(username, password, key_filenames, allow_agent, look_for_keys)

self._connected = True # there was no error authenticating
self._closing.clear()
# TODO: leopoul: Review, test, and if needed rewrite this part
subsystem_names = self._device_handler.get_ssh_subsystem_names()
for subname in subsystem_names:
Expand Down Expand Up @@ -570,7 +573,11 @@ def start_delim(data_len): return '\n#%s\n'%(data_len)
self._parse11()
else:
self._parse10()
elif self._closing.is_set():
# End of session, expected
break
else:
# End of session, unexpected
raise SessionCloseError(self._buffer.getvalue())
if not q.empty() and chan.send_ready():
logger.debug("Sending message")
Expand Down

0 comments on commit ace0cae

Please sign in to comment.