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

Fix __del__ excep #685

Merged
merged 3 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
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
32 changes: 30 additions & 2 deletions devlib/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
#

import atexit
import asyncio
from contextlib import contextmanager
import io
Expand Down Expand Up @@ -40,6 +41,7 @@
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:
Expand Down Expand Up @@ -413,6 +415,10 @@ def kind_conflict(kind, names):
))
self._modules = modules

atexit.register(
WeakMethod(self.disconnect, atexit.unregister)
)

self._update_modules('early')
if connect:
self.connect(max_async=max_async)
Expand Down Expand Up @@ -521,10 +527,32 @@ async def check_connection(self):

def disconnect(self):
connections = self._conn.get_all_values()
# Now that we have all the connection objects, we simply reset the TLS
# property so that the connections we got will not be reused anywhere.
del self._conn

unused_conns = self._unused_conns
self._unused_conns.clear()

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

def __exit__(self, *args, **kwargs):
self.disconnect()

async def __aenter__(self):
return self.__enter__()

async def __aexit__(self, *args, **kwargs):
return self.__exit__(*args, **kwargs)

def get_connection(self, timeout=None):
if self.conn_cls is None:
Expand Down
7 changes: 0 additions & 7 deletions devlib/utils/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@
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
Expand Down Expand Up @@ -372,8 +370,6 @@ def __init__(self,
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".
Expand Down Expand Up @@ -815,9 +811,6 @@ def __init__(self,

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)

Expand Down