Description
Found three issues in src/backgrounds/plugins/rf_mapper.py that need to be addressed for better code quality and thread safety.
Issues
1. 🔴 Thread Synchronization Problem (Critical)
File: src/backgrounds/plugins/rf_mapper.py
Problem:
Shared variables self.scan_results and self.scan_idx are accessed by two threads without proper synchronization, causing potential race conditions.
Current Code:
# In _scan_task (Thread 1) - Line 215
def _scan_task(self):
while self.running:
self.scan_results = self.loop.run_until_complete(self.scan())
logging.info(f"RF scan index: {self.scan_idx}")
self.scan_idx += 1 # ← Writing without lock
# In run (Thread 2) - Line 240
def run(self):
while self.running:
if self.scan_results and self.scan_idx > self.scan_last_sent: # ← Reading without lock
fresh_scan_results = self.scan_results
self.scan_last_sent = self.scan_idx
self.scan_results = [] # ← Race condition!
Impact:
- Data loss or corruption in scan results
- Inconsistent state between threads
- Unpredictable behavior
Proposed Fix:
# Add lock in __init__
self.scan_lock = threading.Lock()
# Protect writes in _scan_task
with self.scan_lock:
self.scan_results = scan_results
self.scan_idx += 1
# Protect reads in run
with self.scan_lock:
if self.scan_results and self.scan_idx > self.scan_last_sent:
fresh_scan_results = self.scan_results
self.scan_results = []
self.scan_last_sent = self.scan_idx
2. 🟡 Unused Variable
File: src/backgrounds/plugins/rf_mapper.py, Line 105
Problem:
Variable self.seen_names is declared but never used anywhere in the code.
Current Code:
self.seen_devices: Dict[str, RFData] = {}
self.seen_names: List[str] = [] # ← Never referenced
self.start()
Impact:
- Code clutter
- Confusing for maintainers
- Minor memory waste
Proposed Fix:
Remove the line entirely.
3. 🟡 Deprecated Logging Method
File: src/backgrounds/plugins/rf_mapper.py, Line 251
Problem:
Using logging.warn() which is deprecated since Python 3.3. Should use logging.warning() instead.
Current Code:
if g["ble_scan"] is not None:
self.ble_scan = g["ble_scan"]
logging.debug(f"RF scan results {self.ble_scan}")
else:
logging.warn("No nRF52 scan results") # ← Deprecated
Impact:
- Deprecation warnings in Python 3.10+
- May break in future Python versions
- Not following best practices
Proposed Fix:
logging.warning("No nRF52 scan results")
Environment
- Python Version: 3.10
- File:
src/backgrounds/plugins/rf_mapper.py
Description
Found three issues in
src/backgrounds/plugins/rf_mapper.pythat need to be addressed for better code quality and thread safety.Issues
1. 🔴 Thread Synchronization Problem (Critical)
File:
src/backgrounds/plugins/rf_mapper.pyProblem:
Shared variables
self.scan_resultsandself.scan_idxare accessed by two threads without proper synchronization, causing potential race conditions.Current Code:
Impact:
Proposed Fix:
2. 🟡 Unused Variable
File:
src/backgrounds/plugins/rf_mapper.py, Line 105Problem:
Variable
self.seen_namesis declared but never used anywhere in the code.Current Code:
Impact:
Proposed Fix:
Remove the line entirely.
3. 🟡 Deprecated Logging Method
File:
src/backgrounds/plugins/rf_mapper.py, Line 251Problem:
Using
logging.warn()which is deprecated since Python 3.3. Should uselogging.warning()instead.Current Code:
Impact:
Proposed Fix:
Environment
src/backgrounds/plugins/rf_mapper.py