diff --git a/belay/device.py b/belay/device.py index 1c4a411..9f80895 100644 --- a/belay/device.py +++ b/belay/device.py @@ -650,6 +650,10 @@ 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. + if self._board is None: + # Has already been closed + return + atexit.unregister(self.close) self._board.cancel_running_program() @@ -657,7 +661,8 @@ def close(self) -> None: for executer in _sort_executers(self._belay_teardown._belay_executers): executer() - return self._board.close() + self._board.close() + self._board = None def reconnect(self, attempts: Optional[int] = None) -> None: """Reconnect to the device and replay the command history.