feat: atomic file writes via rename-in-place - #333
Draft
troglodyne-bot wants to merge 2 commits into
Draft
Conversation
teodesian
reviewed
Jun 2, 2026
|
|
||
| =cut | ||
|
|
||
| sub write_file_atomic ( $path, $content ) { |
Collaborator
There was a problem hiding this comment.
The problem with this whole pull request is making and then using this method rather than using File::Slurper::Temp::write_binary() from CPAN, which is far better implemented and tested than this.
Collaborator
|
@troglodyne-bot rebase |
Prevents data corruption from partial writes on crash or signal.
Direct open('>') calls are replaced with write_file_atomic() which
writes to a temp file in the same directory then renames atomically.
- Add Trog::Utils::write_file_atomic($path, $content)
- Update Trog::Data::FlatFile::write to use it
- Update Trog::Data::DUMMY::read and write to use it
- Add t/Trog-Utils.t covering basic write, overwrite, no-leftover-temp, bad-dir
Closes Troglodyne-Internet-Widgets#302
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Contributor
Author
Rebase with requested adjustmentsBranch Changes applied
StatsActions performed
CI statusCI will be checked asynchronously. Automated by Kōan |
troglodyne-bot
force-pushed
the
koan/atomic-file-writes
branch
from
June 5, 2026 19:56
26ec45a to
eb3fc9f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Replace direct
open(my $fh, '>', $file)writes with atomic temp-file + rename, so partial writes can't corrupt data on crash or signal.Why
Issue #302: a process killed mid-write leaves a truncated or incomplete file in place.
rename()is a POSIX atomic operation — either the old file survives or the new one is fully in place, never an intermediate state.How
Trog::Utils::write_file_atomic($path, $content)— writes to aFile::Temptemp file in the same directory (same filesystem, so rename is guaranteed atomic), then renames to the target.FlatFile::write: removed theopen/print/closetrio; one call towrite_file_atomic.DUMMY::readandDUMMY::write: same treatment for the JSON datastore initialization and save path.evalcatch block if an error occurs before rename, so no orphan temps on failure.Testing
t/Trog-Utils.t: 5 subtests covering basic write, overwrite, no-leftover-temp-file guarantee, and die-on-bad-directory.PERL5LIB=/opt/domains/koan.troglodyne.net/perl5/lib/perl5 perl -Ilib t/Trog-Utils.t→ 5/5 pass.t/Trog-SQLite.tfailure (errstr mock gap) is unrelated and predates this branch.