@@ -124,7 +124,6 @@ static void reset_devices(void) {
124124}
125125
126126STATIC void start_mp (supervisor_allocation * heap , bool first_run ) {
127- autoreload_reset ();
128127 supervisor_workflow_reset ();
129128
130129 // Stack limit should be less than real stack size, so we have a chance
@@ -336,7 +335,13 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
336335 // Collects stickiness bits that apply in the current situation.
337336 uint8_t next_code_stickiness_situation = SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET ;
338337
338+ // Do the filesystem flush check before reload in case another write comes
339+ // in while we're doing the flush.
339340 if (safe_mode == NO_SAFE_MODE ) {
341+ stack_resize ();
342+ filesystem_flush ();
343+ }
344+ if (safe_mode == NO_SAFE_MODE && !autoreload_pending ()) {
340345 static const char * const supported_filenames [] = STRING_LIST (
341346 "code.txt" , "code.py" , "main.py" , "main.txt" );
342347 #if CIRCUITPY_FULL_BUILD
@@ -345,8 +350,6 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
345350 "main.txt.py" , "main.py.txt" , "main.txt.txt" ,"main.py.py" );
346351 #endif
347352
348- stack_resize ();
349- filesystem_flush ();
350353 supervisor_allocation * heap = allocate_remaining_memory ();
351354
352355 // Prepare the VM state. Includes an alarm check/reset for sleep.
@@ -390,22 +393,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
390393 // Print done before resetting everything so that we get the message over
391394 // BLE before it is reset and we have a delay before reconnect.
392395 if ((result .return_code & PYEXEC_RELOAD ) && supervisor_get_run_reason () == RUN_REASON_AUTO_RELOAD ) {
393- serial_write_compressed (translate ("\nCode stopped by auto-reload.\n" ));
394-
395- // Wait for autoreload interval before reloading
396- uint64_t start_ticks = 0 ;
397- do {
398- // Start waiting, or restart interval if another reload request was initiated
399- // while we were waiting.
400- if (reload_requested ) {
401- reload_requested = false;
402- start_ticks = supervisor_ticks_ms64 ();
403- }
404- RUN_BACKGROUND_TASKS ;
405- } while (supervisor_ticks_ms64 () - start_ticks < CIRCUITPY_AUTORELOAD_DELAY_MS );
406-
407- // Restore request for use below.
408- reload_requested = true;
396+ serial_write_compressed (translate ("\nCode stopped by auto-reload. Reloading soon.\n" ));
409397 } else {
410398 serial_write_compressed (translate ("\nCode done running.\n" ));
411399 }
@@ -425,8 +413,6 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
425413
426414 if (result .return_code & PYEXEC_RELOAD ) {
427415 next_code_stickiness_situation |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_RELOAD ;
428- skip_repl = true;
429- skip_wait = true;
430416 } else if (result .return_code == 0 ) {
431417 next_code_stickiness_situation |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_SUCCESS ;
432418 if (next_code_options & SUPERVISOR_NEXT_CODE_OPT_RELOAD_ON_SUCCESS ) {
@@ -484,22 +470,27 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
484470 size_t total_time = blink_time + LED_SLEEP_TIME_MS ;
485471 #endif
486472
473+ // This loop is waits after code completes. It waits for fake sleeps to
474+ // finish, user input or autoreloads.
487475 #if CIRCUITPY_ALARM
488476 bool fake_sleeping = false;
489477 #endif
490478 while (!skip_wait ) {
491479 RUN_BACKGROUND_TASKS ;
492480
493481 // If a reload was requested by the supervisor or autoreload, return.
494- if (reload_requested ) {
482+ if (autoreload_ready () ) {
495483 next_code_stickiness_situation |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_RELOAD ;
496484 // Should the STICKY_ON_SUCCESS and STICKY_ON_ERROR bits be cleared in
497485 // next_code_stickiness_situation? I can see arguments either way, but I'm deciding
498486 // "no" for now, mainly because it's a bit less code. At this point, we have both a
499487 // success or error and a reload, so let's have both of the respective options take
500488 // effect (in OR combination).
501- reload_requested = false;
502489 skip_repl = true;
490+ // We're kicking off the autoreload process so reset now. If any
491+ // other reloads trigger after this, then we'll want another wait
492+ // period.
493+ autoreload_reset ();
503494 break ;
504495 }
505496
@@ -526,7 +517,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
526517 #endif
527518
528519 // If messages haven't been printed yet, print them
529- if (!printed_press_any_key && serial_connected ()) {
520+ if (!printed_press_any_key && serial_connected () && ! autoreload_pending () ) {
530521 if (!serial_connected_at_start ) {
531522 print_code_py_status_message (safe_mode );
532523 }
0 commit comments