-
-
Notifications
You must be signed in to change notification settings - Fork 7
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] print() combined with UART writes can freeze hub #304
Comments
However, this works fine. from pybricks.pupdevices import UltrasonicSensor
from pybricks.parameters import Port
from urandom import randint
lights = UltrasonicSensor(Port.C).lights
while True:
lights.on([randint(0, 100) for j in range(4)])
But this freezes. It appears that combining fast prints with fast sensor writes is an issue. from pybricks.pupdevices import UltrasonicSensor
from pybricks.parameters import Port
from urandom import randint
lights = UltrasonicSensor(Port.C).lights
while True:
lights.on([randint(0, 100) for j in range(4)])
print("a") # <------- this is only addition. Now it freezes.
|
I think I reproduced this with a CityHub with an older firmware (before When running it like this, the hub becomes unresponsive after an inconsistent time of running (not every print freezes the hub). Decreasing the frequency of the "speed measurement" requests as well as increasing the delay of the main loop in the program seems to decrease the chance of the crash. So this would support the theory that fast prints and sensor readouts are the problem. Because I did this with an older version of the firmware and I have modified my scripts since then, I am not attaching any specific code here. It seems the above example does represent my case pretty well. |
Have seen freezes in a loop without sensor and only doing randints [EDIT] but it had prints in the loop. from pybricks.pupdevices import UltrasonicSensor
from pybricks.parameters import Port
# from urandom import randint
lights = UltrasonicSensor(Port.C).lights
while True:
# lights.on([randint(0, 100) for j in range(4)])
lights.on([60, 7, 57, 25]) No print in here. |
Does that always happen? That script without prints does not freeze for me. |
It did on here. |
Did not test for always, but the first four attempts froze within about seconds (including program load). program: from pybricks.pupdevices import UltrasonicSensor
from pybricks.parameters import Port
# from urandom import randint
lights = UltrasonicSensor(Port.C).lights
while True:
# lights.on([randint(0, 100) for j in range(4)])
lights.on([60, 7, 57, 25]) versions
|
Same Spike setup (as in previous comment) tested on Linux Mint 20.1 All 4 tests froze up. |
It's possible there is some variation between sensors. I'd have to try another to see if I can reproduce it. I also tried both the dual-boot and single-boot just to be sure, but that makes no difference, as expected. |
Good idea, I do have two of them with different codes. The Spike one (if I kept them correctly boxed) is coded 424N9 and the Mindstorms one 316N0 |
The 424N9 definitely reacts differently. lights.on([60, 7, 57, 25]) does NOT freeze (well not in a few minutes). But if the loop only contains: lights.on([randint(0, 100) for j in range(4)]) It freezes the Spike prime hub in 5 to 10 seconds. Strange . . . . Is there a way to report the sensor characteristics in python? Or should we be looking at the random function randint? |
We don’t currently expose the device hardware and firmware version, but we could add it to the PUPDevice.info() dictionary in a future release. If we get to a point where we need this for this release, we can give you a debug build which just prints all that information when you plug it in. I think I’ll try to find a sensor that has this issue. Thanks for the heads up 👍 |
So these might be two different issues:
Case (1) seems to be the most interesting for now: Can we get Case (2) may be less critical for now. We can just delete the light feature from the v3.0 documentation (just like we did with the IMU) and bring it back for v3.1 when we've investigated it properly. |
Since I don't have an ultrasonic sensor, I tried something similar to the above examples with my ColorDistanceSensor yesterday, but I couldn't reproduce the problem. Maybe someone can test if the Ultrasonic Sensor also has the problem with the CityHub or TechnicHub? |
Were you using the internal IMU sensor, by any chance? That has known issues too. |
No, I was using the CityHub when I encountered the problem (which doesn't have the IMU, please definitely correct me if I'm wrong 😅 ) |
@laurensvalk
Sometimes the printing stops and the inventorhub display animation goes on. Would it be better to create another issue for that? |
No worries @BertLindeman, we appreciate all your findings. We can split it into different issues once we find out what's wrong 😄 |
Oke @laurensvalk Thank you for your fast responses. One program fit for technichub, movehub, and inventor hub. from pybricks import version
print(version)
hw_type = version[0]
if str(hw_type) == "technichub":
from pybricks.hubs import TechnicHub
hub = TechnicHub()
elif hw_type == "movehub":
from pybricks.hubs import MoveHub
hub = MoveHub()
elif hw_type == "primehub":
from pybricks.hubs import InventorHub
hub = InventorHub()
from pybricks.parameters import Color
from pybricks.tools import wait
# if not hw_type == "movehub":
# from urandom import randint
counter = 0
print("")
while True:
counter += 1
print("Start iteration", counter, end="")
for i in range(50):
hub.light.off()
# x = [randint(0, 100) for j in range(3)]
x = [counter % 90, counter % 111, counter % 99]
hub.light.on(Color(h=x[0], s=x[1], v=x[2]))
print(".", end="")
print("; ended") Technichub results
Second try:
This hub has no display, so cannot comment on that. Inventorhub results
Recovery: movehub results
As we saw on the inventor hub the drop of the Bluetooth connection "revived" the hub General remark |
I extended the above example for the CityHub and reproduce the same behaviour as reported for the TechnicHub. Once the hub freezes, the LED stays at it's color. When pressing the hub button it resets to continuous Blue, but pressing it again doesn't seem to start any program. (Which usually would start the default hello world program I think) |
For TechnicHub/CityHub, we've narrowed down a Bluetooth issue that is unrelated to UART and fits this description that is not present on PrimeHub (haven't tested MoveHub yet). We are tracking it in #306. |
Attempting to prove this issue is not yet fixed like #306 just to be sure. The firmware flashed by install.pybricks.com at the moment (still) is:
Tried to install firmware from build#1006 via the LEGO Mindstorms app on win10. So waiting for install.pybricks.com to provide a newer firmware level. |
Thanks Bert --- indeed, we expect that this issue still exists. It happens on all hubs at the moment. #306 was something specific to Technic Hub and City Hub. |
I'm not able to reproduce on Technic hub since #306 is fixed (I've only tried one sensor so far though). |
It does seem to happen still on Prime Hub. I also just saw the same with the Color Sensor. And this seems to be a total freeze, including the stop button. |
Hi guys, So it is now running over 7 hours without problem.
|
I have reproduced this on the City hub. However, since the city hub is the only hub that currently has the watchdog timer enabled, it will reboot instead of freezing. As a first step, we should be able to enable the watchdog time on other hubs so we at least won't have to pull the batteries. |
This might just be my imagination or a concidence, but it seems like the crash (on the City hub at least) happens sooner when the battery voltage is lower. UPDATE: Probably my imagination. Still crashing with fully charged batteries. |
Since just now (not in any released firmware yet) all Powered Up hubs have the watchdog timer enabled, so we can now do this to test if the most recent reboot was due to a crash: if hub.reset_reason() == 2:
print('reset due to watchdog timeout') (numeric values come from here) |
Problem also reproduces on Move hub. |
Well... I think I accidentally fix this. While I was looking at code that could possibly be related, I did pybricks/pybricks-micropython@7d14182 just to clean things up a bit - but it seems to have fixed the issue. I've run City hub for about 30 minutes (up to a count of 6000) without hanging. I can kind of see how this could make a difference. The hang was probably caused by somehow getting into an infinite IRQ handler loop (interrupt isn't cleared, so handler is called again as soon as it exits). |
Move hub (which shares the same UART driver as City hub) got up to 3378 before locking up. However, this was not the total lock/watchdog reboot as seen before, but rather just the Bluetooth locked up like #306. |
It appears that reading the USART SR register clears some flags. In certain cases, this would cause the interrupt handler to go into an infinite loop. By reading the SR register only once, we ensure that all flags are seen and handled properly. Issue: pybricks/support#304
Applying the same fix to Prime hub got it to run much longer, but I still hit the watchdog reset after 2488 counts. |
Recapping as I understand it:
|
In cases where UART is involved, could there be some race condition with uart writes, to set values and to send a keep-alive message at nearly the same time? |
If we are getting a watchdog reset, then we are getting into an infinite loop somewhere. Most likely, the interrupt handler.
|
Just to confirm, I'll add that this still seems to be the case. The Color Light Matrix has problems on the Prime Hun and the Essential Hub, but I've not seen them on Technic Hub yet. |
There were a couple of places where we are looping waiting for !PBIO_ERROR_AGAIN without calling the Contiki event loop. When this condition was hit, no events would be processed, resulting in an infinite loop leading to a watchdog timer reset. Issue: pybricks/support#304
Found the incorrect code causing the infinite loop at a slightly higher level. The fact that it only happened reliably while printing on hubs using btstack seems to be a coincidence of the timing of those specific hubs and Pybricks firmware config. |
This freezes the Prime Hub consistently.
Originally posted by @BertLindeman in #297 (comment)
The text was updated successfully, but these errors were encountered: