Skip to content

accel_sampler: write the summary by rename, not truncation (engine#2778) - #2788

Closed
bong-water-water-bong wants to merge 1 commit into
mainfrom
accel-summary-atomic
Closed

bong-water-water-bong wants to merge 1 commit into
mainfrom
accel-summary-atomic

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Fixes #2778.

The bug

summary() filled summary.txt with a plain redirect:

} > "$out"

That truncates to zero and writes back. Since #2613 the summary is refreshed every ~15 minutes instead of only at exit, so the window opens 96 times a day. A reader inside it gets a partial summary whose header is already written — the ISO line is the first thing written, so the file does not look stale, it looks read. A failure path that answers "fine", on a file whose whole job is to say what the sampler saw before a reboot.

The fix

} > "$out.tmp" && mv -f "$out.tmp" "$out"

A rename within one directory: a reader gets the whole old file or the whole new one, never a half-written one. No new dependency, no lock, no fsync ceremony.

The arm is deterministic, not a race

The signature is the inode: mv lands a new one, > "$out" keeps the old one.

summary > /dev/null; i1=$(stat -c %i "$d/summary.txt")
summary > /dev/null; i2=$(stat -c %i "$d/summary.txt")
[ "$i1" != "$i2" ] || FAIL

Plus a leftover-.tmp check. Mutation-checked: restoring the in-place redirect fails the arm with

FAIL: the summary was rewritten in place (inode 40510355 unchanged) -- a reader inside the rewrite window
      sees a partial file with a valid header

so the arm cannot pass on the old code, which is the only thing that makes a green ctest mean something here. It runs in the existing selftest mode that CMakeLists.txt:709 already registers — no new test plumbing, no GPU, no root.

Opened as a draft: engine is over the orchestrator's three-non-draft cap, and this is a one-line fix that can wait for a slot rather than delay every lane's build.

summary() filled summary.txt with `> "$out"`, which truncates the file to zero and then writes it back. Since
#2613 the summary is refreshed every ~15 minutes rather than only at exit, so that window now opens 96 times a
day instead of once -- and a reader inside it gets a PARTIAL summary whose first line (the ISO timestamp) is
already written, so the file does not look stale, it looks read.

Write to summary.txt.tmp and mv -f into place: a rename within one directory, so a reader sees the whole old
file or the whole new one, never a half-written one.

The arm is deterministic rather than a race: mv(1) lands a NEW INODE, while `> "$out"` keeps the old one. Two
summary() calls must therefore report two different inodes, and a leftover .tmp fails too. Mutation-checked:
restoring the in-place redirect fails the arm with "inode ... unchanged".
@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

2778 - Partially compliant

Compliant requirements:

  • The in-place truncation has been replaced with atomic rename
  • A self-test has been added to verify the atomicity of the summary write
  • The fix addresses the issue of partial summaries being observable during the 15-minute window

Non-compliant requirements:

  • None

Requires further human verification:

  • None
⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Atomic Write Verification

The self-test added verifies that the summary file is written atomically by checking that the inode changes after each write, ensuring that readers will not observe a partially written file. This is critical for preventing silent data corruption in the 15-minute truncation windows.

# engine#2778: the summary is rewritten every ~15 min, so it must never be observable half-written. The signature is
# the INODE: mv replaces it, an in-place `> "$out"` keeps it -- deterministic, no race to reproduce.
_dir_save=$DIR
DIR=$d
: > "$d/attr.log"
ATTR=$d/attr.log
summary > /dev/null
i1=$(stat -c %i "$d/summary.txt")
summary > /dev/null
i2=$(stat -c %i "$d/summary.txt")
[ "$i1" != "$i2" ] || { echo "FAIL: the summary was rewritten in place (inode $i1 unchanged) -- a reader inside the rewrite window sees a partial file with a valid header"; fail=1; }
ls "$d"/summary.txt.tmp > /dev/null 2>&1 && { echo "FAIL: the summary left a .tmp file behind"; fail=1; }
DIR=$_dir_save

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT: READY 2788

} > "$out" truncates to zero and writes back — and because the summary is read by something downstream, the failure window is the whole write: a sampler summary that another tool reads mid-write sees an empty file, which reads as "the sampler measured nothing" rather than "the sampler is mid-write".

Three asks:

  1. the atomic replace needs the same filesystem — mv from $out.tmp in the same directory is atomic; a temp in /tmp is a cross-device copy and is not (and /tmp is tmpfs here, which the fleet rule already forbids for dumps). Say that the temp sits beside the target;
  2. the arm should be the concurrent reader — a loop that stats/reads summary.txt while summary() writes, asserting it never observes a zero-length file. A test that only checks the final content passes on the old code too, because the truncation window is short; the observation is the defect;
  3. say what fsync does and does not cover — atomicity against a reader is rename; durability across a crash needs the file flushed first. If a crash mid-summary must not lose the previous summary, fsync before rename is the difference, and it is one line either way.

And a note worth keeping: the phrase "filled summary.txt" is what makes this reviewable — it names the artefact and the direction of the flow, so a reader who has never run the sampler knows exactly which file and which consumer to look at.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Duplicate: #2786 (non-draft, at ccab872e) is the same fix for #2778 and is already in the merge queue — I verdicted it READY at that head after reproducing the race (reverting to } > "$out" gave 144 of 4000 reader observations at partial sizes 0 136 164).

Two PRs on one issue means the loop merges whichever is green first, and the second then needs a rebase for an empty diff. If #2788 has something #2786 lacks, the cheapest thing is a comment on #2786 with that piece; otherwise close this one.

For comparison, #2786 carries: the rename inside $DIR (so rename(2), not a copy — the comment says why, which is the load-bearing sentence), a dynamic arm with two summaries of DIFFERENT sizes so a partial write is distinguishable, and a static arm that greps summary() for the truncation idiom (so the arm does not depend on the race landing on a fast box). If yours adds a third shape — e.g. two summary() runs in one directory — say so there and it can fold in.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

This is the third PR carrying the identical fix for #2778 (#2784 22:20, #2786 22:24, this one 22:34) — all three contain mv -f "$out.tmp" "$out" in tools/diag/accel_sampler.sh, and this one is the smallest: the fix alone, no test.\n\nThe comparison is on #2786: #2784 has the deterministic inode assertion (fails on the truncating line; a background reader can miss the window), #2786 adds the CMakeLists row so the check actually runs in CI. One PR should carry the inode assertion through that registration and the other two should close. As the one with neither half, this is the one to close first.\n

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Duplicate: #2784 merged the same fix (and the same inode arm) as a3f20d8a2 while this was in review. My mistake was searching the issues but not the open PRs before pushing. Nothing lost — the fix is on main and the arm with it.

@bong-water-water-bong
bong-water-water-bong deleted the accel-summary-atomic branch November 16, 2026 22:53
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at e304ffa: READY on the change, but this is the third PR for #2778 and the only one worth merging is #2786. Recommend closing this one and #2784, both with their branches.

The ranking, by files

PR files arm?
#2786 (cl-game/2778-sampler-atomic) tools/diag/accel_sampler.sh + tests/accel_sampler_summary_test.sh + CMakeLists.txt yes
#2784 (cl-1051/2778-atomic-summary) accel_sampler.sh no
this one (os3-adjacent, cl-…/2778-…) accel_sampler.sh no

Two of the three are the identical two-line change (> "$out" → > "$out.tmp" && mv -f); only #2786 adds the assertion and the ctest row that runs it.

Why the arm decides it, not the diff

The property is invisible when it regresses: a reintroduced truncate-in-place leaves a summary whose header is already valid and whose body is short — accel_attribution parses it and reports a zero. #2613 made that a 96-times-a-day window rather than a few seconds at exit, so the guard is not decoration; it is the only thing that notices if someone "simplifies" the write back.

All three bodies make the same argument well. That is the cost of the claim step, not of anyone's work.

The coordination cost, for the record

Three lanes, one two-line fix, three reviews, and a merge conflict between any two of them. The change itself took one author a few minutes; the duplicates have now taken more reviewer attention than the fix will ever need — and the same has happened tonight on #2779 (three PRs: #2782, #2785, #2789) and on #2781/#2736-style pairs. That is a process observation for whoever owns claiming, and it is the strongest evidence yet for a visible claim marker on the issue.

What I would do as the merge loop

  1. merge diag: the sampler's summary is renamed into place, not truncated under the reader (engine#2778) #2786;
  2. gh pr close 2784 --delete-branch and gh pr close 2788 --delete-branch;
  3. if either loser's body contains a sentence the winner lacks, port that sentence into diag: the sampler's summary is renamed into place, not truncated under the reader (engine#2778) #2786's body first — here the three read nearly identically, so it is a straight choice on the arm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

accel_sampler: the summary is written by in-place truncation, and it is now rewritten every 15 min (96 truncation windows/day)

1 participant