Skip to content

Write the filesystem state atomically - #9

Open
janbjorge wants to merge 2 commits into
claude/code-review-bugs-vokwlk-06-validate-batch-sizefrom
claude/code-review-bugs-vokwlk-07-filesystem-atomic-write
Open

janbjorge wants to merge 2 commits into
claude/code-review-bugs-vokwlk-06-validate-batch-sizefrom
claude/code-review-bugs-vokwlk-07-filesystem-atomic-write

Conversation

@janbjorge

@janbjorge janbjorge commented Sep 28, 2026 •

Copy link
Copy Markdown
Owner

Stack 7/9, base #8.

The state file was truncated then rewritten in place, so a crash or short write lost the queue. Now writes a temp file, fsyncs, and os.replace()s it; writers lock a sidecar <path>.lock. Old and new writers must not share a file.

Tests: test_short_write_leaves_previous_content_intact, test_failed_fsync_keeps_old_content_and_cleans_up fail on base; test_concurrent_writers_same_etag_exactly_one_wins, test_successful_write_leaves_no_temp_files pass on both.

_sync_write truncated the state file in place and then wrote the new
content with a single unchecked os.write and no fsync. A crash, a full
disk or a short write between the truncate and the write left an empty
or partially written file: the queue state was lost or undecodable, and
a short write was even reported as success.

Write to a temp file in the same directory, fsync it, os.replace() it
over the state file, then fsync the directory. On any failure the temp
file is removed and the previous state is untouched. Because the data
file's inode now changes on every write, writers serialise on a sidecar
<path>.lock file instead of on the data file, and reads need no lock.

Old and new writers lock different files, so they must not run against
the same state file at the same time.

This branch has not been deployed

No deployments
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.

1 participant