Skip to content

fix: restore the tail data file when a reopen's local write fails - #491

Open
thweetkomputer wants to merge 1 commit into
mainfrom
fix/download-to-exist-restore
Open

fix: restore the tail data file when a reopen's local write fails#491
thweetkomputer wants to merge 1 commit into
mainfrom
fix/download-to-exist-restore

Conversation

@thweetkomputer

Copy link
Copy Markdown
Collaborator

Problem

A cloud reopen syncs the tail data file via DownloadFile(download_to_exist=true), which renames the existing file aside to .tmp before rewriting it in place. If the local WriteFile then fails (e.g. cache-disk ENOSPC), there was no rename-back — the file was left stranded as .tmp:

  • its committed pages became briefly unreadable (a read that faults to disk gets ResourceMissing, which an auto-reopen then heals), and
  • its used_local_space_ charge was orphaned while the eventual re-download charged again, inflating cache accounting until restart.

Fix

Track whether the existing file was actually moved aside (moved_existing) and, on WriteFile failure, rename it back before returning the error.

  • Safe: WriteFile only writes bytes at/after offset — the not-yet-installed tail that no live snapshot maps — so the reader-visible prefix is byte-for-byte intact.
  • Restore, not discard: the file holds committed pages current readers need; unlinking/stranding it would break those reads until an auto-reopen, whereas restoring keeps committed reads working (the replica just stays consistent-but-stale until the next reopen re-syncs).
  • The final tmp→filename rename is trusted (a directory op that only fails on hardware).

Test

A cloud [reopen] test arms a WriteFile fail point during a reopen's tail sync and asserts no .tmp is left stranded in the partition directory (exactly one is stranded without the fix). Reads alone don't surface the bug — the committed pages stay in the buffer-pool cache and a cold-read fault self-heals via auto-reopen — so the regression is verified at the filesystem level. Full cloud suite passes (218 assertions).

A cloud reopen syncs the tail data file via
DownloadFile(download_to_exist=true), which renames the existing file
aside to .tmp before rewriting it. If the local WriteFile then fails
(e.g. cache-disk ENOSPC), the file was left stranded as .tmp: its
committed pages became briefly unreadable (ResourceMissing before an
auto-reopen heals it) and its used_local_space_ charge was orphaned,
inflating cache accounting until restart.

Track whether the existing file was moved aside and, on write failure,
rename it back before returning the error. This preserves the committed
prefix -- WriteFile only touches bytes at/after offset, i.e. the
not-yet-installed tail no live snapshot maps, so the reader-visible
prefix is intact. Restoring (rather than discarding) is required because
the file holds committed pages current readers need. The final
tmp->filename rename is trusted (a directory op that only fails on
hardware).

Regression: a cloud [reopen] test arms a WriteFile fail point during a
reopen's tail sync and asserts no .tmp is left stranded in the partition
directory (one is stranded without the fix).
@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@thweetkomputer, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 34 seconds

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 reviews.

How do review 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 refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 86a56fe1-bc75-4081-a86d-c6a666d91d14

📥 Commits

Reviewing files that changed from the base of the PR and between 51dc37d and 850e9e0.

📒 Files selected for processing (2)
  • src/async_io_manager.cpp
  • tests/cloud.cpp
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/download-to-exist-restore

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

Comment @coderabbitai help to get the list of available commands.

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.

1 participant