Skip to content

fix: resolve thread safety, dead code, and memory leak in rf_mapper.py#2428

Open
Wanbogang wants to merge 3 commits intoOpenMind:mainfrom
Wanbogang:fix/rf-mapper-thread-safety-and-memory-leak
Open

fix: resolve thread safety, dead code, and memory leak in rf_mapper.py#2428
Wanbogang wants to merge 3 commits intoOpenMind:mainfrom
Wanbogang:fix/rf-mapper-thread-safety-and-memory-leak

Conversation

@Wanbogang
Copy link
Collaborator

Related Issue

#1771

Summary

This PR addresses the issues reported and adds one additional fix
discovered during code review.

Changes

Point 1: Thread Safety (Critical)

scan_results, scan_idx, and scan_last_sent were accessed by two threads
(_scan_task and run) without synchronization, causing potential race
conditions and data loss.

  • Added self.scan_lock = threading.Lock() in __init__
  • Protected write to scan_results and increment of scan_idx in _scan_task and scan()
  • Protected read/write of scan_results, scan_idx, and scan_last_sent in run()

Point 2: Remove Unused Variable (Dead Code)

self.seen_names: List[str] = [] was declared but never referenced anywhere
in the codebase. Removed entirely.

Point 3: Deprecated logging.warn()

Already fixed

Memory Leak in seen_devices

Discovered during code review: seen_devices was never cleaned up, causing
BLE devices to accumulate in memory indefinitely on long-running sessions.

  • Added TTL cleanup at the end of scan() to remove devices
    not seen for more than 60 seconds

…ice cleanup

- Add scan_lock (threading.Lock) to protect scan_results, scan_idx,
  and scan_last_sent from race conditions between _scan_task and run threads
- Remove unused seen_names variable (dead code)
- Add TTL cleanup in scan() to remove BLE devices not seen for >60s
  to prevent memory leak on long-running sessions
@Wanbogang Wanbogang requested review from a team as code owners February 27, 2026 05:44
@github-actions github-actions bot added robotics Robotics code changes python Python code tests Test files labels Feb 27, 2026
@codecov
Copy link

codecov bot commented Feb 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

- Add -> bool return type to stop_after_one and raise_keyboard_interrupt
  to match RFmapper.sleep(duration: float) -> bool signature
- Fixes pyright reportAttributeAccessIssue in CI/CD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Python code robotics Robotics code changes tests Test files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant