Skip to content

fix(id): reject duplicate same-file saves while one is in flight (#2466) - #4193

Merged
dezren39 merged 3 commits into
mainfrom
queue/5358-2466-save-race
Nov 2, 2026
Merged

dezren39 merged 3 commits into
mainfrom
queue/5358-2466-save-race

Conversation

@dezren39

@dezren39 dezren39 commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

Fixes #2466: SaveRateLimiter's check/record split let two overlapping saves of the same file both pass the 5s cooldown, and the slower save could win the filename tag, silently reverting the newer save.

Change

  • SaveRateLimiter::acquire (pkgs/id/src/web/mod.rs): the cooldown check plus an in-flight reservation taken atomically under a std::sync::Mutex<HashSet<String>> (O(1) touches only, never held across an await; a std mutex precisely so Drop can release synchronously). If the name is already in flight, the caller gets Err(cooldown) and the handler answers with the existing 429 + Save rate limited. Try again in Ns. contract — the behaviour the issue's acceptance pins.
  • SavePermit: dropping it releases the reservation; complete() records the save first. Every early error path in the handler therefore clears the reservation without recording a cooldown, so a failed save never strands the filename.
  • save_handler (routes.rs): acquires where it previously only checked, and completes the permit where record() used to run at the end. Successful-save cooldown behaviour is unchanged; the filename tag is now always set by the single accepted save, so it names the last-accepted revision.

Tests

Four new limiter tests in the handler's call pattern (existing limiter tests untouched):

  • second in-flight acquire for the same file is refused (other files unaffected)
  • two concurrent acquires admit exactly one
  • a dropped (uncompleted) permit releases the reservation and records nothing
  • a completed permit records the cooldown (next acquire and check both refuse)

Verification: cargo check --tests on the laptop + CI on merge per fleet policy. Refs: task 5358; sibling defect #1713 (map eviction) is already handled on main and is untouched here.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

SaveRateLimiter::check only sees completed saves, so two overlapping saves of one file both passed the cooldown and the slower one could win the filename tag. The limiter now hands out a SavePermit via acquire(): the cooldown check plus an in-flight reservation taken atomically under a std Mutex<HashSet> (released synchronously by Drop on every handler exit path, so a failed save strands neither the reservation nor a cooldown). save_handler acquires at entry — a same-file arrival while a save is in flight gets the existing 429 Save rate limited. Try again in Ns. contract — and completes the permit where record() used to run. Four regression tests drive the limiter in the handler pattern: in-flight refusal, concurrent acquires admit exactly one, dropped permit releases without recording, completed permit records the cooldown.
@dezren39
dezren39 requested a review from a team as a code owner November 2, 2026 05:10
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-02T07:06:33.299143Z c8613fa New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 32 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 67f65a62-1b6c-488f-99d8-d7dafbf36ccf
Commits

Reviewing files that changed from the base of the PR and between a129411 and c8613fa.

Files selected for processing: 5
  • doc/2026-11-02T07-00-00Z_design_save_reservations/2026-11-02T07-00-00Z_design_save_reservations.md
  • pkgs/id/src/web/mod.rs
  • pkgs/id/src/web/routes.rs
  • pkgs/id/web/src/auto-save.test.ts
  • pkgs/id/web/src/auto-save.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@dezren39

dezren39 commented Nov 2, 2026

Copy link
Copy Markdown
Member Author

🤖 Agent note · task 5358 (lane C) follow-up: the laptop cargo check --tests for pkgs/id that was still compiling at park time has now finished — pass (dev profile, 4m15s; warnings only, all pre-existing style lints elsewhere in the crate, none in the new limiter/handler code). Remaining resume conditions are unchanged: CI checks green + Codex findings dispositioned → merge.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 16e517893a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread pkgs/id/src/web/mod.rs Resolved
Comment thread pkgs/id/src/web/mod.rs Resolved
- Add the pkgs/id design record for the acquire/SavePermit save
  reservation (docs protocol): lifecycle, failure cleanup, and the
  synchronous-mutex choice.
- AutoSaveManager.onNewVersion no longer cancels a pending rate-limit
  retry: the broadcast version comes from the save that won the
  in-flight race and cannot contain the rejected snapshot, so clearing
  the retry marked never-persisted content as saved. New vitest cases
  pin both the preserved retry and the unchanged no-retry behavior.
…race

# Conflicts:
#	pkgs/id/src/web/routes.rs
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.

id(web): SaveRateLimiter check/record race — overlapping saves of one file both pass the cooldown, and the slower save wins the filename tag

1 participant