Repository navigation
Conversation
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.
|
FYI from an independent reproduction of #9943 (Windows 11 + WinFsp, SFTP mount, 1. 2. Both are in #9975, together with an independent implementation of the same
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, Separately, #9974 is a different defect I hit while reproducing: 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. |
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.
|
I wonder if it wouldn't be more robust to rely on the fairly new os.Root API. Internally, on Windows, @ncw thoughts? |
|
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 Once this is in, #9975 will be rebased on top and keep only the |
No worries!
I went ahead and made some local changes to 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. |
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-backupload).The bug
On Windows
os.Renamefails withAccess is deniedwhen 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.Renamelogged the failure at Info level and then returned success, so the FUSE layer sawerrc=0while 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 overfoo) is exactly this. Observed with Claude Code'sEdit/Writeover an rclone SFTP mount: the tool reports success, the file silently reverts, and the next read may throwEIO. This is the scenario in the long-standing #4293.The fix (two commits)
vfs: return error when the VFS cache rename fails— stop swallowing the cache-rename error inFile.Rename; propagate it so the failure is visible instead of silently discarding data.lib/file: add Rename using Windows POSIX semantics …— newfile.Rename: on Windows it renames viaSetFileInformationByHandle+FileRenameInfoExwithFILE_RENAME_POSIX_SEMANTICS | FILE_RENAME_REPLACE_IF_EXISTS, which can replace a file that has open handles (opened withFILE_SHARE_DELETE, asfile.OpenFilealready does), matchingos.Renameon Unix. Falls back toos.Renameif the POSIX call is unsupported (pre-Windows-10-1607 or non-NTFS). Non-Windows platforms are unchanged (Rename = os.Rename).vfs/vfscacheuses it for the cache rename.Testing
TestRenameinlib/filecovers renaming over a destination that still has an open handle.go test ./lib/file/... ./vfs/vfscache/... ./vfs/...pass;gofmt/go vetclean.--vfs-cache-mode writes --vfs-write-back 30s: rapid temp-file-then-rename saves all persisted with noFile.Rename failed in Cacheerrors, 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