Skip to content

vfs/vfscache: fix silent data loss when a Windows cache rename hits an open handle - #9574

Open
vastator3 wants to merge 2 commits into
rclone:masterfrom
vastator3:fix-windows-vfscache-posix-rename
Open

vastator3 wants to merge 2 commits into
rclone:masterfrom
vastator3:fix-windows-vfscache-posix-rename

Conversation

@vastator3

@vastator3 vastator3 commented Jul 6, 2026 •

Copy link
Copy Markdown

Two commits fixing silent data loss on Windows mounts when a VFS cache rename races an open handle (e.g. an in-flight --vfs-write-back upload).

The bug

On Windows os.Rename fails with Access is denied when another handle holds the destination open. In the VFS cache this happens when a cache file is renamed while its write-back upload is still in flight. File.Rename logged the failure at Info level and then returned success, so the FUSE layer saw errc=0 while the cache file kept the old name and the just-written bytes were dropped, representing a silent data loss.

It reproduces with any app that does the download-then-rename-temp→target pattern; an editor's atomic save (write foo.tmp, rename over foo) is exactly this. Observed with Claude Code's Edit/Write over an rclone SFTP mount: the tool reports success, the file silently reverts, and the next read may throw EIO. This is the scenario in the long-standing #4293.

The fix (two commits)

  1. vfs: return error when the VFS cache rename fails — stop swallowing the cache-rename error in File.Rename; propagate it so the failure is visible instead of silently discarding data.
  2. lib/file: add Rename using Windows POSIX semantics … — new file.Rename: on Windows it renames via SetFileInformationByHandle + FileRenameInfoEx with FILE_RENAME_POSIX_SEMANTICS | FILE_RENAME_REPLACE_IF_EXISTS, which can replace a file that has open handles (opened with FILE_SHARE_DELETE, as file.OpenFile already does), matching os.Rename on Unix. Falls back to os.Rename if the POSIX call is unsupported (pre-Windows-10-1607 or non-NTFS). Non-Windows platforms are unchanged (Rename = os.Rename). vfs/vfscache uses it for the cache rename.

Testing

  • New TestRename in lib/file covers renaming over a destination that still has an open handle.
  • go test ./lib/file/... ./vfs/vfscache/... ./vfs/... pass; gofmt / go vet clean.
  • Validated end-to-end on Windows 11 + WinFsp over the SFTP backend with --vfs-cache-mode writes --vfs-write-back 30s: rapid temp-file-then-rename saves all persisted with no File.Rename failed in Cache errors, whereas the stock binary silently reverted them.

Known limitation

POSIX rename still cannot replace a target held open by a third party without FILE_SHARE_DELETE (e.g. some antivirus scanning the cache file). That case now surfaces as an error rather than silent data loss, thanks to commit 1.

Approach suggested by @ncw on the forum (make the error visible, then fix via lib/file).

Refs #4293

vastator3 added 2 commits July 6, 2026 18:56
Previously File.Rename logged a failed cache rename at Info level and then
carried on returning success. On Windows os.Rename fails if another handle
has the destination open (for example an in-flight write-back upload), so
the rename reported success to the OS while the cache file was left at the
old name - silently losing the just-written data of an atomic
temp-file-then-rename save.

Return the error from cache.Rename instead so the failure is visible and
propagated to the caller. The underlying Windows rename is fixed
separately.
…n handles

On Windows os.Rename cannot rename or replace a file that another handle
still has open, even when it was opened with FILE_SHARE_DELETE. In the VFS
cache this happens when a cache file is renamed while an in-flight
write-back upload still holds it open: os.Rename fails, and (before the
previous commit) that error was silently swallowed, losing the data of an
atomic temp-file-then-rename save made over a mount.

Add file.Rename which on Windows uses SetFileInformationByHandle with
FileRenameInfoEx and FILE_RENAME_POSIX_SEMANTICS |
FILE_RENAME_REPLACE_IF_EXISTS, so the rename succeeds over open handles,
matching os.Rename on Unix. It falls back to os.Rename if POSIX rename is
unsupported (pre Windows 10 1607 or a non-NTFS filesystem). On other
platforms Rename is os.Rename.

Use it for the cache rename in vfs/vfscache so mount renames no longer fail
on Windows when the cache file is still open.
@Kurptas

Kurptas commented Sep 25, 2026

Copy link
Copy Markdown

FYI from an independent reproduction of #9943 (Windows 11 + WinFsp, SFTP mount, --vfs-cache-mode full, v1.75.1 and current master). This PR's two commits fix the core of it and I verified the same end-to-end result with them rebuilt locally. Two gaps in the failure path, in case they are useful to fold in here:

1. vfs/vfscache/item.go (item.rename) — the cache data/metadata files are renamed after item.name/item.o have been set, and writeback.Rename(id, newName) is called even when the file move failed; Cache.rename also returns before moving its map key. So after a failed rename the item claims a name whose files do not exist, and the next write-back resolves that name and uploads the destination's stale (or empty) content over the remote — _store → fremote.NewObject/fcache.NewObject → Copied (new), remote unchanged. Renaming the files before any state mutation, and returning the error with the name/files/write-back target untouched, makes the failure lossless.

2. vfs/file.go (File.rename) — the node adopts destDir/dPath/leaf before the risky part, so even when the error is returned the node and the directory entry disagree; capturing and restoring those three on failure keeps them consistent.

Both are in #9975, together with an independent implementation of the same lib/file POSIX rename as this PR (I could not fetch this branch from the machine where I was reproducing, so I wrote it from the description). Measurements that may help the review:

build editor atomic save (read → write temp → os.replace over the target)
v1.75.1 / master, as-is save reported OK, remote kept the old content; with other timings the target was truncated 21 B → 15 B or zeroed
+ error propagation only fails loudly (EIO), target unchanged, the user's bytes survive at the temp name
+ POSIX rename as well save succeeds, target holds the new content

The failure needs the target to have been read first (an open cache handle from the handle-cache grace period): renaming over an unread target works, and after a remount it works too. Also, MoveFileEx refuses an open destination even when the handle was opened with FILE_SHARE_DELETE (verified outside rclone with CreateFileW + os.replace), so share flags alone cannot fix it — the POSIX-semantics rename is the right primitive.

Separately, #9974 is a different defect I hit while reproducing: vfs/vfscache/cache.go walk() passes an OS-encoded path to Cache.get(), so on a warm-cache remount reload() cannot find dirty items and a pending edit is silently dropped (measured: remote keeps the old content; cache accounting reads 0).

Happy to rebase #9975 on top of this PR and drop whatever it already contains, or to reduce it to a review suggestion — whichever is easier for you.

Kurptas added a commit to Kurptas/rclone that referenced this pull request Oct 5, 2026
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.
@HarmfulBreeze

Copy link
Copy Markdown

I wonder if it wouldn't be more robust to rely on the fairly new os.Root API. Internally, on Windows, os.Root.Rename calls NtSetInformationFile with FILE_RENAME_POSIX_SEMANTICS, which is pretty close to what this PR tries to accomplish, without the added clumsiness of trying to do it ourselves.

@ncw thoughts?

@ncw

ncw commented Oct 14, 2026

Copy link
Copy Markdown
Member

Thanks for this, and sorry for the wait. The diagnosis is spot on, and Kurptas has independently confirmed it fixes #9943 end to end. Making the cache rename error propagate instead of being logged is clearly right.

On @HarmfulBreeze's question: I'd rather use os.Root.Rename than our own FileRenameInfoEx call if it does the job, since the standard library then owns the Windows details. Could you check whether it succeeds against an open destination in your reproduction? The cache files are all under the cache root, so an os.Root opened there should cover it. If it doesn't work, say so and we'll keep lib/file.Rename.

Once this is in, #9975 will be rebased on top and keep only the item.rename / File.rename state fixes, so you don't need to add those here.

@HarmfulBreeze

HarmfulBreeze commented Oct 19, 2026 •

Copy link
Copy Markdown

Thanks for this, and sorry for the wait.

No worries!

Could you check whether it succeeds against an open destination in your reproduction?

I went ahead and made some local changes to cache.go to use os.Root.Rename, and I asked Copilot to write a test case for me that I verified afterwards. It turns out using os.Root.Rename did fix the issue!

The test case can be found here: https://gist.github.com/HarmfulBreeze/83377a5567db3d6ee8b63b7b1ab14e23

I won't share the code as-is because it's a little dirty and I'm not too proud of it, but I can write a PR if needed.

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.

4 participants