The page-in after CloseHandle
git init failed on every Windows mount on builds before the fix. Not
sometimes, not on large repos - every time, on a drive that looked healthy,
with a fatal in-page error.
The cause was a memory-mapped read that arrived after the application had already closed the file handle. The fix is small once you see the sequence: park the handle at Cleanup, unpark it at Close, and serve page-ins from the parked state. The two bugs around it are a lesson in Windows filesystem semantics.
What git does#
Git does not read .git/config with a simple read(2) and close it. It maps
the file, reads the map, and closes the descriptor while the mapping is still
live. On Windows, closing the last handle does not immediately end the
section object: the mapping keeps issuing page-ins until the mapped view is
unmapped (IRP_MJ_CLOSE).
A normal filesystem serves those page-ins from the file object, which remains
valid for the mapping. A userspace filesystem that ties its read state to the
handle id has to keep that state alive too. We did not. WinFsp delivered the
late page-ins as read_async under the released handle id. We looked up
the handle, found nothing, and failed the page-in with STATUS_INVALID_HANDLE
- a fatal in-page error. Git saw a broken .git/config and git init died
on every mount.
The sequence is deterministic, which is why the failure was total:
- git opens
.git/configand reads it. - git maps the file.
- git closes the descriptor - our Cleanup drops the handle.
- git reads through the map - the kernel issues a page-in under the old handle id.
- our adapter rejects it, the page faults fatally, git aborts.
The fix#
The adapter has three phases for a handle: Cleanup, Close and the read path. The fix is one sentence in each:
- Cleanup does not drop the read state. It parks the handle in a
paging table (
park_paging_handle), keyed by the handle id the kernel will still use. - Close unparks it, because after
IRP_MJ_CLOSEno further page-ins can arrive for that mapping. core_read_into_asyncfalls back to the parked read state on a live-handle miss. A read that lost the race with Close still finds its bytes.
The park is read-only. A page-out of a writable mapping after Close still
fails rather than bypassing the released editing lock: serving it would let a
closed writer keep mutating the file behind the lock manager's back. The
adapter logs once per handle and names the remedy (paging_write_warned)
instead of silently accepting the write.
The mount-loss and branch-switch drains park before releasing handles, so a
hot switch cannot re-arm the crash while a mapping is still live. The close
unpark is the only place a handle leaves the paging table on the normal path;
a leaked park is a handle that never got Close, which is a different bug.
The second Windows bug: the owner SID#
Fixing the page-in let git init get further, where it hit "detected dubious
ownership" and refused every command in the repository. The projected
security descriptor's owner was not the engine's own user; it was a foreign
SID. Git's ownership check is doing its job - a repository owned by someone
else is not trustworthy - so the drive had to project the engine's own user
SID as the owner (mount_owner_sid), falling back to SY only if the token
query fails.
The injected local-access grant stays AU (LOCAL_ACCESS_ACE), and that
is deliberate. An AD-linked org projects a member's real AD SID into its ACEs;
using the user SID as a placeholder would make the adapter have to match and
strip it as "ours" on Explorer write-back, which would delete a genuine ACE
for that same person if one existed. Owner and grant are different questions;
the fix treats them differently.
How it is pinned#
The Windows keeper lane (tests/integration/windows-e2e/win-git-mmap.ps1)
does the real sequence on a throwaway hub: an mmap page-in that outlives
CloseHandle, a FILE_APPEND_DATA write, then init/add/commit/log/
status with an init wall-clock bound. status specifically reads
.git/info/exclude, which needs an enumerable .git directory - the shape a
listing-hidden .git broke. The lane runs weekly and on dispatch in
windows-keepers.yml, on the shared Windows box, with only its own engine
killed by path.
The same lane holds a live mapping across a hot branch switch (the drain smoke) and checks the mount owner SID. Those are separate assertions because they are separate failures: a page-in that outlives a handle, a mapping that outlives a branch switch, and a repository whose owner is not the user are three different ways for "the drive works" to be false.
The lesson#
On Windows, a closed handle is not necessarily the end of the file object.
A userspace filesystem has to model the phases the kernel models - Cleanup,
Close, the last section reference - or it will fail the reads that arrive in
between. The page-in after CloseHandle is not an edge case; it is how git
reads its config.
The Windows support surface is in the filesystem guide, and the shared drive rules (versions, locks, the stale-save refusal) are in working with files.