[Bug] Hub crashes if exception is raised while handling unhandled exception. #2040
Labels
bug
Something isn't working
software: pybricks-micropython
Issues with Pybricks MicroPython firmware (or EV3 runtime)
topic: MicroPython
Issues related to the MicroPython programming language
Describe the bug
The unhandled exception handler does not handle exceptions. We call
print_final_exception()
but the print functions can also raise an exception. Since there is nonlr_push()
to handle any exception, we end up crashing the hub and it reboots (or powers off on hubs that can't keep the power up long enough for a reset).https://github.com/pybricks/pybricks-micropython/blob/master/bricks/_common/micropython.c#L108
To reproduce
The easiest way to reproduce this is to modify
mp_hal_stdout_tx_strn()
to not actually print and and just cause an infinite loop callingMICROPY_EVENT_POLL_HOOK
.Then download and run a program that prints something. (e.g.
print("hi")
)The program will run forever because of the hacked print function. Press the stop button (hub button or Pybricks Code button) to stop the program. The firmware jumps to the function to print the unhandled
SystemExit
but gets stuck again in an infinite loop.Press the stop button again. This time, there is nothing to catch the exception, so we end up in
nlr_jump_fail()
.Expected behavior
This is how upstream MicroPython works, so maybe this is the expected behavior? But from a user point of view, having the hub go unresponsive and reset/power off isn't a great experience.
Solutions considered
nlr_push()
to catch exceptions and silently ignore them during the unhandled exception handler.Note: these are not mutually exclusive options.
The text was updated successfully, but these errors were encountered: