Skip to content

[Bug] Unexpected behavior after KeyboardInterrupt #526

Closed

Description

@laurensvalk

Describe the bug
KeyboardInterrupt can be used to drop into the REPL, which can be useful for debugging.

The problem is that pbsys_user_program_unprepare is being run before entering the REPL. This resets a few things that shouldn't be reset. For example, it changes the stop button back to the Center button, and does a full reset of the motors via pbio_stop_all.

There's a comment that says we are currently doing this because:

        // Need to unprepare, otherwise SystemExit could be raised during print.
        pbsys_user_program_unprepare();
        mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);

Is there maybe a way to achieve this without resetting everything else?

It could still be useful to keep some hook there (just not this one), because it is still nice to stop all motors (just not fully reset them).

To reproduce

The more prominent case is with motors, but the stop button case can be demonstrated without:

from pybricks.hubs import ThisHub
from pybricks.tools import wait

hub = ThisHub()

hub.system.set_stop_button(None)

wait(5000)

raise KeyboardInterrupt

# We drop into the REPL here. Stop button
# should still be none, but isn't.

Activity

  1. added
    platform: Powered Up
    topic: motors
    on Nov 11, 2021
  2. laurensvalk commented on Nov 11, 2021

    @laurensvalk
    MemberAuthor

    This is now solved for motors another way. This actually relies on keeping pbsys_user_program_unprepare the way it is.

    And since the button thing isn't all that critical, I think we can keep this as-is for the upcoming releases.

  3. laurensvalk commented on Nov 30, 2021

    @laurensvalk
    MemberAuthor

    For motors, we now have:

    void pbio_dcmotor_stop_all(bool clear_parents);

    pbio_dcmotor_stop_all(false) will just stop motors but you can continue using them.

    pbio_dcmotor_stop_all(true) stops and resets everything.

    Currently pbio_stop_all calls the latter, as it should.

    But since pbio_stop_all is currently also triggered by user_program_unprepare as explained above, motors can't currently be used after a KeyboardInterrupt.

  4. self-assigned this
    on Aug 23, 2022
  5. laurensvalk commented on Aug 23, 2022

    @laurensvalk
    MemberAuthor

    A fix is in progress as part of an extension to pybricks/pybricks-micropython#109

    To reproduce current issue:

    from pybricks.hubs import PrimeHub
    from pybricks.pupdevices import Motor
    from pybricks.parameters import Button, Direction, Port
    from pybricks.robotics import DriveBase
    from pybricks.tools import wait, StopWatch
    
    hub = PrimeHub()
    hub.system.set_stop_button(Button.BLUETOOTH)
    
    left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
    right_motor = Motor(Port.B)
    drive_base = DriveBase(left_motor, right_motor, wheel_diameter=56, axle_track=112)
    
    left_motor.run(500)
    
    wait(2000)
    
    raise KeyboardInterrupt

    This has three bugs:

    • Button.BLUETOOTH no longer works as stop button after entering REPL
    • drive_base.drive(500, 0) fails with EPERM because pbio was reset before entering REPL
    • When starting resources in REPL, >>> left_motor.run(500), it doesn't stop when clicking stop again.

    This is easy to fix now because main/stm32 no longer runs in an infinite loop with a goto. Control always yields back to pbsys, so we can do most of the hardware stop/reset there.

  6. added theissue type on Aug 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

platform: Powered Uptopic: motors

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions