Skip to content

Hold a file not fully on disk before replacing it; clear leftover test drives - #11

Merged
dkgs2000 merged 2 commits into
mainfrom
claude/lock-partial-files-and-clean-test-drives
Nov 26, 2026
Merged

dkgs2000 merged 2 commits into
mainfrom
claude/lock-partial-files-and-clean-test-drives

Conversation

@dkgs2000

Copy link
Copy Markdown
Owner

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), UpdateFromRemote replaced it through a WRITE_DAC handle 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.Unregister sometimes 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. Unregister reported success anyway. A test host that crashes never reaches its cleanup either. The leftover entries piled up on the developer's PC.

The fix

  1. Before replacing a file that isn't fully on disk, a plain handle that shares nothing and has DELETE access now holds it (WithSoleHandle). Requesting DELETE is 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.
    • Nextcloud: registers with CF_HYDRATION_POLICY_FULL and sends whole files, so it never checks for partly downloaded files. For updates it uses CreateFile(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.
    • Difference from the exclusive Cloud Files handle: while the file is held (milliseconds), another program's open fails with "in use" instead of waiting.
  2. Unregister now 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. Success keeps 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_RefusesAFileNotFullyOnDiskWhileOpenInAnotherProgram runs 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.slnx with 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.slnx passes

  • A 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 Unregister leaves an entry. The Nextcloud comparison comes from reading its cfapiwrapper.cpp, vfs_cfapi.cpp and propagatedownload.cpp.

🤖 Generated with Claude Code

dkgs2000 and others added 2 commits November 26, 2026 21:48
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>
@dkgs2000
dkgs2000 merged commit e534645 into main Nov 26, 2026
6 checks passed
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>
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