From fe86e485f4ea415dc40a61b2520f6d20a7580468 Mon Sep 17 00:00:00 2001 From: Shin'ichiro Kawasaki Date: Sat, 25 Jul 2020 10:24:48 +0900 Subject: [PATCH] BLESession.close: Improve close handler Guard peripheral disconnect handling with lock not to race with other peripheral operations. After disconnect, set None to the peripheral reference to ensure no use. --- scratch_link.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scratch_link.py b/scratch_link.py index ad2bc1a..7ab4e7f 100755 --- a/scratch_link.py +++ b/scratch_link.py @@ -433,7 +433,9 @@ class BLESession(Session): self.status = self.DONE if self.perip: logger.info(f"disconnect to BLE peripheral: {self.perip}") - self.perip.disconnect() + with self.lock: + self.perip.disconnect() + self.perip = None def __del__(self): self.close()