Skip to content

Commit 1d7bf72

Browse files
committed
bricks/_common/micropython: Flush stdout on completion.
Ensures we don't send the program stopped status before the program is truly done. In turn, this ensures that a new program can't be started before stdout from the last program is one.
1 parent f32dc6b commit 1d7bf72

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

bricks/_common/micropython.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,10 @@ void pbsys_main_run_program(pbsys_main_program_t *program) {
406406
run_user_program();
407407
break;
408408
}
409+
410+
// Ensure everything is written before the user application is considered
411+
// done, so that the host does not receive stdout after receiving stop.
412+
mp_hal_stdout_tx_flush();
409413
}
410414

411415
void pbsys_main_run_program_cleanup(void) {

bricks/_common/mphalport.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
7171
return len - remaining;
7272
}
7373

74+
// May raise, so long print can be stopped.
7475
MICROPY_EVENT_POLL_HOOK
7576
}
7677

@@ -79,6 +80,7 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
7980

8081
void mp_hal_stdout_tx_flush(void) {
8182
while (!pbsys_host_tx_is_idle()) {
82-
MICROPY_EVENT_POLL_HOOK
83+
// Don't raise, just wait for data to clear.
84+
MICROPY_VM_HOOK_LOOP;
8385
}
8486
}

0 commit comments

Comments
 (0)