Skip to content

Close open files before resetting buses - #11647

Open
shaxntanu wants to merge 2 commits into
adafruit:mainfrom
shaxntanu:codex/sdcard-vm-stop-close
Open

shaxntanu wants to merge 2 commits into
adafruit:mainfrom
shaxntanu:codex/sdcard-vm-stop-close

Conversation

@shaxntanu

@shaxntanu shaxntanu commented Oct 21, 2026 •

Copy link
Copy Markdown

Summary

  • Add a shutdown-only first finaliser pass for VFS file wrappers and FAT directory iterators, so they close while their backing storage and buses are available.
  • Keep normal garbage collection and all remaining finalisers in their existing order; the shutdown ordering does not depend on heap addresses.
  • Extend the manual SD-card soft-reload test to free and reuse heap blocks below the SDCard before opening a file.

Fixes #11562

Testing

  • Build CI: all 690 jobs completed; 689 passed and the docs job was skipped.
  • Unix test jobs: passed.
  • Zephyr simulation jobs: passed, including native_sim.
  • Pre-commit and Read the Docs: passed.
  • The SD-card reload scenario has not been run on physical hardware.

Run heap finalisers in reverse order during VM shutdown so VFS file handles are closed before their block devices and buses. Add a manual SD-card soft-reload regression test.

Fixes adafruit#11562

Signed-off-by: Shantanu <i.shantanu2807@gmail.com>

@dhalbert dhalbert left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDIT: my previous comment wasn't quite right, but the principle stands.

The finaliser table is a bitmap indexed by heap block. So the reverse sweep only works if the SDCardobject is lower in in the heap than the file object. But a gc could free blocks below the SDCard and then a file object could be lower in the heap.

Signed-off-by: Shantanu <i.shantanu2807@gmail.com>
@shaxntanu

shaxntanu commented Oct 22, 2026 •

Copy link
Copy Markdown
Author

Thanks for catching this. I removed the reverse bitmap sweep. Shutdown now has an explicit first pass for finalizer types that must close before their backing resources; FAT, LittleFS, POSIX file wrappers, and the FAT directory iterator finalizer are marked for that pass. Other finalizers stay on the normal teardown path.

I also updated the manual regression test to allocate and free heap space below the SDCard before opening the file, so it exercises the lower-address reuse case you described. The earlier broad pre-reset finalizer pass caused Zephyr PWM trace failures; this narrower pass avoids those. The updated Build CI matrix has finished: all 690 jobs completed (689 passed; docs skipped). Unix, Zephyr simulation (including native_sim), pre-commit, and Read the Docs checks are green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unflushed writes to an open SD card file are lost when the VM stops

2 participants