Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid spurious SessionCloseError when intentionally closing session. #268

Merged
merged 2 commits into from
Aug 20, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ncclient/transport/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,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 @@ -271,6 +272,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 @@ -398,6 +400,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 @@ -520,7 +523,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