Skip to content

vfs: keep cache item and node state consistent when a cache rename fails - #9975

Open
Kurptas wants to merge 2 commits into
rclone:masterfrom
Kurptas:pr/state-consistency
Open

Kurptas wants to merge 2 commits into
rclone:masterfrom
Kurptas:pr/state-consistency

Conversation

@Kurptas

@Kurptas Kurptas commented Sep 25, 2026

Copy link
Copy Markdown

What this fixes (on top of #9574 / for #9943)

This is the same class of bug as #9943 (Windows: an editor's atomic save = write temp + rename over the target silently loses or truncates data). PR #9574 fixes the two central pieces: the failure in vfs/file.go is now returned instead of logged, and lib/file gained a Windows Rename with POSIX semantics that can replace a destination held open with FILE_SHARE_DELETE.

This PR adds the state-consistency parts that the error path still needs, plus one unrelated defect found while reproducing.

1. vfs/vfscache/item.go: the cache files must be renamed before any state is mutated.
Today item.rename sets item.name, item.o and the fingerprint first, then renames the data/metadata files, and the failure path re-targets the pending write-back (writeback.Rename(id, newName)) even when the file move failed. Cache.rename also returns before moving the map key. So after a failed rename the item claims a name whose files do not exist: the next write-back resolves the destination by name (_store → fremote.NewObject/fcache.NewObject) and uploads the destination's stale content over the remote, which is how the data loss is produced (unpatched log: File.Rename failed in Cache: ... Access is denied → Copied (new) → upload succeeded try #1, remote unchanged). This commit renames the files first and returns the error with item.name, item.o, the map key and the write-back target all untouched.

2. vfs/file.go: roll the node back when the rename fails.
The node adopts destDir/dPath/newName before the risky part, so even when the error is returned the node and the directory entry disagree. Capture f.d/f.dPath/f.leaf and restore them on failure.

3. Independent defect: a pending edit is dropped when an existing cache is re-walked — walk() passes an OS-encoded path to Cache.get(), so reload() cannot find dirty items and never resumes them (repro and fix in the first commit of this branch; happy to split it into its own PR if you prefer).

Verification

Windows 11 + WinFsp 2.2.26215, SFTP remote (rclone serve sftp --local-encoding Raw as a name-preserving stand-in for a NAS), WinFsp mount, --vfs-cache-mode full; the save action is exactly what an editor does (read → write temp → os.replace over the target).

build editor atomic save dirty edit across a hard kill + remount ASCII in-place fullwidth-name in-place
v1.75.1 data loss (save reported OK, remote kept the old content) edit lost ok ok
+ these two vfs commits (lib/file not touched) fails loudly (EIO), target unchanged, user's bytes preserved ok ok ok
+ lib/file POSIX rename (= #9574 commit 2) save succeeds ok ok ok

Also reproduced on current master. go build, go vet, gofmt and go test ./vfs/... ./lib/file/... ./lib/encoder/... are clean.

Relationship to #9574

Commits 2–3 of this branch are equivalent in intent to #9574's two commits (which I could not fetch from the machine where this was developed, hence the independent implementation); the parts you will not find there are the item/node state consistency above and the reload fix. I'm happy to rebase this on #9574 once it lands and drop whatever it already contains, or to reduce this to a review suggestion if that is easier.

@Kurptas

Kurptas commented Sep 25, 2026

Copy link
Copy Markdown
Author

Adding the third symptom variant: the remote file is deleted, not zeroed

While preparing the state-consistency report I found that this defect can also produce a different, more alarming symptom than the "revert / truncate / zero byte" family: the file disappears from the remote entirely.

The save pattern that does it is the one many editors (and the foobar2000 case in #9943) use:

  1. read the target (the editor opens it -> the VFS caches it and keeps the cache file open for the handle-caching grace period)
  2. write the new content to a temp file
  3. unlink(target) — "delete the original"
  4. rename(temp, target)

Step 3 is not deferred: vfs/file.go:692 calls f.o.Remove(ctx) synchronously, so the remote object is deleted right there (:686 also drops the cache item). If step 4 then fails — which on Windows it always did before this PR, because os.Rename/MoveFileEx cannot replace the destination's still-open cache file — nothing ever replaces it. The remote file is gone, and the only copy of the new content is the temp file in the cache, whose cache name is the OS-encoded spelling (e.g. 报告‛:2026.txt) — i.e. the user sees "a file with a strange character appeared in the cache folder" while the original vanished.

Measured on Windows 11 + WinFsp 2.2.26215, SFTP mount with --vfs-cache-mode full, remote file 报告:2026.txt containing ORIGINAL, steps 1–4 above, then waiting for the write-back:

build remote after the save mount listing log
v1.75.1 original gone, only tmp-edit.txt remains original missing File.Rename failed in Cache: failed to rename in cache: ... Access is denied
this PR 报告:2026.txt = EDITED-BY-USER (same name, new content) the single file Copied (new), no failure markers

So this PR covers that variant too. vfs/file.go:692 is of course intended behaviour — a mount has to delete the remote object when the application deletes it; what was wrong is that the replacement step failed silently (vfs/file.go:276 logged at Info and returned nil) while item.name and the write-back target had already been changed (vfs/vfscache/item.go:1549-1552, :1569), so the write-back could upload the destination's stale content. That is the "revert/truncate/zero" family in #9943; the delete case is the one where the file simply disappears, and it also explains why users report that only the file they saved went missing (the other one never went through steps 3–4).

On Windows os.Rename (MoveFileEx) cannot replace a destination that is open,
even when the open handle shares delete, and with --vfs-cache-mode full the
target of an atomic temp-file rename keeps its cache file open (its Item holds
the fd for the handle cache grace period).  The cache rename then failed with
Access is denied.

File.Rename only logged that at Info level and returned success, so the
application saw the save succeed while the cache kept both files: the
destination's stale data stayed at the destination name and the newly written
data stayed at the temporary name.  The re-targeted write back then uploaded
the destination's stale (or empty) data over the remote, silently losing the
user's new content (truncated or reverted file).  See
rclone#9943.

Fail the rename instead:

  * vfs/file.go: propagate the cache rename error to the caller and to the
    application, and put the file's directory/leaf back so the node stays
    consistent with the unchanged directory entry.
  * vfs/vfscache/item.go: perform the cache file renames before mutating any
    item state so that a failure leaves the item's name, its cache files and
    its pending write back target untouched (the pending data is then uploaded
    under its own name rather than overwriting the destination).

The real fix for atomic saves over an open cache file is a Windows POSIX
semantics rename (see PR rclone#9574); this change makes the failure safe in the
meantime.
Windows os.Rename uses MoveFileEx which fails with Access is denied when
either path is open, even when those handles were opened with
FILE_SHARE_DELETE (measured), and it cannot be fixed with share flags.

The VFS cache deliberately keeps cache files open (file.OpenFile opens with
FILE_SHARE_DELETE and the Item keeps the fd for the handle cache grace
period), so an application doing the usual atomic save (write a temporary
file then rename it over the target - editors, browsers, tools such as
foobar2000) fails in the cache rename; before the previous commit that
failure was hidden and silently lost the new data (issue rclone#9943).

Add file.Rename: on Windows it renames with SetFileInformationByHandle and
FileRenameInfoEx using FILE_RENAME_REPLACE_IF_EXISTS|FILE_RENAME_POSIX_SEMANTICS
which can replace a destination which has open handles, falling back to
os.Rename when that is not supported.  On other platforms it is os.Rename.

Use it for the cache rename in vfs/vfscache (the only os.Rename there).
A destination held open by a handle without FILE_SHARE_DELETE (eg some
antivirus scanners) still cannot be replaced, but that now surfaces as an
error rather than silent data loss.

New tests: lib/file/file_windows_test.go covers renaming over an open
destination and an open source (both must succeed) and the no-share-delete
case (must fail, leaving the destination untouched).
@Kurptas
Kurptas force-pushed the pr/state-consistency branch from 323fa3a to 46992a1 Compare October 5, 2026 06:43
@Kurptas

Kurptas commented Oct 5, 2026

Copy link
Copy Markdown
Author

Rebased onto current master (branch is now 2 commits ahead); no code changes.

The lint failure in the earlier run — backend/crypt/cipher.go:157: unexported-return (revive) — was upstream code, already fixed on master since. The linux/windows test failures in that run were against the 2026-09-26 base.

Local checks after the rebase: go vet ./lib/file/ ./vfs/ clean, go test ./lib/file/ passes (includes the Windows rename-over-open-destination cases).

The workflow runs for this push are action_required (first-time contributor), so they need an approval before they execute.

@ncw

ncw commented Oct 14, 2026

Copy link
Copy Markdown
Member

Hi @Kurptas, thanks for rebasing, and for the extra write-up of the unlink-then-rename variant. Thanks also for offering to rebase onto #9574: yes please. We'll land #9574 first. Then please drop the duplicated lib/file and error-propagation parts, so this PR is just the item.rename reordering and the File.rename rollback.

The earlier review points still apply to those parts:

  1. In item.rename, if the data file rename succeeds but the metadata rename fails, we return an error with the data file already at newName while the item still has the old name. Please rename the data file back in that case.
  2. In File.rename, when f.o != nil the remote operations.Move has already succeeded before the cache rename runs. Rolling the node back to the old path then leaves it disagreeing with the remote, and f.o still points at the moved object. Only roll back when nothing has changed on the remote yet, or handle the "remote moved, cache didn't" case separately. Please coordinate this with vfs: fixes corrupt file identity on backend rename failure #10060, which adds a rollback for the backend-rename failure in the same function, so there's one restore path.
  3. Please add tests that run on Linux. The vfscache rename() helper refuses a destination that isn't a regular file, so putting a directory at the destination's cache path makes Cache.Rename fail. The test can then check that the item keeps its name, files and map key, and that the File node keeps its old Path().
  4. Please keep issue numbers out of code comments; they belong in the commit message (see AGENTS.md).
  5. Please update the PR description to match the branch. The walk()/reload() fix is now vfs/vfscache: use the remote name when reloading the cache #9974.

Thanks again for the careful work on this.

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.

2 participants