micropython / micropython/micropython-lib
Handle MemoryError in aioble_central.py scanner event
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 2.9k
- Forks
- 1.1k
- Avg merge
- 7d 6h
- Merged PRs (30d)
- 3
Description
I am using the scanner as follows in my application code
# The 'scanner' is of type 'scan' defined in aioble
async with scan(duration_ms=10000, # The length of the scan cycle in milliseconds.
interval_us=30000, # The time between each scan listen start
window_us=15000, # The length of time during the scan interval one is actually listening
# If interval_us = window_us the scan is non stop (so called fully duty cycle)
# Background scanning usually sets the window_us to be less than interval_us.
active=True) as scanner: # active = True means do scan requests to get scan responses
# For PHD we always want to do active scans.
However, every once and a while in the scan event handler a MemoryError is thrown while trying to load a result into the queue shown below:
if event == _IRQ_SCAN_RESULT:
addr_type, addr, adv_type, rssi, adv_data = data # data here is a tuple[int, bytes, int, int, bytes]
if not _active_scanner:
return
try:
if queue_scan:
_active_scanner._queue.append((addr_type, bytes(addr), adv_type, rssi, bytes(adv_data)))
_active_scanner._event.set()
except MemoryError:
queue_scan = False
logger.error('LNI: AIOBLE: Memory allocation error queuing an advertisement/scan response.') # type: ignore
import gc
gc.collect()
I have added a handler for the exception and would like to be able to recover from the error but am not sure what else I need to do. With the code shown above, the scanner restarts after 10 seconds but ScanResult objects are no longer created. Not sure why. Do I need to set the event? Do I need to add a filter that blocks repeat advertisements in a given scan cycle? Maybe it is simply not possible to recover from such an error?
Any help is greatly appreciated, especially from the code author.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in aioble_central.py at the _IRQ_SCAN_RESULT handler and trace how _active_scanner, queue_scan, and the scanner event are managed after a MemoryError. Reproduce the provided scan configuration and verify whether scanning can recover and ScanResult objects resume being created without leaving the queue or event state inconsistent.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- embedded-iot
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100