Repository navigation
fix(opstore): concurrent same-hash blob puts no longer FileExistsError - #23
Conversation
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
left a comment
There was a problem hiding this comment.
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.pyopstore/DESIGN.md(temp uniqueness contract)opstore/tests/test_blobs.pyserver/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.
CI flake on
hephaestus#17(ci / server (shard 0/3), run 33346157629 / job 99350579484):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_inputsholds the part lock only for the snapshot, then releases it before geometry (architecture§3.5). Two sessions may thereforebuild_partthe same widget at once. Both produce the same artifact bytes and both callblobs.put.putwrote through.{digest}.{pid}.tmpwithO_CREAT|O_EXCL. The name is unique across processes, not across threads in one process. The second exclusive create raisedFileExistsError(17).Not a missing
exist_okonmkdir. 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 passedG4/G5 are not touched. Web UI, CLI verbs, checkpoints, and Smith-parity chrome are not in this PR.