Repository navigation
Description
Finding
PR #4193 (merged, commit 06ea8c8) fixed the #2466 check/record race by adding SaveRateLimiter::acquire, but the acquire path itself is not atomic, and its design note claims it is:
- The design doc merged in the same PR (
doc/2026-11-02T07-00-00Z_design_save_reservations/2026-11-02T07-00-00Z_design_save_reservations.md, decision 1) saysacquire"combines the existing cooldown check with an in-flight reservation, taken atomically under the in-flight lock". - The code — unchanged on current main at
pkgs/id/crates/id-web/src/lib.rs:156-166(the file moved in the crate split; line numbers are current main) — does:
pub async fn acquire(&self, name: &str) -> Result<SavePermit, Duration> {
self.check(name).await?; // (1) cooldown check, tokio mutex on last_save
let mut flight = self.in_flight.lock() // (2) reservation, a *different* std mutex
.unwrap_or_else(|poisoned| poisoned.into_inner());
if !flight.insert(name.to_owned()) { return Err(self.cooldown); }
...
}check (1) and the reservation insert (2) are separated by an await on a different lock, so on the multi-threaded tokio runtime the web server runs on, this interleaving is possible:
- Save A holds the permit and is finishing:
SavePermit::complete()callsrecord()(writeslast_save[name] = now), then Drop removesnamefromin_flight. - Save B calls
acquire(name)and itscheckreadslast_savebefore A'srecord()lands — it passes. - B's insert runs after A's Drop removed the name — it succeeds.
B is admitted inside the cooldown A just recorded. This does not recreate the original #2466 overlap (B can only insert after A released the reservation, i.e. after A finished, so saves never run concurrently) — what it re-opens is the cooldown guarantee that acquire exists to enforce atomically: a client retrying at exactly the completion moment bypasses the 5 s rate limit the 429 contract promises.
Second, smaller defect in the same lifecycle: SavePermit::drop (current main, lib.rs:206-208) releases the reservation with if let Ok(mut flight) = ...lock(), while acquire explicitly recovers a poisoned lock with into_inner(). If the in_flight mutex is ever poisoned, Drop silently leaves the name in the set, and every later acquire (which recovers the guard and sees the name still present) refuses that filename forever — a permanently wedged save for that file, with no error anywhere.
Evidence
- PR: fix(id): reject duplicate same-file saves while one is in flight (#2466) #4193 (merge commit 06ea8c8, verified an ancestor of current main ed926a9 — compare
behind_by=0) - Source PR's parent issue: id(web): SaveRateLimiter check/record race — overlapping saves of one file both pass the cooldown, and the slower save wins the filename tag #2466 (CLOSED)
- Current main:
pkgs/id/crates/id-web/src/lib.rs—acquireat line 156 (self.check(name).await?at 157, in-flight lock at ~159-161),completeat ~199-204 (recordthen Drop release),Drop for SavePermitat line 206 (if let Okat 207). Read twice this run (code search + raw contents), identical. - Dedupe (2026-11-13): issue searches for
SaveRateLimiter acquire,save limiter in-flight,acquire re-check cooldown,in_flight poisoned savereturned only PR fix(id): reject duplicate same-file saves while one is in flight (#2466) #4193 / issue id(web): SaveRateLimiter check/record race — overlapping saves of one file both pass the cooldown, and the slower save wins the filename tag #2466 themselves — no open or closed issue covers this residual.
Why it matters
The whole point of #4193 over the old check/record split was that the cooldown + in-flight decision is one atomic step. As merged (and still on main) it is two steps with a completion window between them, so the rate-limit guarantee has a race at exactly the busiest moment (a save just completed, a retry already waiting). The poisoned-Drop asymmetry is a latent permanent wedge for a filename if the lock is ever poisoned — and acquire's own poison recovery shows poisoning was contemplated on one path but not the other.
Acceptance criteria
-
acquirere-validates the cooldown after taking the in-flight reservation (or otherwise makes check + reserve atomic with respect torecord+ release), so a save that records while anotheracquireis between check and insert causes that acquire to be refused with the cooldown error, and the refused acquire leaves no reservation behind. - A deterministic regression test orchestrates the completion-during-acquire interleaving (not a sleep/timing loop) and fails before the fix / passes after; the existing limiter tests (refuses second in-flight, concurrent acquires admit exactly one, dropped permit releases without recording, completed permit records) still pass.
-
SavePermit::droprecovers a poisonedin_flightlock the same wayacquiredoes (into_inner), with a test or a reasoned note if a poison test is impractical. - The design doc's "taken atomically" wording matches the implementation after the fix.
Shadow note
Stage A shadow verdicts are closed (window ended 2026-11-13, summary on #2313), so no shadow verdict section is attached to this filing.
Activity
developing-today commentedon Nov 13, 2026
CODE-3180
dezren39 commentedon Nov 13, 2026
🤖 Agent note · filed
dezren39 commentedon Nov 13, 2026
🤖 Agent note · picked up + triaged
acquireat pkgs/id/crates/id-web/src/lib.rs:156 doescheck().awaitthen a separate std-mutex insert;SavePermit::dropat :206 usesif let Ok, unlike acquire's poison recovery. No hard-stop labels; issue open; PR fix(id): reject duplicate same-file saves while one is in flight (#2466) #4193 merged.dezren39 commentedon Nov 13, 2026
Triage verified against fresh GitHub state. Both defects confirmed present on current
main.main=ed926a977e(fetched this run;pkgs/id/crates/id-web/src/lib.rsunchanged since PR fix(id): reject duplicate same-file saves while one is in flight (#2466) #4193 exceptcargo fmtdrift). PR fix(id): reject duplicate same-file saves while one is in flight (#2466) #4193 is an ancestor ofmain.gh pr list --state open --search "SaveRateLimiter"and--search "9587"return nothing. Issue still open.Defect A —
acquirecheck and reservation are not mutually atomicpkgs/id/crates/id-web/src/lib.rs,SaveRateLimiter::acquire(origin/main lines ~155-168):check(cooldown, tokio mutex onlast_save) and the in-flight reservation (std mutex onin_flight) are two separate critical sections.SavePermit::completedoesrecord()(setslast_save) thenDrop(removesin_flight) — in that order. So this interleaving admits a save inside a just-recorded cooldown:acquire:check("a.txt")returnsOk— no prior completed save.complete:record("a.txt")setslast_save = now; then A1's permitDropremoves"a.txt"fromin_flight.acquire:in_flight.insert("a.txt")now succeeds ("a.txt"was just removed) → A2 is admitted, bypassing the cooldown A1 just recorded.This is exactly the race PR #4193 was meant to close, re-opened for a completed save rather than an overlapping one.
Defect B —
SavePermit::dropdoes not recover lock poisoningacquirerecovers poisoning with.unwrap_or_else(|p| p.into_inner()), butDrop(origin/main lines ~205-209) usesif let Ok(mut flight) = ... .lock():If
in_flightis ever poisoned (a panic while the lock is held),dropsilently skips the removal, stranding the filename inin_flightforever — every lateracquirefor that file returnsErr(cooldown).acquirerecovers;dropwedges.Design-doc mismatch (acceptance item 4)
doc/2026-11-02T07-00-00Z_design_save_reservations/…mddecision 1 states the cooldown check and the in-flight reservation are "taken atomically under the in-flight lock". They are not: the check runs in a separate critical section before the lock is taken. The wording needs to match whatever the fix makes true.Proposed fix (reserve, then re-validate)
Reorder so the reservation is taken first and the cooldown is re-validated after it; a refused acquire releases the reservation it just took:
Invariant:
in_flight.insertsucceeds only when no permit for the name is outstanding, so no otherrecord/dropfor that name can run between our insert and our re-check; the re-check therefore observes any cooldown a prior completion recorded. Update theacquiredoc comment and design decision 1 to say the reservation is taken first and the cooldown re-validated under it.Test plan (acceptance item 2)
checkandinsert) has no existing seam; recommended: add a#[cfg(test)]hook — a counter/Notifyconsulted between the old check and insert points — so the test can force the A2.check → A1.complete → A2.insert order, assert A2 is refused post-fix. If a seam is judged too intrusive, a bounded-loop concurrent stress test (manyjoin!acquire/complete pairs) reliably catches the race pre-fix; note the determinism trade-off in the PR.test_save_limiter_acquire_refuses_a_second_in_flight_save,…completed_permit_records_the_cooldown,…concurrent_acquires_admit_exactly_one,…dropped_permit_releases_without_recording).Disposition
Fix deferred to a queued follow-up (small fixes are not compiled on this host within the run budget — no warm
target/; CI-first). Follow-up task enqueued against issue #9587.Merge pull request #9590 from developing-today/queue/16143-9587-save-…
dezren39 commentedon Nov 13, 2026
🤖 Agent note · done
SaveRateLimiter::acquire(pkgs/id/crates/id-web/src/lib.rs) now re-validates the cooldown after taking the in-flight reservation — complete() records before Drop releases, so a completion racing the check/reserve gap is observed and refused, and a refused acquire releases its reservation.SavePermit::droprecovers a poisoned in-flight lock viainto_inner(), matching acquire. Design doc decisions 1/3 wording updated to match.dezren39 commentedon Nov 13, 2026
🤖 Agent note · already done (duplicate packet)
dezren39 commentedon Nov 13, 2026
Agent note — task 16149 pickup + already-done (lane any-b)