Repository navigation
Conditional writes are atomic: PUT and DELETE run under the per-resource queue and lock (#600) - #638
Conversation
…rce queue and lock (#600) If-Match and If-None-Match on PUT and DELETE were stat, compare, write with nothing holding the resource in between. Two writers racing with the same, still valid, ETag both passed the check and both got 2xx; the loser's body vanished. PATCH had been serialised already (in-process queue plus a lock file across processes); PUT and DELETE now go through the same helper, serialised(), so one of them wins and the rest see the new ETag and get 412. The lock file lives next to the resource and taking it creates the parent directory, so two cases answer before the lock: a DELETE of a missing resource is 404, and a PUT whose If-Match cannot hold because the resource is not there is 412. Neither leaves a directory behind. Tests (test/conditional-race.test.js, all failing before the fix): five racing PUTs with one If-Match give exactly one 204 and four 412 and the stored body is the winner's; DELETE racing PUT gives one 204 and one 412; three racing If-None-Match: * creates give one 201; a failed conditional PUT into a container that does not exist creates nothing; no lock sidecar is listed or left on disk; the sequential case is unchanged.
There was a problem hiding this comment.
Note
Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.
🟡 Changes recommended
A critical concurrency risk remains because the updated PUT/DELETE/PATCH path depends on a filesystem lock that can be treated as stale and force-removed after 15s (LOCK_STALE_MS), so longer PUT writes could have their lock broken and permit overlapping writers, undermining the atomic conditional-write guarantee.
1 open issue
What changed in this PR
This pull request fixes a race condition where concurrent conditional PUT/DELETE requests (If-Match / If-None-Match) could both pass precondition checks and return success, causing lost updates. It aligns PUT and DELETE with PATCH by running the relevant “stat → compare → write/delete” logic under the same per-resource in-process queue and cross-process lock, adds regression tests for the race scenarios, and documents the behavior.
Changes:
- Route
PUT,DELETE, andPATCHthrough a shared per-resource serialization wrapper (queue + filesystem lock) to make conditional writes atomic. - Add
test/conditional-race.test.jscovering concurrentIf-Match/If-None-Matchraces and lockfile cleanup expectations. - Document “Conditional writes are atomic” in
docs/security.md.
This pull request requires changes.
| File | Summary |
|---|---|
test/conditional-race.test.js |
Adds regression tests for concurrent conditional write races (including winner/loser outcomes and no lock sidecar leakage). |
src/handlers/resource.js |
Unifies PUT/DELETE/PATCH under per-resource serialization, but has outstanding review items: (1) early If-Match/missing-resource checks performed unlocked can return incorrect 412/404 under contention; (2) in-process queue key should match the normalized lock identity; (3) critical: lock staleness/force-removal after 15s can break the atomicity guarantee for longer PUTs. |
docs/security.md |
Documents the atomic conditional write guarantee and related lock/retry behavior. |
Review effort: Balanced
| // Writes to one resource run one at a time. A conditional write (If-Match, | ||
| // If-None-Match) is stat → compare → write; two of them racing with the same, still | ||
| // valid, ETag would both pass the check and the loser's write would vanish with a 2xx | ||
| // (#600). PATCH is read-modify-write and was already serialised this way. So PUT, | ||
| // DELETE and PATCH on a path are queued within this process and run under a lock file | ||
| // on disk across processes that share the data directory (storage.withLock). |
… is never mistaken for a dead one
Copilot's point on PR 638: withLock breaks a lock whose mtime is older than 15 s, so a
PUT that held the lock longer than that could have it taken from under it by another
process. The holder now touches the lock file every staleMs / 3 while fn runs; a stale
lock means a crashed process, not a slow one. withLock takes optional { staleMs,
waitMs } so tests can exercise the timings in milliseconds.
Also from the review: the in-process queue is keyed by the resolved lock path, the same
identity the disk lock uses; and the two unlocked pre-checks (DELETE of a missing
resource, If-Match on a missing resource) carry a note on why their answers are
consistent under contention.
test/withlock.test.js: a holder slower than the stale timeout keeps the lock and the
second writer waits for it; a lock left by a dead holder is broken; a live lock that
is never released ends in ELOCKED after waitMs.
There was a problem hiding this comment.
Note
Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.
🟡 Changes recommended
The change introduces a critical race condition in src/storage/filesystem.js around stale-lock breaking where a contender may delete a newly created lock file after both judge the prior lock stale, potentially allowing concurrent writers without a lock.
2 open issues
Review effort: Balanced
| const stat = await fs.stat(lockPath).catch(() => null); | ||
| if (stat && Date.now() - stat.mtimeMs > LOCK_STALE_MS) { | ||
| if (stat && Date.now() - stat.mtimeMs > staleMs) { | ||
| await fs.remove(lockPath).catch(() => {}); | ||
| continue; | ||
| } |
…#600) Copilot's second pass on PR 638: two contenders can both see the same dead lock; the faster one removes it and takes a fresh lock, and the slower one's unlink then removes that fresh lock, leaving two writers under no lock. This predates the PR but the PR widened its reach to PUT and DELETE. breakStaleLock moves the stale file aside with rename, which is atomic, so only one contender gets it and the other sees ENOENT and looks again. It then compares the moved file's inode with the one it observed stale: a match is deleted; a mismatch is somebody's fresh lock caught between their break and our rename, and it is put back with link, which fails rather than overwrite a third contender's lock. test/withlock.test.js: eight contenders on a dead lock all run, never two at once, and leave no lock or moved-aside file behind.
…al-write-lock # Conflicts: # docs/security.md
Changes since 0.0.224: - Security: the dotfile guard decodes path segments before checking them, and the storage lookup refuses a segment that only becomes a dotfile by decoding, so `%2Egit/config` can no longer reach `.git/config`. Malformed percent-encoding and an encoded path separator (`%2F`) are refused with 400. New src/utils/dotfiles.js holds the shared rule. (#639, PR #640) - Conditional writes are atomic: PUT and DELETE join PATCH under the per-resource queue and cross-process lock file, so If-Match and If-None-Match are checked and acted on as one step. Two clients racing with the same ETag get one 2xx and one 412. The lock holder heartbeats so a slow write is never mistaken for a dead one; a stale lock is broken by rename and inode check, never a blind unlink; a request that cannot take the lock within 30 s gets 503 with Retry-After: 1. (#600, PR #638) - WAC-exempt app subtrees: a path under an app prefix that the app did not register answers 404 instead of falling through to pod storage with WAC skipped. OPTIONS under an app path is a bare 204. (#611, PR #637) - docs/security.md: Dotfiles and Conditional writes sections.

Fixes #600.
The bug
If-MatchandIf-None-Matchon PUT and DELETE were stat, compare, write, with nothing holding the resource in between. Two writers racing with the same, still valid, ETag both passed the check and both got2xx; the loser's body was gone. Reproduced intest/conditional-race.test.jsagainstgh-pages, three runs out of three:PATCH had already been serialised (in-process queue per path, plus a
.<name>.lockfile next to the resource across processes that share the data directory). PUT and DELETE were not.The fix
src/handlers/resource.js: the PATCH queue-and-lock wrapper becomesserialised(storagePath, reply, fn)and PUT, DELETE and PATCH all go through it. For PUT, everything from the stat onwards (precondition check, quota reservation, write) runs inside; the cheap rejections (read-only, container PUT, content type, ACL body) stay outside. For DELETE, the stat is repeated under the lock. A writer that cannot take the lock within 30 s gets503withRetry-After: 1, as PATCH already did.The lock file lives next to the resource and taking it creates the parent directory, so two cases are answered before the lock to avoid leaving an empty container behind: DELETE of a missing resource is
404, and a PUT whoseIf-Matchcannot hold because the resource is not there is412.Tests
test/conditional-race.test.js, new, six cases, all but the last failing before the fix:If-Match: exactly one204, four412, and the stored body is the winner'sIf-Match: one204, one412, and the resource state matches the winnerIf-None-Match: *creates: one201, two412204then412, unchangeddocs/security.mdgets a short section, "Conditional writes are atomic".Review follow-ups (f8fff8a)
Copilot: the disk lock was treated as stale after 15 s, so a write holding it longer could have had it broken by another process. The holder now touches the lock file every
staleMs / 3while it works, so a stale lock means a crashed process, not a slow one.withLocktakes optional{ staleMs, waitMs }for tests. Also: the in-process queue is keyed by the resolved lock path, the identity the disk lock uses; the two unlocked pre-checks carry a note on why their answers are consistent under contention (a request that finds nothing at the probe gets the answer it would have got had it run first; the check is repeated under the lock).test/withlock.test.js, new: a holder slower than the stale timeout keeps the lock and the second writer waits; a lock left by a dead holder is broken; a live lock never released ends inELOCKEDafterwaitMs.Copilot, second pass: two contenders could both judge a dead lock stale, the faster one break it and take a fresh lock, and the slower one then unlink that fresh lock. Pre-existing, but the PR widened its reach.
breakStaleLocknow moves the stale file aside with an atomic rename, compares the moved file's inode with the one observed, deletes it on a match and puts it back withlink(which refuses to overwrite) on a mismatch. Test: eight contenders on a dead lock all run, never two at once, nothing left behind.Full suite on the branch: 1113 tests, 1113 pass, 0 fail.