From 9297732806664da369d0c06701969a44d03d5e56 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Sun, 7 Jul 2024 14:59:11 -0700 Subject: [PATCH] fix possible race with thread shutdown. somehow receiveThread can be null --- meshtastic/ble_interface.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/meshtastic/ble_interface.py b/meshtastic/ble_interface.py index 8d7f47e..ff4b5dd 100644 --- a/meshtastic/ble_interface.py +++ b/meshtastic/ble_interface.py @@ -196,7 +196,7 @@ class BLEInterface(MeshInterface): def _sendToRadioImpl(self, toRadio): b = toRadio.SerializeToString() - if b: + if b and self.client: # we silently ignore writes while we are shutting down logging.debug(f"TORADIO write: {b.hex()}") try: self.client.write_gatt_char( @@ -219,10 +219,11 @@ class BLEInterface(MeshInterface): if self._want_receive: self.want_receive = False # Tell the thread we want it to stop - self._receiveThread.join( - timeout=2 - ) # If bleak is hung, don't wait for the thread to exit (it is critical we disconnect) - self._receiveThread = None + if self._receiveThread: + self._receiveThread.join( + timeout=2 + ) # If bleak is hung, don't wait for the thread to exit (it is critical we disconnect) + self._receiveThread = None if self.client: atexit.unregister(self._exit_handler)