Skip to content

Commit 92ba5c2

Browse files
committed
Unify VM cleanup code
Before we were skipping reset of the standard busses after boot and repl which could lead to a crash in a subsequent VM run. Fixes adafruit#1806
1 parent 49c4c1e commit 92ba5c2

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

main.c

+19-26
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,22 @@ bool maybe_run_list(const char ** filenames, pyexec_result_t* exec_result) {
176176
return true;
177177
}
178178

179+
void cleanup_after_vm(supervisor_allocation* heap) {
180+
// Turn off the display and flush the fileystem before the heap disappears.
181+
#if CIRCUITPY_DISPLAYIO
182+
reset_displays();
183+
#endif
184+
filesystem_flush();
185+
stop_mp();
186+
free_memory(heap);
187+
supervisor_move_memory();
188+
189+
reset_port();
190+
reset_board_busses();
191+
reset_board();
192+
reset_status_led();
193+
}
194+
179195
bool run_code_py(safe_mode_t safe_mode) {
180196
bool serial_connected_at_start = serial_connected();
181197
#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS
@@ -219,19 +235,7 @@ bool run_code_py(safe_mode_t safe_mode) {
219235
serial_write_compressed(translate("WARNING: Your code filename has two extensions\n"));
220236
}
221237
}
222-
// Turn off the display and flush the fileystem before the heap disappears.
223-
#if CIRCUITPY_DISPLAYIO
224-
reset_displays();
225-
#endif
226-
filesystem_flush();
227-
stop_mp();
228-
free_memory(heap);
229-
supervisor_move_memory();
230-
231-
reset_port();
232-
reset_board_busses();
233-
reset_board();
234-
reset_status_led();
238+
cleanup_after_vm(heap);
235239

236240
if (result.return_code & PYEXEC_FORCED_EXIT) {
237241
return reload_requested;
@@ -359,13 +363,7 @@ void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
359363
boot_output_file = NULL;
360364
#endif
361365

362-
// Reset to remove any state that boot.py setup. It should only be used to
363-
// change internal state that's not in the heap.
364-
reset_port();
365-
reset_board();
366-
stop_mp();
367-
free_memory(heap);
368-
supervisor_move_memory();
366+
cleanup_after_vm(heap);
369367
}
370368
}
371369

@@ -382,12 +380,7 @@ int run_repl(void) {
382380
} else {
383381
exit_code = pyexec_friendly_repl();
384382
}
385-
filesystem_flush();
386-
reset_port();
387-
reset_board();
388-
stop_mp();
389-
free_memory(heap);
390-
supervisor_move_memory();
383+
cleanup_after_vm(heap);
391384
autoreload_resume();
392385
return exit_code;
393386
}

0 commit comments

Comments
 (0)