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 #1806
This commit is contained in:
Scott Shawcroft 2019-04-17 14:52:00 -07:00
parent 49c4c1e2ac
commit 92ba5c26b0
No known key found for this signature in database
GPG Key ID: FD0EDC4B6C53CA59
1 changed files with 19 additions and 26 deletions

45
main.c
View File

@ -176,6 +176,22 @@ bool maybe_run_list(const char ** filenames, pyexec_result_t* exec_result) {
return true; return true;
} }
void cleanup_after_vm(supervisor_allocation* heap) {
// Turn off the display and flush the fileystem before the heap disappears.
#if CIRCUITPY_DISPLAYIO
reset_displays();
#endif
filesystem_flush();
stop_mp();
free_memory(heap);
supervisor_move_memory();
reset_port();
reset_board_busses();
reset_board();
reset_status_led();
}
bool run_code_py(safe_mode_t safe_mode) { bool run_code_py(safe_mode_t safe_mode) {
bool serial_connected_at_start = serial_connected(); bool serial_connected_at_start = serial_connected();
#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS #ifdef CIRCUITPY_AUTORELOAD_DELAY_MS
@ -219,19 +235,7 @@ bool run_code_py(safe_mode_t safe_mode) {
serial_write_compressed(translate("WARNING: Your code filename has two extensions\n")); serial_write_compressed(translate("WARNING: Your code filename has two extensions\n"));
} }
} }
// Turn off the display and flush the fileystem before the heap disappears. cleanup_after_vm(heap);
#if CIRCUITPY_DISPLAYIO
reset_displays();
#endif
filesystem_flush();
stop_mp();
free_memory(heap);
supervisor_move_memory();
reset_port();
reset_board_busses();
reset_board();
reset_status_led();
if (result.return_code & PYEXEC_FORCED_EXIT) { if (result.return_code & PYEXEC_FORCED_EXIT) {
return reload_requested; return reload_requested;
@ -359,13 +363,7 @@ void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
boot_output_file = NULL; boot_output_file = NULL;
#endif #endif
// Reset to remove any state that boot.py setup. It should only be used to cleanup_after_vm(heap);
// change internal state that's not in the heap.
reset_port();
reset_board();
stop_mp();
free_memory(heap);
supervisor_move_memory();
} }
} }
@ -382,12 +380,7 @@ int run_repl(void) {
} else { } else {
exit_code = pyexec_friendly_repl(); exit_code = pyexec_friendly_repl();
} }
filesystem_flush(); cleanup_after_vm(heap);
reset_port();
reset_board();
stop_mp();
free_memory(heap);
supervisor_move_memory();
autoreload_resume(); autoreload_resume();
return exit_code; return exit_code;
} }