Skip to content

fix: use process-unique temp names for session saves - #2376

Closed
docxology wants to merge 1 commit into
herdrdev:masterfrom
docxology:fix/session-save-tmp-names
Closed

fix: use process-unique temp names for session saves#2376
docxology wants to merge 1 commit into
herdrdev:masterfrom
docxology:fix/session-save-tmp-names

Conversation

@docxology

Copy link
Copy Markdown

save_json_to_path used target.with_extension("json.tmp") — a fixed name not unique per write. If two processes (e.g. a herdr server and client, or two server instances) save concurrently, they clobber each other's in-progress temp file, and one can rename() a file the other is still writing, producing a truncated/garbled session.json that fails to parse on next load (silent total session loss).

Fix: use a process-unique temp file name incorporating PID, an atomic counter, and nanosecond timestamp: .{file_name}.{pid}.{seq}.{nanos}.tmp. This ensures no two writers collide on the same temp path.

Regression tests: unique_tmp_path_is_unique_per_call and save_to_paths_leaves_no_tmp_files_behind verify uniqueness and cleanup.

cargo fmt --check: clean
cargo test --locked persist: 9 passed, 0 failed

save_to_paths writes session.json and session-history.json as two separate
renames, so a crash between them can leave them out of sync, and the old fixed
.json.tmp suffix collided when another herdr process or a stalled first save
used the same name. Emit a unique temp file beside the target — PID +
per-process counter + high-resolution timestamp — so a live save never
clobbers a concurrent or abandoned tmp, while the rename stays on the same
filesystem and thus atomic.
@kangal-bot kangal-bot added the ai-review Trigger automated AI reviews for pull requests admitted by the PR gate label Aug 5, 2026
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 29231f5d-e54f-4a8b-af7e-0d7200f82f34

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Session persistence now creates unique temporary paths for atomic JSON writes. The paths remain beside the target file and include process and sequence identifiers. Tests verify path properties and cleanup after session and history saves.

Changes

Persistence temporary paths

Layer / File(s) Summary
Unique temporary path generation
src/persist/io.rs
Adds a process-local counter and timestamp-based temporary filename generation beside the target file.
Atomic save integration and validation
src/persist/io.rs
Uses unique temporary paths for atomic writes. Tests verify uniqueness, .tmp suffixes, same-directory placement, and cleanup.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • herdrdev/herdr#2335: Both changes update temporary-file handling in src/persist/io.rs for session and history persistence.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@docxology

Copy link
Copy Markdown
Author

Closing — the maintainer requested no unsolicited fix PRs. These findings are documented in case they're useful for future issue reports.

@docxology docxology closed this Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-review Trigger automated AI reviews for pull requests admitted by the PR gate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants