Skip to content

Commit f72c3da

Browse files
committed
Treat a reload requested during the wait state the same as one requested during execution regarding preserving next code info.
1 parent ce74fdd commit f72c3da

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

main.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,19 @@ bool cleanup_after_vm(supervisor_allocation* heap) {
281281
return false;
282282
}
283283

284+
void maybe_clear_next_code_info(bool new_next_code) {
285+
// - If a new next code file was set, honor that in any case.
286+
// - Otherwise: If a reload was requested by a USB file write, we want to run the same file
287+
// again, preserve any next-code info. Currently that also covers a reload requested by
288+
// supervisor.reload(), or do we want to treat that differently by turning
289+
// reload_requested into an enum?
290+
// - Otherwise: Clear any leftover next-code info and restart at code.py/main.py.
291+
if (!new_next_code && !reload_requested) {
292+
free_memory(next_code_allocation);
293+
next_code_allocation = NULL;
294+
}
295+
}
296+
284297
bool run_code_py(safe_mode_t safe_mode) {
285298
bool serial_connected_at_start = serial_connected();
286299
#if CIRCUITPY_AUTORELOAD_DELAY_MS > 0
@@ -304,6 +317,7 @@ bool run_code_py(safe_mode_t safe_mode) {
304317
result.exception_line = 0;
305318

306319
bool found_main = false;
320+
bool new_next_code = false;
307321

308322
if (safe_mode != NO_SAFE_MODE) {
309323
serial_write_compressed(translate("Running in safe mode! "));
@@ -342,22 +356,14 @@ bool run_code_py(safe_mode_t safe_mode) {
342356
}
343357
#endif
344358
}
345-
bool new_next_code = cleanup_after_vm(heap);
346-
// - If a new next code file was set, honor that in any case.
347-
// - Otherwise: If a reload was requested by a USB file write, we want to run the same file
348-
// again, preserve any next-code info. Currently that also covers a reload requested by
349-
// supervisor.reload(), or do we want to treat that differently by turning
350-
// reload_requested into an enum?
351-
// - Otherwise: Clear any leftover next-code info and restart at code.py/main.py.
352-
if (!new_next_code && !reload_requested) {
353-
free_memory(next_code_allocation);
354-
next_code_allocation = NULL;
355-
}
359+
new_next_code = cleanup_after_vm(heap);
356360

357361
if (result.return_code == 0 && (next_code_options & SUPERVISOR_NEXT_CODE_OPT_RELOAD_ON_SUCCESS)) {
362+
maybe_clear_next_code_info(new_next_code);
358363
return true;
359364
}
360365
if (result.return_code & PYEXEC_FORCED_EXIT) {
366+
maybe_clear_next_code_info(new_next_code);
361367
return reload_requested;
362368
}
363369
}
@@ -376,11 +382,13 @@ bool run_code_py(safe_mode_t safe_mode) {
376382
while (true) {
377383
RUN_BACKGROUND_TASKS;
378384
if (reload_requested) {
385+
// no need to call maybe_clear_next_code_info(), it would be a no-op
379386
reload_requested = false;
380387
return true;
381388
}
382389

383390
if (serial_connected() && serial_bytes_available()) {
391+
maybe_clear_next_code_info(new_next_code);
384392
// Skip REPL if reload was requested.
385393
return (serial_read() == CHAR_CTRL_D);
386394
}

0 commit comments

Comments
 (0)