Skip to content

fix(native/windows): share FILE_SHARE_DELETE so owned objects can be marked delete-pending - #2445

Merged
morluto merged 3 commits into
morluto:mainfrom
maodns:fix/windows-native-delete-share-mode
Oct 13, 2026
Merged

morluto merged 3 commits into
morluto:mainfrom
maodns:fix/windows-native-delete-share-mode

Conversation

@maodns

@maodns maodns commented Oct 13, 2026

Copy link
Copy Markdown

Problem

The Windows native authority self-test (binding.call("inspect", [])) fails closed
on stock Windows x64 under a non-elevated account, so every provider-backed
operation is rejected
and the whole native authority is unusable:

REA Windows native controls are unavailable: Delete owned object by handle failed [...]
(Win32 5): Access is denied.

Reproduced deterministically on Windows 11 x64 (Node 22.22.2, non-admin):
inspect failed 15/15 — 100%, not intermittent.

Existing reports for this exact failure, both still open: #1701, #2196.
PR #1826 (merged) fixed the symptom — the cached failure plus leaked
rea-native-probe-* directories — but not the root cause; the addon still fails
here after that fix.

Root cause

Windows requires that every handle currently open on an object grants
FILE_SHARE_DELETE before that object can be marked delete-pending via
SetFileInformationByHandle(FileDispositionInfo). Two sites violate this:

  1. createRelative() creates the runtime object with FILE_SHARE_READ only,
    and the returned handle is retained for the runtime's whole lifetime
    (Runtime::directory). closeRuntime() then has to mark that same object
    delete-pending — but the retained handle never shared DELETE, so the
    transition returns Win32 5.

  2. closeRuntime()'s child handles have the same defect: they request
    DELETE access while sharing only FILE_SHARE_READ, so the subsequent
    dispose() call on those very handles is denied too.

Fix

Add FILE_SHARE_DELETE to both share masks:

  • createRelative() — FILE_SHARE_READ → FILE_SHARE_READ | FILE_SHARE_DELETE
  • closeRuntime() child open — same

Deliberately not adding FILE_SHARE_WRITE: the write-exclusion behaviour of
the retained leases is preserved (the in-code comments at the snapshot and
read-open sites document that exclusion as intentional), while the delete-pending
transition the cleanup path depends on becomes possible. Only the DELETE share bit
is required by the root cause.

--- a/native/windows/src/filesystem.cc
+++ b/native/windows/src/filesystem.cc
@@ -165,7 +165,8 @@ static Handle createRelative(HANDLE parent, const std::wstring& name, DWORD acce
   const auto status = NtCreateFile(&raw, access | SYNCHRONIZE, &attributes, &observation, nullptr,
-                                   FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, FILE_CREATE,
+                                   FILE_ATTRIBUTE_NORMAL,
+                                   FILE_SHARE_READ | FILE_SHARE_DELETE, FILE_CREATE,
                                    FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_REPARSE_POINT |
                                    (directory ? FILE_DIRECTORY_FILE : FILE_NON_DIRECTORY_FILE), nullptr, 0);
@@ -369,7 +370,7 @@ void closeRuntime(Runtime& root) {
     Handle child(CreateFileW((L"\\\\?\\" + path).c_str(), DELETE | GENERIC_READ | FILE_WRITE_ATTRIBUTES,
-                            FILE_SHARE_READ, nullptr, OPEN_EXISTING,
+                            FILE_SHARE_READ | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING,
                             FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, nullptr));

Verification

All of the following were run through the real loader path
(WindowsNativeLoader + the manifest.json SHA-256 check), not by calling the
addon directly:

  • inspect passes; jobMemoryLimit / privateDacl / atomicJobAssignment
    all report true.
  • 10 consecutive inspect calls leave zero residual rea-native-probe-*
    directories.
  • Full runtime lifecycle succeeds end-to-end: runtime_create → runtime_mkdir →
    runtime_write → runtime_open → read (data read back correctly) → close →
    runtime_close, with no residual directory.
  • runtime_snapshot (async) resolves with a correct SHA-256.
  • A real analysis run on a large application completes all stages.

Note on the build this was verified with

Verified with a locally cross-compiled addon (llvm-mingw, Node headers 22.19.0).
Because the local build tracks main while the installed npm package is 6.4.0,
inspectionSchema in dist/windows/WindowsNativeLoader.js needed
jobMemoryLimit: z.literal(true).optional() to accept the newer artifact. That is
a packaging/version-skew concern only and is not part of this patch — the
schema fix belongs to whichever branch adds that field.

…marked delete-pending

The Windows native authority self-test (`binding.call("inspect", [])`) fails
closed on stock Windows x64 with a non-admin user:

    Delete owned object by handle failed (Win32 5): Access is denied.

Reproduced deterministically (15/15 failures) on Windows 11 x64, Node 22.22.2,
non-elevated account. Because `inspect` is a hard gate, every provider-backed
operation is rejected and the whole native authority is unusable.

Root cause: Windows requires that every handle currently open on an object
grants FILE_SHARE_DELETE before that object can be marked delete-pending via
SetFileInformationByHandle(FileDispositionInfo).

`createRelative()` creates the runtime object with FILE_SHARE_READ only, and the
returned handle is retained for the lifetime of the runtime
(`Runtime::directory`). `closeRuntime()` later needs to mark that same object
delete-pending, but the retained handle never shared DELETE, so the transition
is denied (Win32 5).

The child handles opened by `closeRuntime()` for recursive cleanup have the same
defect: they request DELETE access while sharing only FILE_SHARE_READ, so the
subsequent `dispose()` call on those very handles is denied too.

Fix: add FILE_SHARE_DELETE to both share masks, keeping FILE_SHARE_READ and
deliberately not adding FILE_SHARE_WRITE, so the existing write-exclusion
behaviour of the retained leases is preserved while the delete-pending
transition the cleanup path depends on becomes possible.

Verified end-to-end after the change: `inspect` passes, the full runtime
lifecycle (`runtime_create` / `runtime_mkdir` / `runtime_write` / `runtime_open`
/ `runtime_close`) completes with no residual directories, and a real 925 MB
application analysis (4396 files) runs through all stages.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@morluto

morluto commented Oct 13, 2026

Copy link
Copy Markdown
Owner

@greptileai review

@greptile-apps

greptile-apps Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[High impact] The PR appears safe to merge; the previous cleanup-retry test failure is addressed.

Summary

The PR allows delete sharing for owned directories while preserving retained files against replacement. Cleanup now walks through directory handles rather than saved paths.

  • Windows runtime cleanup follows owned handles and can mark its tree for deletion.

Reviews (3) · Last reviewed commit: "test(windows): assert cleanup sharing fa..." · Reviewed by Greptile

Comment thread native/windows/src/filesystem.cc Resolved
Comment thread native/windows/src/filesystem.cc Resolved
Comment thread native/windows/src/filesystem.cc Resolved

@Rinkia Rinkia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verification from a Windows host where the #1701 failure does not reproduce, so this is a regression check rather than a confirmation of the fix: the native lane passes on this PR's base and fails on its head.

Environment

Windows 11 Pro 26100 x64, Node v24.16.0, 12 logical processors, non-elevated, Defender real-time on, no third-party AV, C: NTFS fixed. Addon cross-built with the documented lane (Linux + MinGW-w64 g++ 12 in a container, since this host has no C++ toolchain). Same host, same fixture, same command in both runs — only the addon differs.

A/B

artifact SHA-256 verify-windows-native controls.cleanupFailureRetainsOwnershipForRetry
base ca2d15b 1eecd8e6…01219e ok: true, 20/20 filesystem controls, 11/11 process observations true
head c81b4a8 a1b9137b…eb935c fails assertion thrown

Command, identical in both:

node scripts/verify-windows-native.mjs . <abs>\process-boundary.exe

npm run verify:windows-native:artifact reports ok: true for both artifacts, and binding.call("inspect", []) passes 30/30 with zero leaked rea-native-probe-* directories on this head — so the self-test itself is unaffected here.

The failure

AssertionError [ERR_ASSERTION]: The input did not match the regular expression
/owned cleanup object|Delete owned object/u. Input:

'Error: Open private object relative to its directory handle failed
 [...\rea-native-conformance-5njYKd\cleanup-retry-...\snapshot.bin] (Win32 32):
 The process cannot access the file because it is being used by another process.'

  at #remove (dist/windows/WindowsPrivateRuntime.js:120:24)
  at scripts/verify-windows-native.mjs:442

constraint: 'Open private object relative to its directory handle failed', win32Code: 32.

That scenario holds a reader on snapshot.bin and expects cleanup to fail at the disposition step, retaining ownership for retry with a Delete owned object-shaped constraint. On this head it fails earlier, at the open step, with ERROR_SHARING_VIOLATION: a cleanup open that requests DELETE can no longer be granted while the existing reader does not share delete, so it is refused outright instead of opening and being denied at SetFileInformationByHandle.

So the observable change is the failure mode of cleanup while a reader is live, not just a stale expectation. native/windows/README.md states that failed removal retains the root and ancestor leases for retry and that Windows failures retain their constraint, and the lane asserts that specific constraint, so this looks like a behavioural change worth making deliberate either way — scoping the cleanup child open's share mode, or changing the documented expectation on purpose.

Note this PR has no completed checks yet, so the Windows capability and package lane has not exercised it; that lane runs npm run verify:windows-native and should reproduce the above once a maintainer approves the run.

On the root cause

One data point against the stated mechanism, not against the fix: on this host SetFileInformationByHandle(FileDispositionInfo) succeeds on a directory whose sole handle was created with FILE_SHARE_READ and no FILE_SHARE_DELETE — 30/30 through the addon, and in an isolated CreateFileW replication, including while a second non-sharing reader is held open. Details in #1701. Combined with @liudong-geek's matrix, which shows the opposite on their host, that suggests share-delete is necessary but not sufficient and the enforcement is host-dependent — which does not undermine adding it, but may mean this fix needs confirmation on an affected host rather than only a passing one.

Happy to re-run the lane on any revision; the cross-build lane here takes a few minutes.

@morluto

morluto commented Oct 13, 2026

Copy link
Copy Markdown
Owner

Review findings addressed at their producing boundaries and consumers. Latest changes were reviewed from source; tests and provider runs were skipped at the maintainer's request. Findings requiring separate decisions or verification remain open.

  1. thread 1

createRelative now selects its share mask by object role: directories permit delete sharing for owned cleanup, while retained files keep FILE_SHARE_READ only. Their path replacement and competing write/delete exclusions are preserved; closeRuntime releases immutable-file handles before removing files.

Source inspected at c81b4a8; git diff --check passed. Tests, builds, source checks and CI reruns were skipped at the user's explicit request. No passing runtime/provider verification is claimed for this update.

  1. thread 1

The cleanup owner now enumerates directories using GetFileInformationByHandleEx on the retained handle and opens each child with NtCreateFile RootDirectory relative to that same handle. Display paths no longer select cleanup objects, and reparse entries are opened and removed as objects rather than traversed. The same relative lookup owner now serves runtimeDirectory, runtime_open and incomplete-snapshot cleanup; the snapshot path retains its recorded volume/file-ID check. Directory record offsets and names are bounded, and failed walks retain the root leases for retry. A second agent independently inspected this source change and found no blocking defect. Windows compilation, NTFS sharing/concurrency and runtime cleanup remain unverified; this source correction does not establish integration readiness.

Source inspected at c81b4a8; git diff --check passed. Tests, builds, source checks and CI reruns were skipped at the user's explicit request. No passing runtime/provider verification is claimed for this update.

@morluto

morluto commented Oct 13, 2026

Copy link
Copy Markdown
Owner

Review findings addressed at their producing boundaries and consumers. Latest changes were reviewed from source; tests and provider runs were skipped at the maintainer's request. Findings requiring separate decisions or verification remain open.

  1. thread 1

Source fix: 7f428a8cefd8d401db2eadc7e030df2cd1a06eae.

The cleanup retry regression matched diagnostic prose from the old path-based open. The handle-relative open correctly fails earlier when the held reader denies delete sharing. The regression now asserts the native addon's structured win32Code: 32 (ERROR_SHARING_VIOLATION), retaining its checks that ownership and the held file survive the failed cleanup, writes remain blocked, and cleanup succeeds after the reader closes.

Source inspection traced the native failure through the addon error projection and the retry consumer; the assertion no longer depends on cleanup-stage wording. No production cleanup change was needed. Windows compilation/runtime verification, tests, typechecks, builds, lint, workloads, and CI reruns were not run, as explicitly requested.

@morluto morluto added bug Something isn't working area: runtime Process capture and runtime observation area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior area: build Build, CI, packaging, and publishing P2: medium Medium priority issue labels Oct 13, 2026
@morluto
morluto merged commit 9b08c2b into morluto:main Oct 13, 2026
1 check passed
N0zoM1z0 added a commit that referenced this pull request Oct 13, 2026
The native directory lease now shares DELETE as intended by #2445, so rename
is permitted. Verify retained identity after rename and that retry/teardown
remove the owned object while preserving a replacement at its former path.
Keep the injected first-disposition failure, recovery, admission-cache and
environment-cleanup checks. No native runtime behavior is changed.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
morluto pushed a commit that referenced this pull request Oct 13, 2026
The native directory lease now shares DELETE as intended by #2445, so rename
is permitted. Verify retained identity after rename and that retry/teardown
remove the owned object while preserving a replacement at its former path.
Keep the injected first-disposition failure, recovery, admission-cache and
environment-cleanup checks. No native runtime behavior is changed.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: build Build, CI, packaging, and publishing area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior area: runtime Process capture and runtime observation bug Something isn't working P2: medium Medium priority issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants