Skip to content

fix: use atomic write-then-rename for state files#262

Open
tylerbutler wants to merge 2 commits intomainfrom
fix/237-atomic-state-writes
Open

fix: use atomic write-then-rename for state files#262
tylerbutler wants to merge 2 commits intomainfrom
fix/237-atomic-state-writes

Conversation

@tylerbutler
Copy link
Copy Markdown
Owner

Summary

  • Replace non-atomic fs::write calls in state file operations with a write-then-rename pattern using tempfile::NamedTempFile
  • Add atomic_write helper function that writes to a temporary file in the same directory then atomically renames it into place
  • Promote tempfile from dev-dependency to regular dependency

Closes #237

Details

Three state file write locations were vulnerable to corruption if the process was interrupted mid-write (fs::write truncates then writes):

  1. save_overlay_state - in-repo overlay state files
  2. save_external_state - external backup state files
  3. remove_external_state - marking overlays as removed

The .target_path marker file write in save_external_state is left as fs::write since it's a non-critical debug marker.

Test plan

  • All 155 existing tests pass
  • Formatting check passes
  • No new clippy warnings introduced

State file writes using fs::write are non-atomic (truncate then write),
risking corruption if the process is interrupted mid-write. Replace with
a write-then-rename pattern using tempfile::NamedTempFile, which writes
to a temporary file in the same directory then atomically renames it
into place.

Closes #237
🤖 Generated with [Nori](https://noriagentic.com)

Co-Authored-By: Nori <contact@tilework.tech>
@github-actions
Copy link
Copy Markdown
Contributor

Missing Changelog Entry

This PR includes commits with types that typically require a changelog entry (fix), but no changie fragment was found.

To add one, run:

changie new

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 11, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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.

bug: state file writes are non-atomic, risking corruption on interrupted writes

1 participant