Skip to content

Commit ce8a7c9

Browse files
committed
micropython: Stop de-init on forced shutdown.
While MicroPython has a vm abort, several pbio tasks are started after this. If they get stuck, the hub cannot shut down. This completes the unification of the de-init tasks and adds a break for a shutdown request.
1 parent 01b9f23 commit ce8a7c9

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
- Raise a descriptive error when the `Car` class can't find a steering mechanism
1313
end stop within 10 seconds ([support#1564]).
1414

15+
### Fixed
16+
- Fixed hubs not shutting down when holding hub button ([support#1419]).
17+
18+
[support#1419]: https://github.com/pybricks/support/issues/1419
1519
[support#1564]: https://github.com/pybricks/support/issues/1564
1620
[support#1574]: https://github.com/pybricks/support/issues/1574
1721

bricks/_common/micropython.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,8 @@ void pbsys_main_run_program(pbsys_main_program_t *program) {
331331
}
332332
#endif // PYBRICKS_OPT_COMPILER
333333

334-
// Ensure all buffered output has been sent over the air.
335-
mp_hal_stdout_tx_flush();
336-
337-
// Clean up non-MicroPython resources used by the pybricks package.
334+
// De-init bluetooth resources (including flushing stdout) that may use
335+
// memory allocated by MicroPython before we wipe it.
338336
pb_package_pybricks_deinit();
339337

340338
gc_sweep_all();

pybricks/pybricks.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#include <pbdrv/bluetooth.h>
1414
#include <pbio/version.h>
15+
#include <pbsys/bluetooth.h>
16+
#include <pbsys/status.h>
1517

1618
#include <pybricks/common.h>
1719
#include <pybricks/hubs.h>
@@ -146,8 +148,13 @@ void pb_package_pybricks_deinit(void) {
146148
// tasks and deinit tasks have completed.
147149
static pbio_task_t noop_task;
148150
pbdrv_bluetooth_queue_noop(&noop_task);
149-
while (noop_task.status == PBIO_ERROR_AGAIN) {
151+
while (noop_task.status == PBIO_ERROR_AGAIN || !pbsys_bluetooth_tx_is_idle()) {
150152
MICROPY_VM_HOOK_LOOP
153+
154+
// Stop waiting (and potentially blocking) in case of forced shutdown.
155+
if (pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN_REQUEST)) {
156+
break;
157+
}
151158
}
152159
#endif
153160
}

0 commit comments

Comments
 (0)