mirror of
https://github.com/ARM-software/devlib.git
synced 2025-01-31 02:00:45 +00:00
connection: Terminate background commands on close()
Ensure all background commands are terminated before we close the connection.
This commit is contained in:
parent
27fb0453a3
commit
7bdd6a0ade
@ -66,6 +66,7 @@ class ConnectionBase(InitCheckpoint):
|
|||||||
self._closed = False
|
self._closed = False
|
||||||
self._close_lock = threading.Lock()
|
self._close_lock = threading.Lock()
|
||||||
self.busybox = None
|
self.busybox = None
|
||||||
|
self.logger = logging.getLogger('Connection')
|
||||||
|
|
||||||
def cancel_running_command(self):
|
def cancel_running_command(self):
|
||||||
bg_cmds = set(self._current_bg_cmds)
|
bg_cmds = set(self._current_bg_cmds)
|
||||||
@ -83,11 +84,21 @@ class ConnectionBase(InitCheckpoint):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
|
||||||
|
def finish_bg():
|
||||||
|
bg_cmds = set(self._current_bg_cmds)
|
||||||
|
n = len(bg_cmds)
|
||||||
|
if n:
|
||||||
|
self.logger.debug(f'Canceling {n} background commands before closing connection')
|
||||||
|
for bg_cmd in bg_cmds:
|
||||||
|
bg_cmd.cancel()
|
||||||
|
|
||||||
# Locking the closing allows any thread to safely call close() as long
|
# Locking the closing allows any thread to safely call close() as long
|
||||||
# as the connection can be closed from a thread that is not the one it
|
# as the connection can be closed from a thread that is not the one it
|
||||||
# started its life in.
|
# started its life in.
|
||||||
with self._close_lock:
|
with self._close_lock:
|
||||||
if not self._closed:
|
if not self._closed:
|
||||||
|
finish_bg()
|
||||||
self._close()
|
self._close()
|
||||||
self._closed = True
|
self._closed = True
|
||||||
|
|
||||||
|
@ -696,9 +696,6 @@ class SshConnection(SshConnectionBase):
|
|||||||
def _close(self):
|
def _close(self):
|
||||||
logger.debug('Logging out {}@{}'.format(self.username, self.host))
|
logger.debug('Logging out {}@{}'.format(self.username, self.host))
|
||||||
with _handle_paramiko_exceptions():
|
with _handle_paramiko_exceptions():
|
||||||
bg_cmds = set(self._current_bg_cmds)
|
|
||||||
for bg_cmd in bg_cmds:
|
|
||||||
bg_cmd.close()
|
|
||||||
self.client.close()
|
self.client.close()
|
||||||
|
|
||||||
def _execute_command(self, command, as_root, log, timeout, executor):
|
def _execute_command(self, command, as_root, log, timeout, executor):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user