adafruit / adafruit/Adafruit_CircuitPython_BLE

memory allocation error in simpletest

Open
#182 7 comments 1 reaction 0 assignees View on GitHub
Dominant language
Python
Stars
138
Forks
58
PR merge metrics
No merged PRs in 30d

Description

I have been testing BLE on both a feather_esp32s3 no-psram and on an itsybitsy_nrf52840 and I am seeing the same failure when running the ble_simplest.py. It scans for awhile, then crashes with a memory failure. The failure can happen immediately or after some time.
Is this a known issue?

this was run with `Adafruit CircuitPython 8.0.3 on 2023-02-23; Adafruit Feather ESP32S3 No PSRAM with ESP32S3`
with the example from the bundle
```
# SPDX-FileCopyrightText: 2020 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
This example scans for any BLE advertisements and prints one advertisement and one scan response
from every device found.
"""

from adafruit_ble import BLERadio

ble = BLERadio()
print("scanning")
found = set()
scan_responses = set()
for advertisement in ble.start_scan():
addr = advertisement.address
if advertisement.scan_response and addr not in scan_responses:
scan_responses.add(addr)
elif not advertisement.scan_response and addr not in found:
found.add(addr)
else:
continue
print(addr, advertisement)
print("\t" + repr(advertisement))
print()

print("scan done")
```
results

```
>>> import ble_simpletest
scanning

>
Advertisement(data=b"\x0b\xff\x4c\x00\x09\x06\x03\x2c\x0a\x00\x00\xf5\x02\x01\x1a")

>
Advertisement(data=b"\x10\xff\x70\x08\x03\x06\x7c\x78\xb2\x53\x3c\x99\x01\x01\x01\x0a\x01\x02\x01\x06")

tx_power=12 >
Advertisement(data=b"\x02\x01\x1a\x02\x0a\x0c\x0a\xff\x4c\x00\x10\x05\x09\x14\xb9\x96\xf6")

tx_power=12 >
Advertisement(data=b"\x02\x01\x1a\x02\x0a\x0c\x0a\xff\x4c\x00\x10\x05\x05\x1c\x91\x26\x72")

>
Advertisement(data=b"\x11\x07\xfc\x9d\xd0\xb3\xcb\x84\xe0\x84\x06\x42\xf3\xf7\xe1\xe0\xbf\xcb\x02\x01\x1a")


Advertisement(data=b"\x17\x16\x6f\xfd\x8f\xc5\x79\x22\x82\x28\x18\xa8\xb6\xb0\x68\x37\xff\xd3\xe4\x26\x8c\xef\x21\x51\x03\x03\x6f\xfd")

>
Advertisement(data=b"\x1a\xff\x4c\x00\x0c\x0e\x00\xb7\xb3\xc2\x02\x2d\x79\xc4\xa7\x80\x15\xf8\xb5\x6c\x10\x05\x4a\x1c\xed\x5a\xff\x02\x01\x1a")

>
Advertisement(data=b"\x0b\xff\x4c\x00\x09\x06\x03\x3a\x0a\x00\x00\xc9\x02\x01\x1a")

>
Advertisement(data=b"\x1a\xff\x4c\x00\x0c\x0e\x00\xb7\xb3\xc2\x02\x2d\x79\xc4\xa7\x80\x15\xf8\xb5\x6c\x10\x05\x4a\x1c\xed\x5a\xff\x02\x01\x1a")

tx_power=12 >
Advertisement(data=b"\x02\x01\x1a\x02\x0a\x0c\x0a\xff\x4c\x00\x10\x05\x08\x1c\x07\x3b\x90")

>
Advertisement(data=b"\x10\xff\x70\x08\x03\x06\x7c\x78\xb2\x53\x76\xfd\x01\x01\x01\x0a\x01\x02\x01\x06")


Advertisement(data=b"\x1d\xff\x2d\x01\x02\x00\x01\x10\x0e\x9a\xfe\x0c\xa0\x22\x46\x9c\x88\xab\xd1\x28\xde\xdf\xbd\xfa\xbc\x90\xd2\xae\xec\x54")

tx_power=7 >
Advertisement(data=b"\x02\x01\x1a\x02\x0a\x07\x0b\xff\x4c\x00\x10\x06\x31\x1e\x0e\x30\x64\x72")


Advertisement(data=b"\x12\x16\x1a\x18\x3b\x6d\x2b\x38\xc1\xa4\x94\x07\x31\x0b\x65\x0b\x4f\x4d\x0f")

tx_power=7 >
Advertisement(data=b"\x02\x01\x1a\x02\x0a\x07\x0b\xff\x4c\x00\x10\x06\x30\x1e\x25\x34\x9b\xd0")


Advertisement(data=b"\x07\xff\x4c\x00\x12\x02\x00\x00")


Advertisement(data=b"\x0a\xff\x05\x05\x50\x41\x00\x7c\x0f\xdd\x01\x0c\x09\x42\x4d\x41\x34\x30\x30\x5f\x44\x61\x74\x61")

Traceback (most recent call last):
File "", line 1, in
File "ble_simpletest.py", line 15, in
File "adafruit_ble/__init__.py", line 270, in start_scan
MemoryError: memory allocation failed, allocating 62721 bytes
>>>
```

I also have a test program to receive beacon packets and it has the same issue. I have implemented a try/except workaround, but is there something else I should be doing?

```
Adafruit CircuitPython 8.0.3 on 2023-02-23; Adafruit Feather ESP32S3 No PSRAM with ESP32S3
>>> import ble_bma400_beacon
scanning
Exception: memory allocation failed, allocating 62721 bytes
scan done


battery: 2.5 x: 0.123047 y: -0.251953 z: 0.917969

Exception: memory allocation failed, allocating 65179 bytes
scan done
Exception: memory allocation failed, allocating 63127 bytes
scan done


battery: 2.5 x: 0.121094 y: -0.25 z: 0.933594

Exception:
scan done
Exception: memory allocation failed, allocating 65179 bytes
scan done
Exception: memory allocation failed, allocating 52853 bytes
scan done
Exception: memory allocation failed, allocating 52148 bytes
scan done
Exception: memory allocation failed, allocating 65371 bytes
scan done


battery: 2.5 x: 0.113281 y: -0.248047 z: 0.923828


battery: 2.5 x: 0.119141 y: -0.257813 z: 0.933594


battery: 2.5 x: 0.103516 y: -0.251953 z: 0.923828

Exception: memory allocation failed, allocating 52853 bytes
scan done
```
using this code
```
# This example scans for BMA400_data advertisements from the IN100 beacon

from adafruit_ble import BLERadio

ble = BLERadio()
print("scanning")
while (True):
try:
for advertisement in ble.start_scan():
addr = advertisement.address
if advertisement.complete_name == "BMA400_Data" :
print(addr, advertisement)
data= advertisement.data_dict[255]
voltage = data[2] * .03125
x_accel = data[4]*256 + data[3]
y_accel = data[6]*256 + data[5]
z_accel = data[8]*256 + data[7]
if x_accel > 2047 :
x_accel -= 4096
if y_accel > 2047 :
y_accel -= 4096
if z_accel > 2047 :
z_accel -= 4096
x_accel = 4* x_accel / 2048
y_accel = 4* y_accel / 2048
z_accel = 4* z_accel / 2048
print("battery: ",voltage," x: ", x_accel," y: ", y_accel," z: ",z_accel)
print()
except Exception as e:
print("Exception: ",e)
ble.stop_scan()
print("scan done")

```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at adafruit_ble/__init__.py line 270 and reproduce the allocation failure with ble_simpletest.py or ble_bma400_beacon on the reported boards and CircuitPython version. Compare repeated scans and the traceback behavior; done means the BLE scan no longer produces the reported memory allocation failures under the demonstrated workloads.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
embedded-iot, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.