You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unflushed writes to an open SD card file are lost when the VM stops #11562
When code.py is stopped by auto-reload, or finishes with the file still open:
OSError: with no message is printed after "Code stopped by auto-reload" or "Code done running."
Lines written since the last flush() are lost. A newly created file that was never flushed does not appear on the card at all.
Description
The file is only closed when its finaliser runs in gc_sweep_all(), from gc_deinit() in stop_mp(). By then reset_port() has already reset the SPI bus, and the SDCard object may have been finalised first, since sweep order is arbitrary. The close() in the finaliser fails, and mp_call_function_1_protected() prints the exception.
Split out from #9138, whose hard hang no longer reproduces.
I'd suggest the scoped first pass with the heap unlocked, since it fixes both cases now and doesn't block general ordering later (#11108).
If that works for you, I can open a PR that carries #11647 forward with both changes.
Sorry, I missed needing to reply.
Unlocking the heap sounds dangerous, as described. The point is that we do need some ordering of finalizers. I am not sure what you meant by a "scoped first pass". Do you mean what was in #11647? Let's discuss.
I've dropped the heap unlock. The new pass finds open files with the heap locked, then closes them like user code calling close(), outside any collection.
#12017 has the lab results, with a locked pass alongside for comparison. What do you think?
Activity
mikeysklar commentedon Nov 5, 2026
I tested this on cp-desktop-lab.
#11647 alone doesn't save files when code.py stops.
run_code_py()clears_vm_is_runningbefore cleanup, so sdcardio refuses the bus and the close fails.Keeping that flag set until after
gc_run_finalisers_first()fixes it.Python block devices still fail with MemoryError, because the pass runs with the GC locked.
mikeysklar commentedon Nov 21, 2026
@dhalbert Open SD files lose unflushed writes when code.py stops, because the bus is reset before the file finalizers run.
#11647 plus keeping
_vm_is_runningset until aftergc_run_finalisers_first()fixes this for sdcardio on every stop path I tested (table above).Python block devices still fail, because the first finalizer pass runs with the heap locked.
Unlocking the heap for that pass fixes them too (
adafruit_sdcard, 3/3 in the simulator).The catch: a garbage collection can then run during that pass, so unrelated finalizers may run before the bus reset.
I'd suggest the scoped first pass with the heap unlocked, since it fixes both cases now and doesn't block general ordering later (#11108).
If that works for you, I can open a PR that carries #11647 forward with both changes.
dhalbert commentedon Dec 2, 2026
Sorry, I missed needing to reply.
Unlocking the heap sounds dangerous, as described. The point is that we do need some ordering of finalizers. I am not sure what you meant by a "scoped first pass". Do you mean what was in #11647? Let's discuss.
Note that the camera fix in #11108 is removed in #11963. I am checking about that: #11963 (comment)
mikeysklar commentedon Dec 2, 2026
@dhalbert Yes, by "scoped first pass" I meant what #11647 did. File types get a flag, and only flagged objects are finalized early:
I've dropped the heap unlock. The new pass finds open files with the heap locked, then closes them like user code calling
close(), outside any collection.#12017 has the lab results, with a locked pass alongside for comparison. What do you think?
Close files left open on FAT before the buses reset