upstream-sync: atomic writes for checkpoint and dev-cache files#31
Conversation
Write to a sibling .tmp file then rename so a process kill between truncation and completion cannot leave a corrupt or zero-length file. std::fs::rename is atomic on POSIX (single syscall), matching the behaviour of Python os.replace used in upstream Scrapling v0.4.9 (PR #344). https://claude.ai/code/session_01WyJXoeiQoH8fRW4DBx7HQq
|
Warning Review limit reached
More reviews will be available in 57 minutes and 45 seconds. Learn how PR review limits work. Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file). ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits. 🚦 How do rate limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThis PR adds atomic write patterns to two persistence operations: response cache and checkpoint management. Both now write to temporary files and rename them to their final destinations, preventing partially written or corrupted files if a process crashes mid-write. ChangesAtomic write safety for cache and checkpoint
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
Address Repo Guardian review (#43) on the atomic-write change: - std::fs::rename does not atomically replace an existing file on Windows (MoveFileW fails with ERROR_ALREADY_EXISTS), so the cache/checkpoint overwrite path would break on windows-latest. Use tempfile::NamedTempFile::new_in(dir) + persist(target), which maps to MoveFileExW(REPLACE_EXISTING) on Windows and rename(2) on POSIX. - NamedTempFile auto-removes the temp file on drop, so a failed persist no longer leaves orphaned .tmp files, and the random suffix avoids collisions between concurrent writers of the same target. - Promote tempfile from dev-dependencies to dependencies. - Add roundtrip + overwrite + cleanup tests for both ResponseCache and CheckpointManager (review Issue 4). Closes #43 https://claude.ai/code/session_012RmdaovmNWZVAim4XxCWwn
Upstream reference
Ported from D4Vinci/Scrapling v0.4.9 – PR #344
"fix(spiders): use os.replace for atomic checkpoint/cache writes on Windows"
(merged 2026-06-07)
Problem
Both
ResponseCache::putandCheckpointManager::saveusedstd::fs::write()directly on the target path.std::fs::writetruncates the file then fills it, so a process kill (SIGKILL, power loss, OOM) between those two operations leaves a zero-byte or half-written file. On the next run this silently fails to parse, losing the checkpoint or corrupting the dev cache.Fix
Write the serialized data to a sibling
.tmpfile, then callstd::fs::renameto atomically replace the target. On POSIX,rename(2)is a single syscall — the target either contains the old content or the new content, never a partial write.This is the direct Rust equivalent of Python's
os.replace()used in the upstream fix.Files changed
src/spiders/cache.rsResponseCache::put— atomic write via tmp+renamesrc/spiders/checkpoint.rsCheckpointManager::save— atomic write via tmp+renameTesting
cargo check✅cargo test✅ — all 175 tests pass, no regressions.Generated by Claude Code
Summary by CodeRabbit