Repository navigation
Hold a file not fully on disk before replacing it; clear leftover test drives - #11
Merged
Merged
Conversation
A file that is online-only or partly downloaded can hold local writes: a program can write to it without reading the rest, and nothing is downloaded. UpdateFromRemote replaced such a file through a WRITE_DAC handle that keeps no one out, so a file another program had open was replaced under it, and that program's unsaved writes could be lost. Now it is checked and replaced while a plain handle that shares nothing and has DELETE access holds it (DELETE is what makes Windows check sharing). It is refused while another program has the file open, as a file fully on disk is, and downloads nothing. A Cloud Files handle, exclusive or not, would download the whole file first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ftovers StorageProviderSyncRootManager.Unregister now and then returns, or throws "Element not found", while the drive's Explorer entry stays: about 7 in 400 times with drives registered and removed side by side, as the contract tests do. Unregister reported success anyway, and the entries piled up. Unregister now checks that the entry is gone and asks again (an immediate retry removed them), up to three times, and says so in its message if it stays. Success means what it did before, so moving the drive to another folder isn't blocked by a leftover entry. A test host that crashes never reaches its cleanup either. So before any test runs, the test assembly removes the drives earlier runs left: only in the contract tests' %TEMP%\opencfdrive-<test>\<id> folders, and only once the folder is gone or untouched for an hour, so a test run going on beside it keeps its own. The end-to-end suite's drives and the user's own are never touched. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 26, 2026
dkgs2000
added a commit
that referenced
this pull request
Nov 26, 2026
) * Register FULL hydration so a read returns only once the file is downloaded The drive registered PARTIAL hydration, which Microsoft documents as not supported and which Windows runs as PROGRESSIVE. A read was let through as soon as its bytes arrived and Windows finished the download a moment later; a write in between could lose the downloaded bytes, so the file read back as zeros followed by what was written (the intermittent UpdatePlaceholderContractTest failure on CI). It hid on a PC because Defender downloads a file when a program opens it; CI runners have it off. FULL is what Nextcloud and OneDrive register. * Say why a file not fully on disk is still held before it is replaced With FULL, a program's write to a file that is not fully on disk downloads all of it first, so such a file holds nothing local. The hold from #11 stays: a program that has the file open can have a write waiting for that download, which could land between the check and the replace. #11's test now holds the file open without writing, and AWrite_ToAFileNotFullyOnDisk_DownloadsAllOfItFirst pins the download. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem
1. A file another program has open could be replaced. When the server changes a file that isn't fully on this PC (online-only, or a download that broke off),
UpdateFromRemotereplaced it through aWRITE_DAChandle that keeps no one out. Such a file can still hold local writes: a program can write to it without reading the rest, and nothing is downloaded. So a program's unsaved writes could be lost. Fully downloaded files were already safe: they are replaced only through an exclusive handle.2. Test drives were left registered with Windows.
StorageProviderSyncRootManager.Unregistersometimes returns, or throws "Element not found", while the drive's Explorer entry stays. In 400 register-and-remove cycles run 8 at a time, this happened 7 times.Unregisterreported success anyway. A test host that crashes never reaches its cleanup either. The leftover entries piled up on the developer's PC.The fix
DELETEaccess now holds it (WithSoleHandle). RequestingDELETEis what makes Windows check sharing at all. While the file is held, its size and write time are checked and it is replaced. If another program has the file open, the hold is refused and the next sync tries again, as for a fully downloaded file. Nothing is downloaded. A Cloud Files handle (CfOpenFileWithOplock) would download the whole file first, even when exclusive and without write access, so it can't be used here.CF_HYDRATION_POLICY_FULLand sends whole files, so it never checks for partly downloaded files. For updates it usesCreateFile(path, 0, 0, ...). That handle requests no access at all, so it keeps no one out: on a plain file, it opens while another program has the file open for writing. OneDrive's code isn't public. So this goes beyond Nextcloud.Unregisternow checks that the Explorer entry is gone, and asks again right away if it isn't, up to three times. In the test above, an immediate retry removed the entries. If the entry still stays, the message says so.Successkeeps its meaning, so moving the drive to another folder isn't blocked by a leftover entry. Before any test runs, the test assembly removes drives earlier runs left (LeftoverTestDrives). It only removes drives in the contract tests'%TEMP%\opencfdrive-<test>\<id>folders, and only once a folder is gone or untouched for an hour. The end-to-end suite's drives and the user's own are never touched.Tests
New
UpdateFromRemote_UnchangedFrom_RefusesAFileNotFullyOnDiskWhileOpenInAnotherProgramruns for an online-only file and for a broken-off download. It fails on main in both cases: the update returns success while another program has the file open. It passes with the fix and downloads nothing.New
TestDriveCleanupTests: the retry until the entry is gone, giving up after three tries, and which folders count as test drives.Locally:
dotnet test .\OpenCfDrive.slnxwith coverage and rclone, 294 passed. The cloud E2E suite on WebDAV passed all 68 scenarios. After rebasing on main, the affected tests passed again (27).The cleanup removed this PC's 6 leftover test drives from
HKLM\...\SyncRootManager. Only the real drive is left.dotnet test .\OpenCfDrive.slnxpassesA test fails without this change (bug 2 is a 2% race; its retry logic is unit-tested instead)
Docs updated (
docs/architecture.md,docs/testing.md)AI use
Written with Claude Code. Both problems were found and checked with temporary experiments against the real Cloud Files API (not committed): which handles download a file, which are refused while it's open, and how often
Unregisterleaves an entry. The Nextcloud comparison comes from reading itscfapiwrapper.cpp,vfs_cfapi.cppandpropagatedownload.cpp.🤖 Generated with Claude Code