state: Save syncs the state file before renaming it into place (closes #264) #276

Merged
clawbot merged 1 commits from fix/264-sync-state-save into next 2026-10-13 18:27:31 +02:00
Collaborator

Fixes #264.

Save wrote state.json.tmp with os.WriteFile and renamed it over state.json without syncing anything. After a power loss or kernel crash the rename could reach the disk before the data, leaving an empty or cut-short state.json. Load cannot parse that, and dnswatcher then stops at every start.

Save now opens the temp file itself, writes it, syncs and closes it, renames it into place, then opens and syncs the data directory so the rename is on disk too. An error from any step is returned, as before.

TestSaveOverLeftoverTempFile starts from a temp file longer than the state Save writes, and checks that afterwards the state file parses with the saved domains and no temp file remains. A test cannot observe a sync: that the file and the directory are synced is confirmed by reading Save.

Rule suppressed: gosec's file-path warning on the temp file's open in Save and on the read in the new test, as Load and the existing tests already do.
Judgement call: the README sentence on atomic writes is unchanged; it stays true.

Model: opus-5-5

Fixes https://git.eeqj.de/sneak/dnswatcher/issues/264. `Save` wrote `state.json.tmp` with `os.WriteFile` and renamed it over `state.json` without syncing anything. After a power loss or kernel crash the rename could reach the disk before the data, leaving an empty or cut-short `state.json`. `Load` cannot parse that, and dnswatcher then stops at every start. `Save` now opens the temp file itself, writes it, syncs and closes it, renames it into place, then opens and syncs the data directory so the rename is on disk too. An error from any step is returned, as before. `TestSaveOverLeftoverTempFile` starts from a temp file longer than the state `Save` writes, and checks that afterwards the state file parses with the saved domains and no temp file remains. A test cannot observe a sync: that the file and the directory are synced is confirmed by reading `Save`. Rule suppressed: gosec's file-path warning on the temp file's open in `Save` and on the read in the new test, as `Load` and the existing tests already do. Judgement call: the README sentence on atomic writes is unchanged; it stays true. Model: opus-5-5
clawbot added the needs-review label 2026-10-13 18:07:05 +02:00
clawbot self-assigned this 2026-10-13 18:07:05 +02:00
clawbot added 1 commit 2026-10-13 18:07:05 +02:00
Save wrote state.json.tmp with os.WriteFile and renamed it over
state.json without syncing. After a power loss the rename could reach
the disk before the data, leaving an empty state.json that Load cannot
parse, so every start failed.

Save now writes the temp file through its own handle, syncs and closes
it, renames it, then syncs the data directory so the rename is on disk
too. The new test starts from a leftover temp file longer than the
state; a test cannot observe the syncs, which are confirmed by reading.

Rule suppressed: gosec's file-path warning on the temp file's open, as
Load already does.

Model: opus-5-5
Author
Collaborator

Review: PASS

Model: opus-5-5

Review: PASS Model: opus-5-5
clawbot removed the needs-review label 2026-10-13 18:20:55 +02:00
clawbot merged commit 5617c92fb3 into next 2026-10-13 18:27:31 +02:00
clawbot deleted branch fix/264-sync-state-save 2026-10-13 18:27:32 +02:00
Sign in to join this conversation.