Storm: one outside rewrite never landed (possible lost edit) #222

Closed
opened 2026-10-14 09:18:31 +00:00 by kayg · 2 comments
Owner

Found by the perf storm harness (#44) on the perf VM.

What happened

  • At 100k notes, in 1 of 5 storm runs on the build before #44's changes, one of about 35 outside rewrites (a file rewritten on disk behind a live room, the watcher path) never landed in the note.
  • All clients and the server agreed on the result without it. The lines before and after it were present.
  • Not seen in any run after #44's changes, nor in 15 earlier runs.
  • calternal.cloud runs dev, so if this is real it can lose kayg's edits made outside the app.

Plan

  1. Reproduce: a focused stress test in crates/notes/src/tests with many outside rewrites, typing from 2–3 clients and saves, thousands of iterations with random timing.
  2. Root cause in watch.rs: echo-by-hash, diff against the last-written base, "our own write" detection (#3 round 2), debounce/coalescing, a rewrite landing during a save.
  3. Minimal fix, keep the test as a regression. If it is harness-only, prove it with evidence.

Owned by a Claude agent.

Found by the perf storm harness (#44) on the perf VM. **What happened** - At 100k notes, in 1 of 5 storm runs on the build before #44's changes, one of about 35 outside rewrites (a file rewritten on disk behind a live room, the watcher path) never landed in the note. - All clients and the server agreed on the result without it. The lines before and after it were present. - Not seen in any run after #44's changes, nor in 15 earlier runs. - calternal.cloud runs dev, so if this is real it can lose kayg's edits made outside the app. **Plan** 1. Reproduce: a focused stress test in `crates/notes/src/tests` with many outside rewrites, typing from 2–3 clients and saves, thousands of iterations with random timing. 2. Root cause in `watch.rs`: echo-by-hash, diff against the last-written base, "our own write" detection (#3 round 2), debounce/coalescing, a rewrite landing during a save. 3. Minimal fix, keep the test as a regression. If it is harness-only, prove it with evidence. Owned by a Claude agent.
Author
Owner

Reproduced, root cause found. It is a real server bug, not the harness.

Root cause: a save overwrote an outside edit that landed mid-save

room/save.rs checked the file against our last write, then wrote. Between that check and the rename() there was:

  • the pending-save write to history (SQLite, fsync), and
  • writing and fsyncing the temp file.

At 100k notes, history writes are slower, so the window is wider. That matches "seen only at 100k, before #44's changes". If another app renamed its version over the note inside that window, our rename() replaced it. The watcher's later event then read our bytes, which matched our hash, so it was ignored as an echo. The edit was gone with no trace. Everyone agreed on the result, and the lines before and after it were present: exactly the storm symptom.

None of the other suspects was the cause:

  • echo-by-hash was correct;
  • diffing against the base was correct;
  • coalescing watcher events is harmless, because the room always reads the current file.

Repro: crates/notes/src/tests/storm.rs

  • 3 typists, 120 keystrokes each, with random 0–3 ms gaps.
  • Saves on their own (40 ms quiet) plus 20 saves on request at random moments.
  • Another app rewrites the file every 0–8 ms the way editors do: read, append a line, write a temp file, rename it over the note.
  • Pass condition: every outside line is in the note exactly once, all clients equal the server, and the file equals the note.

A temporary probe placed between the check and the write fired several times per storm ("file changed between the check and the write").

Baseline (dev, unfixed): 196 of 200 storms lost lines, then 100 of 100 with the final test. A typical run: seed 1: 5 of 58 outside lines not exactly once: 1.13, 1.18, 1.24, 1.27, 1.37 (0 times each).

Fix (PR to follow)

A new function, calternal_fsio::write_unless_changed, checks the file once more right before writing. It then swaps our file in with one atomic exchange (renameat2(RENAME_EXCHANGE), through rustix; macOS uses RENAME_SWAP).

If the file it replaced is not the very file it checked (same device and inode), another app wrote in between. The save then swaps that file straight back, ingests it like any outside edit, and tries again. Nothing is ever deleted on an error.

I tried two other designs first and the storm caught both:

  1. Keep our write, merge what it replaced. This doubled lines when the other app wrote again from the copy it had read.
  2. Swap back, but check after the swap. In debug builds under load, the check (read and hash) took long enough for a reader to see our version. That lost about 1 storm in 40.
**Reproduced, root cause found. It is a real server bug, not the harness.** ### Root cause: a save overwrote an outside edit that landed mid-save `room/save.rs` checked the file against our last write, then wrote. Between that check and the `rename()` there was: - the pending-save write to history (SQLite, fsync), and - writing and fsyncing the temp file. At 100k notes, history writes are slower, so the window is wider. That matches "seen only at 100k, before #44's changes". If another app renamed its version over the note inside that window, our `rename()` replaced it. The watcher's later event then read **our** bytes, which matched our hash, so it was ignored as an echo. The edit was gone with no trace. Everyone agreed on the result, and the lines before and after it were present: exactly the storm symptom. None of the other suspects was the cause: - echo-by-hash was correct; - diffing against the base was correct; - coalescing watcher events is harmless, because the room always reads the current file. ### Repro: `crates/notes/src/tests/storm.rs` - 3 typists, 120 keystrokes each, with random 0–3 ms gaps. - Saves on their own (40 ms quiet) plus 20 saves on request at random moments. - Another app rewrites the file every 0–8 ms the way editors do: read, append a line, write a temp file, rename it over the note. - Pass condition: every outside line is in the note exactly once, all clients equal the server, and the file equals the note. A temporary probe placed between the check and the write fired several times per storm ("file changed between the check and the write"). **Baseline (dev, unfixed):** 196 of 200 storms lost lines, then 100 of 100 with the final test. A typical run: `seed 1: 5 of 58 outside lines not exactly once: 1.13, 1.18, 1.24, 1.27, 1.37 (0 times each)`. ### Fix (PR to follow) A new function, `calternal_fsio::write_unless_changed`, checks the file once more right before writing. It then swaps our file in with one atomic exchange (`renameat2(RENAME_EXCHANGE)`, through rustix; macOS uses `RENAME_SWAP`). If the file it replaced is not the very file it checked (same device and inode), another app wrote in between. The save then swaps that file straight back, ingests it like any outside edit, and tries again. Nothing is ever deleted on an error. I tried two other designs first and the storm caught both: 1. **Keep our write, merge what it replaced.** This doubled lines when the other app wrote again from the copy it had read. 2. **Swap back, but check after the swap.** In debug builds under load, the check (read and hash) took long enough for a reader to see our version. That lost about 1 storm in 40.
Author
Owner

Fixed in PR #264 (not merged; waiting for review).

Storm results (one storm per seed, debug build, on a shared machine)

build storms storms that lost a line
dev (unfixed), first test version 200 196
dev (unfixed), final test 100 100
first attempt: keep ours, merge what we replaced 40 1 (a doubled line)
second attempt: swap back after a full check 1,592 43
final (check, swap, compare inode, swap back) 2,000 + 2,001 + 401 + 601 = 5,003 1

The one remaining failure was seed 292 in the first 2,000-storm batch, which ran 2 lanes in parallel. Two adjacent lines were lost. It never came back in about 3,000 more storms, including 2,001 with full tracing.

I hardened the inode check against that run's most plausible cause, without proof: the checked file is now kept open across the swap, so a newer file can't reuse its inode number. The 601 storms after that change all passed. It is still a 1-in-5,000 event under artificial load, and I could not trace it.

Remaining limit, by design

For the few microseconds between the swap and the swap back, the file holds our save. An app that reads in exactly that instant and writes back without re-reading would remove its own last line. That is the same class as the documented "stale copy" exception in ADR 0020. The storm's app re-reads when its own line is missing. Across all the final storms that re-read never fired in a passing run, so in practice the transient was not observed.

Write cost

The extra work is one more read of the note file before the swap. It is noise next to fsync:

  • disk-backed folder: 20–60 ms per save for both the old and the new write on this busy machine;
  • tmpfs: 30–300 µs for both.

CI

  • rust: passes.
  • web: fails in packages/editor/test/tree-cost.test.ts ("expected 1001 to be 1000"). That test belongs to the tree-lines work on dev (#133), and this PR touches no web code.

Note for #44

The perf storm (crates/perf/src/scenarios/storm.rs) does the same read, append, rename, so it hit this bug. With this fix it should stop flaking. In theory it could still hit the transient described above, because it does not re-read.

**Fixed in PR #264** (not merged; waiting for review). ### Storm results (one storm per seed, debug build, on a shared machine) | build | storms | storms that lost a line | |---|---|---| | dev (unfixed), first test version | 200 | 196 | | dev (unfixed), final test | 100 | 100 | | first attempt: keep ours, merge what we replaced | 40 | 1 (a doubled line) | | second attempt: swap back after a full check | 1,592 | 43 | | **final** (check, swap, compare inode, swap back) | **2,000 + 2,001 + 401 + 601 = 5,003** | **1** | The one remaining failure was seed 292 in the first 2,000-storm batch, which ran 2 lanes in parallel. Two adjacent lines were lost. It never came back in about 3,000 more storms, including 2,001 with full tracing. I hardened the inode check against that run's most plausible cause, without proof: the checked file is now kept open across the swap, so a newer file can't reuse its inode number. The 601 storms after that change all passed. It is still a 1-in-5,000 event under artificial load, and I could not trace it. ### Remaining limit, by design For the few microseconds between the swap and the swap back, the file holds our save. An app that reads in exactly that instant and writes back without re-reading would remove its own last line. That is the same class as the documented "stale copy" exception in ADR 0020. The storm's app re-reads when its own line is missing. Across all the final storms that re-read never fired in a passing run, so in practice the transient was not observed. ### Write cost The extra work is one more read of the note file before the swap. It is noise next to fsync: - disk-backed folder: 20–60 ms per save for both the old and the new write on this busy machine; - tmpfs: 30–300 µs for both. ### CI - rust: passes. - web: fails in `packages/editor/test/tree-cost.test.ts` ("expected 1001 to be 1000"). That test belongs to the tree-lines work on dev (#133), and this PR touches no web code. ### Note for #44 The perf storm (`crates/perf/src/scenarios/storm.rs`) does the same read, append, rename, so it hit this bug. With this fix it should stop flaking. In theory it could still hit the transient described above, because it does not re-read.
kayg closed this issue 2026-10-14 12:15:58 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
kayg/calternal#222
No description provided.