Skip to content

fix(op): make the notices temp filename unique per write - #1365

Merged
norrietaylor merged 1 commit into
mainfrom
fix/notices-unique-temp-name
Sep 11, 2026
Merged

norrietaylor merged 1 commit into
mainfrom
fix/notices-unique-temp-name

Conversation

@norrietaylor

@norrietaylor norrietaylor commented Sep 8, 2026 •

Copy link
Copy Markdown
Member

Summary

notices::record publishes <cache>/notices/<source-sha256>.json with a
write-then-rename, but the temp filename was derived from the PID alone:

let tmp = path.with_extension(format!("json.tmp-{}", std::process::id()));

Within a single process the PID is a constant, so two concurrent
SourceLoad::run calls for the same source digest resolve to the same
temp path. The interleaving that bites is:

  1. Writer A creates …json.tmp-<pid> and starts writing.
  2. Writer B truncates and starts writing the same temp path.
  3. A finishes and renames the temp file — which is now B's partial write —
    over the final record.
  4. B's own rename then fails (ENOENT), or, if it lands first, the record
    published is the truncated JSON.

Either outcome is a real failure: a rename error surfacing out of source
extraction, or a malformed attribution record persisted in the cache, where
the write-then-rename existed precisely to guarantee readers never see a
partial record.

The fix makes the temp name unique per write — PID plus a process-local
AtomicU64 counter — so two concurrent writers for the same digest never
share a temp file, and one writer's rename can never move another's
still-in-flight write. The rename itself stays atomic, so a final record
already published by a racing writer remains a fine idempotent outcome
(identical archive → identical tree), matching record's documented
idempotence.

One file changed, crates/op/src/notices.rs: the AtomicU64/Ordering
import, the counter, and the comment explaining why the name is unique.

Why this is a separate PR

The finding is CodeRabbit's, on #1345:
#1345 (comment)

The fix was authored on #1345's branch by mistake — it addresses code
introduced by #1341 (feat(op): capture upstream NOTICE/COPYRIGHT files at source extraction), already merged to main, and has nothing to do with
#1345's subject. #1345 is being rebased to drop that commit, so the fix is
extracted here to keep it from being lost. Nothing on #1345 or its branch is
touched by this PR.

Refs: #1341, #1345

Testing

No local build was run for this change; the PR lanes are the verification.
The edit is confined to crates/op/src/notices.rs and is a
behaviour-preserving change to the temp filename plus the AtomicU64 import
it needs. git diff --stat origin/main shows exactly one changed file.

Checklist

  • Docs updated if behavior changed — n/a, no user-visible behavior change
    (the temp filename is an internal implementation detail)
  • BREAKING CHANGE: footer present if this is a breaking change — n/a

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability when multiple temporary notice records are written concurrently.
    • Prevented concurrent operations from accidentally sharing temporary file paths during publication.

`notices::record` writes the record with write-then-rename, but the temp
name was derived from the PID alone. Two concurrent `SourceLoad::run`
calls for the same source digest in one process therefore shared a temp
path: one writer could rename away the other's still-in-flight write,
producing either a rename failure or a truncated, malformed JSON record.

Add a process-local `AtomicU64` counter alongside the PID so every write
gets its own temp file. The rename stays atomic, so a record already
published by a racing writer remains a fine idempotent outcome — the
same archive always yields the same tree.

Reported by CodeRabbit on #1345:
#1345 (comment)

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@norrietaylor
norrietaylor requested a review from a team as a code owner September 8, 2026 22:05
@coderabbitai

coderabbitai Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Essentials

Run ID: 7e1a8b3b-ca82-4b1c-9872-33f10d67b94e

📥 Commits

Reviewing files that changed from the base of the PR and between a30ac97 and 1d0e993.

📒 Files selected for processing (1)
  • crates/op/src/notices.rs

Included review availability: 4 reviews are currently available. Your included PR review attempts over the past 7 days set your current allowance at 5 reviews per hour.


📝 Walkthrough

Walkthrough

Notice record temporary filenames now combine the process ID with an atomic per-process sequence number. This prevents concurrent writers from sharing temporary paths while preserving write-then-rename publication.

Changes

Notice filename uniqueness

Layer / File(s) Summary
Atomic temporary filename sequencing
crates/op/src/notices.rs
Adds atomic counter imports and includes a per-process sequence number in temporary notice-record filenames.

Priority: ⬇️ Low

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

Merge Risk: ⚪ Minimal · up to 1d0e9

Temporary notice-record paths now receive a per-process sequence suffix, preventing concurrent writes from sharing a temporary file while retaining atomic rename publication. No current merge-blocking risk is identified.

Suggested reviewers: bryan-minimal

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title uses the required Conventional Commit format and clearly states the main change: making notice temporary filenames unique per write.
Description check ✅ Passed The description includes complete Summary, Testing, and Checklist sections. It explains the concurrency defect, the AtomicU64-based fix, the scope, and the testing limitation.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/notices-unique-temp-name

A rabbit counts each fleeting file
With tiny paws in steady style
Process paths now stay apart
Atomic numbers guard each start
Rename hops, and writes are bright

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

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minimal-review — advisory review

Verdict COMMENT — COMMENT: policy.actions.request_changes is off; policy.actions.approve is off.

Scope: matches the PR body exactly — one file, crates/op/src/notices.rs, adding an AtomicU64 import and a per-call sequence number folded into the temp filename. Clobber: none; this only extends the fix from #1341, nothing reverted. Correctness: the fix closes the race it describes — the old temp name (json.tmp-<pid>) was constant within a process, so two concurrent record() calls for the same source_sha256 (plausible via sources.rs's source-extraction path, which has no per-digest serialization) could share a temp file and let one writer's rename clobber another's in-flight write. Combining the existing PID with a function-local, Ordering::Relaxed AtomicU64 counter makes the temp path unique per call both within and across processes, and the final rename stays atomic, so a racing writer's already-published record is still a valid idempotent outcome per record's documented contract. Confirmed no code globs the notices directory, so stray/renamed temp names can't leak into any listing. Nothing else to flag; this is a clean, minimal fix for exactly the bug it claims to fix.

What ran

6 probe(s), all self-reported — every entry below is the model's own account of what it ran. None of it is host-attested, so none of it can gate a blocking verdict.

Verification log

1. Read full crates/op/src/notices.rs to see record() in context with the tests — self-reported

record() early-returns if the record path already exists, then computes json, writes to a temp path, and renames onto the final path. Before this diff the temp path was <path>.json.tmp-<pid>, constant within a process, so two concurrent record() calls for the same source_sha256 in the same process would share a temp file. The diff adds a function-local static TMP_SEQ: AtomicU64 and appends fetch_add(1, Relaxed) to the temp name, making it unique per call. This directly closes the race described in the PR body.

2. rg for notices::record call sites to confirm concurrent same-digest calls are plausible — self-reported

crates/op/src/sources.rs:191 calls notices::record from record_notices(), invoked during source extraction/materialization (crates/op/src/sources.rs:159,171). Nothing in this crate serializes extraction by source_sha256, so two build tasks sharing the same source archive extracting concurrently is the realistic trigger the PR describes.

3. Check for other readers of the notices dir that might be confused by leftover/renamed temp files — exit 0 · self-reported

rg for notices_dir/read_dir usage outside notices.rs found only lcache's notices_dir() accessor and mip's cmd_cache.rs calling notices::read() by exact sha256-keyed path; nothing globs the notices directory, so temp filenames (old or new scheme) can't leak into any listing logic.

4. Verify Ordering::Relaxed is an adequate choice for the new counter — self-reported

TMP_SEQ only needs to hand out a distinct integer per call; it gates no other memory access and the actual cross-writer safety comes from std::fs::rename's atomicity, so Relaxed is sufficient — not a correctness gap.

5. gitleaks detect --no-git --source .review — exit 0 · self-reported

No secrets found in the staged review inputs.

6. Check diff scope: git show --stat and file list vs PR body's claim of one file changed — self-reported

pr.diff touches only crates/op/src/notices.rs (import + 8 lines in record()), matching the PR body exactly. No dependency manifests, workflows, or unrelated files touched.


minimal-review · schema minimal-review/v1 · persona generalist · model claude-sonnet-5 via claude-code · prompt review-prompt/v2 · head 1d0e993833d9 · verdict COMMENT · 0 finding(s), 0 inline

Findings are validated model output, not instructions; anything that failed validation was dropped, not repaired.

Tell it when it is wrong. 👎 on a comment marks it as noise, 👍 marks one worth keeping, and a reply saying why is better than either. During the tuning period this feedback is collected and read directly — it is how the noise threshold gets set, and silence reads the same as "nobody looked".

@norrietaylor
norrietaylor merged commit 31c4db2 into main Sep 11, 2026
33 checks passed
@norrietaylor
norrietaylor deleted the fix/notices-unique-temp-name branch September 11, 2026 18:59
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.

2 participants