From 622e39f8bfdadd8941002ca2102c12c42a46e956 Mon Sep 17 00:00:00 2001 From: Brian Pugh Date: Mon, 13 Feb 2023 18:04:25 -0800 Subject: [PATCH] Register Device.close with atexit --- belay/device.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/belay/device.py b/belay/device.py index 2446795..fce5f63 100644 --- a/belay/device.py +++ b/belay/device.py @@ -1,4 +1,5 @@ import ast +import atexit import concurrent.futures import importlib.resources import linecache @@ -311,6 +312,8 @@ def __init__( for executer in autoinit_executers: executer() + atexit.register(self.close) + self.__post_init__() def __pre_autoinit__(self): @@ -647,6 +650,8 @@ def __exit__(self, exc_type, exc_value, exc_tb): def close(self) -> None: """Close the connection to device.""" # Invoke all teardown executers prior to closing out connection. + atexit.unregister(self.close) + for executer in _sort_executers(self._belay_teardown._belay_executers): executer()