Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Observing crashes the City Hub #1278

Closed
laurensvalk opened this issue Nov 6, 2023 · 4 comments
Closed

[Bug] Observing crashes the City Hub #1278

laurensvalk opened this issue Nov 6, 2023 · 4 comments
Assignees
Labels
bug Something isn't working hub: cityhub Issues related to the LEGO Powered Up Smart hub (Hub No. 4) software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) topic: bluetooth Issues involving bluetooth

Comments

@laurensvalk
Copy link
Member

laurensvalk commented Nov 6, 2023

Describe the bug
After just a few minutes, the City Hub resets itself (consistently) on receiving data:

Send with Prime Hub:

from pybricks.hubs import PrimeHub
from pybricks.pupdevices import ColorLightMatrix
from pybricks.parameters import Color, Port
from pybricks.tools import wait

hub = PrimeHub(broadcast_channel=12)

EXTRA_DATA = "extra bytes to send"

while True:
    for i in range(100):
        # hub.ble.broadcast(i)
        if i % 2:
            hub.ble.broadcast(("red", EXTRA_DATA))
            hub.light.on(Color.RED)
        else:
            hub.ble.broadcast(("green", EXTRA_DATA))
            hub.light.on(Color.GREEN)
        wait(250)

Receive on City Hub:

from pybricks.hubs import CityHub
from pybricks.parameters import Color
from pybricks.tools import wait

hub = CityHub(observe_channels=[12])

while True:
    data = hub.ble.observe(12)
    print(data)
    if data is None:
        # bad state
        hub.light.on(Color.VIOLET)
    else:
        if data[0] == "red":
            hub.light.on(Color.RED)
        else:
            hub.light.on(Color.GREEN)
    wait(10)

Both hubs disconnected from PC.

@laurensvalk laurensvalk added the triage Issues that have not been triaged yet label Nov 6, 2023
@laurensvalk
Copy link
Member Author

laurensvalk commented Nov 6, 2023

Reducing the amount of data seems to delay prevent the crash:

from pybricks.hubs import PrimeHub
from pybricks.pupdevices import ColorLightMatrix
from pybricks.parameters import Color, Port
from pybricks.tools import wait

hub = PrimeHub(broadcast_channel=12)

while True:
    for i in range(100):
        hub.ble.broadcast("red" if i % 2 else "green")
        hub.light.on(Color.RED if i % 2 else Color.GREEN)
        wait(250)
from pybricks.hubs import CityHub
from pybricks.parameters import Color
from pybricks.tools import wait

hub = CityHub(observe_channels=[12])

while True:
    data = hub.ble.observe(12)
    if data is None:
        # bad state
        hub.light.on(Color.VIOLET)
    else:
        if data == "red":
            hub.light.on(Color.RED)
        else:
            hub.light.on(Color.GREEN)
    wait(10)

@laurensvalk laurensvalk added bug Something isn't working hub: cityhub Issues related to the LEGO Powered Up Smart hub (Hub No. 4) topic: bluetooth Issues involving bluetooth software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) and removed triage Issues that have not been triaged yet labels Nov 6, 2023
@dlech
Copy link
Member

dlech commented Nov 12, 2023

issue-1278-crash.zip

Message capture shows that the hub is sending GAP_DEVICE_DISCOVERY_CANCEL which means the MicroPython program has crashed and called pb_type_BLE_cleanup(). So, I don't think the hub is actually crashing, only the MicroPython program. So maybe a memory fragmentation problem?

@dlech
Copy link
Member

dlech commented Nov 12, 2023

If we make the first string the same size (i.e. change "green" to "grn" so it is the same size as "red") it appears to avoid the crash as well. This leads me to believe that the data buffer is somehow getting updated while we are iterating on it somewhere or something like that which causes an exception to be raised due to bad data.

@laurensvalk
Copy link
Member Author

Awesome, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working hub: cityhub Issues related to the LEGO Powered Up Smart hub (Hub No. 4) software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) topic: bluetooth Issues involving bluetooth
Projects
None yet
Development

No branches or pull requests

2 participants