Use unique temp filenames for atomic state writes - #188
Merged
Conversation
The shared `.json.tmp` path meant concurrent status writes for the same pane could clobber each other's temp file before the atomic rename. Give each writer a unique temp filename (pid + nanos) so only the rename is shared, and relax the test assertion to catch any leftover `.tmp` suffix.
The state write fix needs a temp file strategy that is unique across concurrent writers and consistent across every atomic write path. The pid and clock based name still allows same-process collisions on systems with coarse clock resolution, and each copied helper needs the same cleanup behavior. This adds a shared write_atomic helper backed by tempfile so temp names are created exclusively in the target directory. The helper removes stale temp files matching its target-specific prefix, writes and flushes the content, and persists the temp file over the target. Agent state, Codex status, run result, and GitHub PR cache writes all use the shared helper. The focused tests cover target replacement, temp cleanup after successful writes, and preservation of fresh temp files.
Owner
|
Thank you |
raine
added a commit
that referenced
this pull request
Jul 2, 2026
* Use unique temp filenames for atomic state writes The shared `.json.tmp` path meant concurrent status writes for the same pane could clobber each other's temp file before the atomic rename. Give each writer a unique temp filename (pid + nanos) so only the rename is shared, and relax the test assertion to catch any leftover `.tmp` suffix. * use shared atomic write helper The state write fix needs a temp file strategy that is unique across concurrent writers and consistent across every atomic write path. The pid and clock based name still allows same-process collisions on systems with coarse clock resolution, and each copied helper needs the same cleanup behavior. This adds a shared write_atomic helper backed by tempfile so temp names are created exclusively in the target directory. The helper removes stale temp files matching its target-specific prefix, writes and flushes the content, and persists the temp file over the target. Agent state, Codex status, run result, and GitHub PR cache writes all use the shared helper. The focused tests cover target replacement, temp cleanup after successful writes, and preservation of fresh temp files. --------- Co-authored-by: aserper <aserper@users.noreply.github.com> Co-authored-by: Raine Virta <raine.virta@gmail.com>
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.
Summary
pid + nanos) instead of a shared.json.tmpStateStore::write_atomicand the Codex statuswrite_atomic.tmpsuffixWhy
Concurrent status writes for the same pane (e.g. duplicate status extensions firing at once) shared one
.json.tmppath, so one writer could clobber another's temp file before the atomic rename. The rename itself is atomic on the same filesystem; only the temp path needs to be unique per writer.Tests
cargo test --quiet -- --test-threads=1Split out of #184 where it was bundled with unrelated Pi status changes.