From 7b2d753cae1f7515b1fc7357236937209bf54aa6 Mon Sep 17 00:00:00 2001 From: dolepee Date: Wed, 11 Feb 2026 10:08:50 +0000 Subject: [PATCH 1/3] fix: make rf_mapper scan state thread-safe and cleanup sleep/shutdown handling --- src/backgrounds/plugins/rf_mapper.py | 48 ++++++++++++++++++---------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/src/backgrounds/plugins/rf_mapper.py b/src/backgrounds/plugins/rf_mapper.py index a381d69ec..2cef73b8f 100644 --- a/src/backgrounds/plugins/rf_mapper.py +++ b/src/backgrounds/plugins/rf_mapper.py @@ -71,6 +71,8 @@ def __init__(self, config: RFmapperConfig): self.thread = threading.Thread(target=self._scan_task) self.running = False + # Shared between scan thread and main run loop + self.scan_lock = threading.Lock() self.scan_results: List[RFData] = [] self.scan_idx = 0 self.scan_last_sent = 0 @@ -113,8 +115,6 @@ def __init__(self, config: RFmapperConfig): self.seen_devices: Dict[str, RFData] = {} - self.seen_names: List[str] = [] - self.start() async def scan(self) -> List[RFData]: @@ -217,7 +217,8 @@ def detection_callback(device, advdata: AdvertisementData): final_list.append(device) logging.debug(f"Scan...{final_list}") - self.scan_idx += 1 + with self.scan_lock: + self.scan_idx += 1 return final_list @@ -229,9 +230,13 @@ def _scan_task(self): logging.info("Starting RF scan thread...") self.running = True while self.running: - self.scan_results = self.loop.run_until_complete(self.scan()) - logging.info(f"RF scan index: {self.scan_idx}") - logging.info(f"RF scan last sent: {self.scan_last_sent}") + scan_results = self.loop.run_until_complete(self.scan()) + with self.scan_lock: + self.scan_results = scan_results + scan_idx = self.scan_idx + scan_last_sent = self.scan_last_sent + logging.info(f"RF scan index: {scan_idx}") + logging.info(f"RF scan last sent: {scan_last_sent}") time.sleep(0.5) def start(self): @@ -245,8 +250,7 @@ def stop(self): Stop the background process. """ self.running = False - time.sleep(1) - self.thread.join() + self.thread.join(timeout=1.0) def run(self) -> None: """ @@ -259,14 +263,24 @@ def run(self) -> None: logging.info(f"Sending to fabric: payload {self.payload_idx}") # add scan results if they are new - logging.info(f"RF scan index: {self.scan_idx}") - logging.info(f"RF scan last sent: {self.scan_last_sent}") - fresh_scan_results = [] - if self.scan_results and self.scan_idx > self.scan_last_sent: - fresh_scan_results = self.scan_results - self.scan_last_sent = self.scan_idx - self.scan_results = [] - logging.info(f"RF scan sending new payload: {self.scan_last_sent}") + with self.scan_lock: + scan_idx = self.scan_idx + scan_last_sent = self.scan_last_sent + scan_results = list(self.scan_results) + + logging.info(f"RF scan index: {scan_idx}") + logging.info(f"RF scan last sent: {scan_last_sent}") + + fresh_scan_results: List[RFData] = [] + with self.scan_lock: + if self.scan_results and self.scan_idx > self.scan_last_sent: + fresh_scan_results = list(self.scan_results) + self.scan_last_sent = self.scan_idx + self.scan_results = [] + scan_last_sent = self.scan_last_sent + + if fresh_scan_results: + logging.info(f"RF scan sending new payload: {scan_last_sent}") # basic gps data and occasional scan results try: @@ -347,7 +361,7 @@ def run(self) -> None: except Exception as e: logging.error(f"Error sharing to Fabric: {e}") - self.sleep(1) # we should send a payload every second + time.sleep(1.0) # we should send a payload every second except KeyboardInterrupt: logging.info("Stopping RF scanner...") From c2350dbeb4c80893108e2f412904140d8b3ee4f0 Mon Sep 17 00:00:00 2001 From: dolepee Date: Wed, 11 Feb 2026 10:13:57 +0000 Subject: [PATCH 2/3] fix: remove unused scan_results local in rf_mapper run loop --- src/backgrounds/plugins/rf_mapper.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/backgrounds/plugins/rf_mapper.py b/src/backgrounds/plugins/rf_mapper.py index 2cef73b8f..2581115ff 100644 --- a/src/backgrounds/plugins/rf_mapper.py +++ b/src/backgrounds/plugins/rf_mapper.py @@ -266,7 +266,6 @@ def run(self) -> None: with self.scan_lock: scan_idx = self.scan_idx scan_last_sent = self.scan_last_sent - scan_results = list(self.scan_results) logging.info(f"RF scan index: {scan_idx}") logging.info(f"RF scan last sent: {scan_last_sent}") @@ -274,7 +273,6 @@ def run(self) -> None: fresh_scan_results: List[RFData] = [] with self.scan_lock: if self.scan_results and self.scan_idx > self.scan_last_sent: - fresh_scan_results = list(self.scan_results) self.scan_last_sent = self.scan_idx self.scan_results = [] scan_last_sent = self.scan_last_sent From 87976463a570fb5ceccf04a20250f1a1e1d80b5b Mon Sep 17 00:00:00 2001 From: dolepee Date: Wed, 11 Feb 2026 10:38:51 +0000 Subject: [PATCH 3/3] fix: preserve fresh scan results before clearing shared buffer --- src/backgrounds/plugins/rf_mapper.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/backgrounds/plugins/rf_mapper.py b/src/backgrounds/plugins/rf_mapper.py index 2581115ff..ec0c0bb4f 100644 --- a/src/backgrounds/plugins/rf_mapper.py +++ b/src/backgrounds/plugins/rf_mapper.py @@ -273,6 +273,7 @@ def run(self) -> None: fresh_scan_results: List[RFData] = [] with self.scan_lock: if self.scan_results and self.scan_idx > self.scan_last_sent: + fresh_scan_results = list(self.scan_results) self.scan_last_sent = self.scan_idx self.scan_results = [] scan_last_sent = self.scan_last_sent