1
0
mirror of https://github.com/ARM-software/devlib.git synced 2024-10-05 18:30:50 +01:00

target: Run Target.disconnect() upon process termination

Use atexit handler to run Target.disconnect() when the process is about
to exit. This avoids running it with a half torn down namespace, with
ensuing exceptions and non-clean disconnect.
This commit is contained in:
Douglas Raillard 2024-05-07 11:01:05 +01:00 committed by Marc Bonnici
parent 7714acc897
commit 52281051b2
2 changed files with 11 additions and 9 deletions

View File

@ -13,6 +13,7 @@
# limitations under the License.
#
import atexit
import asyncio
from contextlib import contextmanager
import io
@ -40,6 +41,7 @@ from past.builtins import long
from past.types import basestring
from numbers import Number
from shlex import quote
from weakref import WeakMethod
try:
from collections.abc import Mapping
except ImportError:
@ -413,6 +415,10 @@ class Target(object):
))
self._modules = modules
atexit.register(
WeakMethod(self.disconnect, atexit.unregister)
)
self._update_modules('early')
if connect:
self.connect(max_async=max_async)
@ -530,8 +536,11 @@ class Target(object):
for conn in itertools.chain(connections, self._unused_conns):
conn.close()
if self._async_pool is not None:
self._async_pool.__exit__(None, None, None)
pool = self._async_pool
self._async_pool = None
if pool is not None:
pool.__exit__(None, None, None)
def __enter__(self):
return self

View File

@ -24,14 +24,12 @@ import tempfile
import socket
import sys
import time
import atexit
import contextlib
import select
import copy
import functools
import shutil
from shlex import quote
from weakref import WeakMethod
from paramiko.client import SSHClient, AutoAddPolicy, RejectPolicy
import paramiko.ssh_exception
@ -372,8 +370,6 @@ class SshConnection(SshConnectionBase):
self.client = None
try:
self.client = self._make_client()
weak_close = WeakMethod(self.close, atexit.unregister)
atexit.register(weak_close)
# Use a marker in the output so that we will be able to differentiate
# target connection issues with "password needed".
@ -818,9 +814,6 @@ class TelnetConnection(SshConnectionBase):
self.conn = telnet_get_shell(host, username, password, port, timeout, original_prompt)
weak_close = WeakMethod(self.close, atexit.unregister)
atexit.register(weak_close)
def fmt_remote_path(self, path):
return '{}@{}:{}'.format(self.username, self.host, path)