1
0
mirror of https://github.com/ARM-software/devlib.git synced 2025-01-31 02:00:45 +00: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. # limitations under the License.
# #
import atexit
import asyncio import asyncio
from contextlib import contextmanager from contextlib import contextmanager
import io import io
@ -40,6 +41,7 @@ from past.builtins import long
from past.types import basestring from past.types import basestring
from numbers import Number from numbers import Number
from shlex import quote from shlex import quote
from weakref import WeakMethod
try: try:
from collections.abc import Mapping from collections.abc import Mapping
except ImportError: except ImportError:
@ -413,6 +415,10 @@ class Target(object):
)) ))
self._modules = modules self._modules = modules
atexit.register(
WeakMethod(self.disconnect, atexit.unregister)
)
self._update_modules('early') self._update_modules('early')
if connect: if connect:
self.connect(max_async=max_async) self.connect(max_async=max_async)
@ -530,8 +536,11 @@ class Target(object):
for conn in itertools.chain(connections, self._unused_conns): for conn in itertools.chain(connections, self._unused_conns):
conn.close() 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): def __enter__(self):
return self return self

View File

@ -24,14 +24,12 @@ import tempfile
import socket import socket
import sys import sys
import time import time
import atexit
import contextlib import contextlib
import select import select
import copy import copy
import functools import functools
import shutil import shutil
from shlex import quote from shlex import quote
from weakref import WeakMethod
from paramiko.client import SSHClient, AutoAddPolicy, RejectPolicy from paramiko.client import SSHClient, AutoAddPolicy, RejectPolicy
import paramiko.ssh_exception import paramiko.ssh_exception
@ -372,8 +370,6 @@ class SshConnection(SshConnectionBase):
self.client = None self.client = None
try: try:
self.client = self._make_client() 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 # Use a marker in the output so that we will be able to differentiate
# target connection issues with "password needed". # 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) 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): def fmt_remote_path(self, path):
return '{}@{}:{}'.format(self.username, self.host, path) return '{}@{}:{}'.format(self.username, self.host, path)