fix: serialize concurrent runtime cache writes#373
Conversation
Quota cache saves and update-check cache writes could overlap within the same process. That risks write races, and the update checker also wrote directly to the final cache file instead of staging through a temp file. Serialize in-process writes for both caches and make update-check cache writes atomic via temp-file rename so concurrent runtime writers do not corrupt the on-disk state.
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
|
Warning Rate limit exceeded
Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 12 minutes and 56 seconds. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
✨ Simplify code
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. Comment |
|
Superseded by #387, which rebuilds the full open PR stack onto one reviewed integration branch. |
|
Closing in favor of #387. |
Problem
Multiple runtime cache writers could overlap within the same process.
quota-cachealready used temp-file renames, but concurrent saves still raced logically.auto-update-checkerwrote directly to the final cache file, so concurrent writes had a higher corruption risk.Fix
Serialize in-process cache writes and make update-check cache writes atomic.
Changes
lib/quota-cache.tssaveQuotaCache()lib/auto-update-checker.tsEBUSY/EPERMfailurestest/auto-update-checker.test.tsValidation
note: greptile review for oc-chatgpt-multi-auth. cite files like
lib/foo.ts:123. confirm regression tests + windows concurrency/token redaction coverage.Greptile Summary
serializes in-process cache writes for both
quota-cacheandauto-update-checkerusing a module-level promise queue with temp-file + rename for atomicity. thequota-cacherefactor is clean.auto-update-checkerhas two minor gaps:clearUpdateCachebypasses the new queue (a queued rename can silently undo a clear), and the new code uses tabs while the rest of the file uses spaces.Confidence Score: 4/5
safe to merge after addressing the clearUpdateCache queue bypass and fixing mixed indentation
the core serialization logic is correct and well-tested; the P2 gap in clearUpdateCache is a real behavioral issue (a stale rename can silently undo a clear) but unlikely to be triggered in normal use — still worth fixing before merge per the PR's explicit serialization goal
lib/auto-update-checker.ts — clearUpdateCache queue bypass and mixed tabs/spaces indentation
Important Files Changed
Sequence Diagram
sequenceDiagram participant C1 as Caller 1 participant C2 as Caller 2 participant Q as writeQueue participant FS as Filesystem C1->>Q: saveCache() → enqueue writeTask1 C2->>Q: saveCache() → enqueue writeTask2 Q->>FS: writeFileSync(tempPath1) Q->>FS: renameSync(tempPath1 → CACHE_FILE) Q->>FS: writeFileSync(tempPath2) Q->>FS: renameSync(tempPath2 → CACHE_FILE) note over FS: writes serialized,\neach atomic via rename note over C1,FS: clearUpdateCache() bypasses Q C1->>FS: writeFileSync(CACHE_FILE, '{}') Q-->>FS: renameSync(tempPath → CACHE_FILE) races ↑Comments Outside Diff (1)
lib/auto-update-checker.ts, line 276-284 (link)clearUpdateCachebypasses the write queueclearUpdateCachewrites directly toCACHE_FILEwithout going throughupdateCacheWriteQueue. if a queuedsaveCachetask has already written its temp file but hasn't yet calledrenameSync,clearUpdateCachewrites{}to the final path — then the pendingrenameSyncimmediately overwrites it with stale data, silently undoing the clear. routing through the queue would fix this:Prompt To Fix With AI
Prompt To Fix All With AI
Reviews (1): Last reviewed commit: "fix: serialize concurrent runtime cache ..." | Re-trigger Greptile