Skip to content

fix(opstore): concurrent same-hash blob puts no longer FileExistsError - #23

Merged
Code4me2 merged 2 commits into
mainfrom
cursor/concurrent-blob-put-race-eb6c
Aug 31, 2026
Merged

Code4me2 merged 2 commits into
mainfrom
cursor/concurrent-blob-put-race-eb6c

Conversation

@Code4me2

@Code4me2 Code4me2 commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

CI flake on hephaestus#17 (ci / server (shard 0/3), run 33346157629 / job 99350579484):

server/tests/test_request_binding.py::test_two_concurrent_runs_each_read_their_own_request
AssertionError: a concurrent turn crashed: FileExistsError(17, 'File exists')

That PR is web-only. This failure is not caused by it. Tracking is the flake only — does not close #11 or #4.

Race

Publisher.freeze_inputs holds the part lock only for the snapshot, then releases it before geometry (architecture §3.5). Two sessions may therefore build_part the same widget at once. Both produce the same artifact bytes and both call blobs.put.

put wrote through .{digest}.{pid}.tmp with O_CREAT|O_EXCL. The name is unique across processes, not across threads in one process. The second exclusive create raised FileExistsError(17).

Not a missing exist_ok on mkdir. The colliding path is the exclusive temp file.

Fix

Temp names include a per-writer random token. If another thread already installed the same content, the loser unlinks its temp and both return the same hash. The request-binding test is unchanged in what it asserts (G4/G5 not touched); it now prints the traceback if a concurrent turn crashes again.

Tests

Locally:

  • opstore/tests/test_blobs.py — 16 passed (including crash recovery + the new concurrent-put race, 40 pairs)
  • server/tests/test_request_binding.py — 14 passed
  • The original concurrent test, 5 extra repeats — all passed

G4/G5 are not touched. Web UI, CLI verbs, checkpoints, and Smith-parity chrome are not in this PR.

Open in Web Open in Cursor 

cursoragent and others added 2 commits August 31, 2026 01:21
Two in-process puts of the same content shared .{digest}.{pid}.tmp, so
O_CREAT|O_EXCL raised FileExistsError(17) when two sessions built the
same part. Temp names are now unique per writer.

Co-authored-by: Vel <Code4me2@users.noreply.github.com>
Extract the two-thread put race from the loop body and format the
request-binding crash traceback the way ruff wants it.

Co-authored-by: Vel <Code4me2@users.noreply.github.com>
@Code4me2
Code4me2 marked this pull request as ready for review August 31, 2026 01:23

@Code4me2 Code4me2 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Verdict: MERGE

Adversarial review of #23 (37c9a99 vs main e4e3e01). No merge-blocking bug in this PR's scope. Did not merge. Did not expand scope.

Race is real -- temp name was pid-only

On main, put used .{digest}.{pid}.tmp + O_CREAT|O_EXCL. Publisher.freeze_inputs holds the part lock only for the snapshot and releases it before geometry (architecture §3.5). Two in-process sessions can therefore put the same artifact bytes. Threads share a pid, so the second exclusive create is FileExistsError(17).

Reproduced the old name scheme: two threads, same digest, FileExistsError(17) on iteration 0.

The fix adds os.urandom(8).hex() so the exclusive temp is unique per writer, not per pid. Collision on the create is 2^-64; not a practical residual.

Concurrent identical puts do not FileExistsError

Losers that see final already installed unlink their own temp. INSERT ... ON CONFLICT(hash) DO NOTHING was already in place, so both threads can commit the accounting row. Database already serializes in-process transactions on _txn_lock.

test_concurrent_puts_of_the_same_bytes_do_not_raise (40 fresh payloads so the exists() short-circuit does not hide the race) is the right regression. Extra local stress: 80 payloads x 8 threads, all returned the same hash, no exceptions.

The final.exists() / rename TOCTOU is still there -- both writers can pass the check and both os.rename. On POSIX that is an atomic same-bytes replace, not EEXIST. Not blocking.

No leftover temps

Success path always unlinks or renames the writer-private temp. The new test rglobs the whole blobs/ tree for .* files after all 40 pairs. Same leftover assertion held after the 8-way stress.

Crash-after-fsync orphans were already a designed recovery case (temp orphan is invisible and rewritten). Unique names do not make that worse in a way this PR needs to solve.

G4 / G5 not skipped

Diff vs e4e3e01 is four files. server/tests/test_request_binding.py only formats the crash traceback (traceback.format_exception). Assertions are unchanged: errors == [], seen["a"] == [40.0], seen["b"] == [77.0]. No skip/xfail, no -k filter, no workflow edit. G4 (Playwright web/e2e) and G5 are not in this PR.

Scope

  • opstore/src/opstore/blobs.py
  • opstore/DESIGN.md (temp uniqueness contract)
  • opstore/tests/test_blobs.py
  • server/tests/test_request_binding.py (diagnostics only)

No web UI, CLI, checkpoints, or Smith-parity chrome.

CI on 37c9a99: all 18 checks green, including opstore + stage0a, server shards 0-2 (the original flake job), and stage gates 1-6.

@Code4me2
Code4me2 merged commit 6203427 into main Aug 31, 2026
18 checks passed
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.

web: tree sections + stop duplicating Results

2 participants