Skip to content

Handle STATUS_OBJECT_NAME_NOT_FOUND in Windows directory enumeration (#130134) - #8

Closed
jeffhandley wants to merge 1 commit into
jeffhandley/directory-getfilesfrom
jeffhandley/directory-getfiles-status-object
Closed

jeffhandley wants to merge 1 commit into
jeffhandley/directory-getfilesfrom
jeffhandley/directory-getfiles-status-object

Conversation

@jeffhandley

@jeffhandley jeffhandley commented Jul 4, 2026 •

Copy link
Copy Markdown
Owner

Summary

Addresses dotnet/runtime#130134: Directory.GetFiles/EnumerateFiles with a zero-match search pattern throws FileNotFoundException (naming the directory itself) on a read-only, run-from-package (zip-mounted) content root, instead of returning an empty array. Regression introduced in 10.0.9 by dotnet/runtime#127974.

Root cause

dotnet/runtime#127974 began passing the search pattern to NtQueryDirectoryFile as an OS-level FileName filter. When zero entries match, FileSystemEnumerator<T>.GetData()'s switch only handles STATUS_NO_SUCH_FILE (0xC000000F). Some file system drivers report the same "no matching entries" condition as STATUS_OBJECT_NAME_NOT_FOUND (0xC0000034), which fell through to the default case → ERROR_FILE_NOT_FOUND → throw.

Evidence that the culprit is STATUS_OBJECT_NAME_NOT_FOUND

The default arm of GetData() turns the raw NTSTATUS into the thrown exception in two hops:

// FileSystemEnumerator.Windows.cs, default case
int error = (int)Interop.NtDll.RtlNtStatusToDosError(status);      // hop 1: NTSTATUS  -> Win32 error
...
throw Win32Marshal.GetExceptionForWin32Error(error, _currentPath); // hop 2: Win32 error -> Exception

Hop 2 — Win32Marshal.GetExceptionForWin32Error (src/libraries/Common/src/System/IO/Win32Marshal.cs) selects the exception type and message purely from the Win32 error code:

Win32 error Exception Message resource / text
ERROR_FILE_NOT_FOUND (2) FileNotFoundException IO_FileNotFound_FileName = "Could not find file '{0}'."
ERROR_PATH_NOT_FOUND (3) DirectoryNotFoundException IO_PathNotFound_Path = "Could not find a part of the path '{0}'."

The report is System.IO.FileNotFoundException: Could not find file '<dir>' — exactly the ERROR_FILE_NOT_FOUND (2) branch. So the raw NTSTATUS must be one that hop 1 (RtlNtStatusToDosError, an ntdll OS function — the mapping is defined by Windows, not .NET) translates to 2, and that is not already handled by the switch.

RtlNtStatusToDosError translations for the relevant statuses (values verified by calling it directly):

NTSTATUS Value RtlNtStatusToDosError In switch today?
STATUS_SUCCESS 0x00000000 ERROR_SUCCESS (0) handled
STATUS_NO_MORE_FILES 0x80000006 ERROR_NO_MORE_FILES (18) handled
STATUS_NO_SUCH_FILE (aka STATUS_FILE_NOT_FOUND) 0xC000000F ERROR_FILE_NOT_FOUND (2) handled
STATUS_OBJECT_NAME_INVALID 0xC0000033 ERROR_INVALID_NAME (123) handled
STATUS_OBJECT_NAME_NOT_FOUND 0xC0000034 ERROR_FILE_NOT_FOUND (2) NOT handled → throws
STATUS_OBJECT_PATH_NOT_FOUND 0xC000003A ERROR_PATH_NOT_FOUND (3) n/a

Only two NTSTATUS values map to ERROR_FILE_NOT_FOUND (2): STATUS_NO_SUCH_FILE (already handled, so it can't produce the throw) and STATUS_OBJECT_NAME_NOT_FOUND. Therefore 0xC0000034 is the only possible culprit. STATUS_OBJECT_PATH_NOT_FOUND (0xC000003A) is ruled out: it maps to ERROR_PATH_NOT_FOUND (3), which hop 2 turns into a DirectoryNotFoundException with "Could not find a part of the path '{0}'." — a different exception type and message than the report.

Empirically, a direct NtQueryDirectoryFile P/Invoke with a zero-match filter returns the handled STATUS_NO_SUCH_FILE (0xC000000F) on NTFS, ReFS, UDF, and CDFS — so the built-in drivers don't hit this, and it takes a driver that returns 0xC0000034 (as reported for the read-only zip mount) to trigger it.

Change (this branch — recommended)

Add case STATUS_OBJECT_NAME_NOT_FOUND to the existing no-matches branch in GetData() (Windows), alongside STATUS_FILE_NOT_FOUND. The directory handle was already opened successfully in Init(), so this status can only mean the filter matched nothing — consistent with how the sibling 0xC000000F/0xC0000033 statuses are already handled. The constant already exists in Interop.NtStatus.cs.

Validation

  • Faithful repro: an in-process hook of ntdll!NtQueryDirectoryFile rewrites the zero-match status 0xC000000F→0xC0000034 for a single sentinel directory only, exactly mirroring the reported driver behavior (inline hooking defeats the CLR's cached P/Invoke pointer, so it works on shipped and locally built runtimes alike).
  • Cross-version (via dotnet-install, x64 + ARM64): 10.0.7 / 10.0.8 return empty; 10.0.9 throws; the .NET 11 branch throws unfixed and returns empty with this fix (the driver still returns 0xC0000034, now handled).
  • Tests: full System.IO.FileSystem.Tests — no regressions. Enumeration namespace 134/134; GetFiles/EnumerateFiles/GetFileSystemEntries 234 pass. The only suite failures are pre-existing admin-required volume-mount tests, which fail identically on baseline.

Alternatives

Two alternative approaches were captured as peer PRs for comparison:

  • jeffhandley/directory-getfiles-error-mapping (closed) — translate NTSTATUS→Win32 error and treat ERROR_FILE_NOT_FOUND as no-matches.
  • jeffhandley/directory-getfiles-unfiltered-fallback — retry unfiltered on STATUS_OBJECT_NAME_NOT_FOUND, reverting to enumerate-all-then-filter. Note the affected drivers do honor the filter (a matching pattern returns results; only zero-match reports the status), so falling back to unfiltered enumeration would re-introduce the O(all-entries) managed scan for the zero-match case that dotnet/runtime#127974 set out to avoid.

Note

This pull request (including its description) was created by GitHub Copilot.

Comment on lines +174 to +179
// When an OS-level filter (FileName) is passed to NtQueryDirectoryFile and nothing matches, most file
// system drivers report STATUS_NO_SUCH_FILE (handled above as STATUS_FILE_NOT_FOUND). Some drivers report
// STATUS_OBJECT_NAME_NOT_FOUND for the same "no matching entries" condition instead- notably the read-only
// zip mount used by Azure App Service's run-from-package deployments. The directory handle was already
// opened successfully, so this can only mean the filter matched nothing; treat it the same as no matches.
case Interop.StatusOptions.STATUS_OBJECT_NAME_NOT_FOUND:

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

The comment should begin with "STATUS_OBJECT_NAME_NOT_FOUND occurs when..." to match the other comments.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

And don't name Azure App Service's run-from-package deployments specifically. Reference the more generalized scenario.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Done in the amended commit (354b8e0). The comment now begins with STATUS_OBJECT_NAME_NOT_FOUND occurs when... to match the sibling comments, and no longer names Azure run-from-package specifically — it references the generalized "read-only or virtual file systems" scenario instead.

Note

Reply generated by GitHub Copilot.

@jeffhandley

Copy link
Copy Markdown
Owner Author

It is provably the only other unhandled NTSTATUS

Then you need to prove it. Show the evidence.

Add STATUS_OBJECT_NAME_NOT_FOUND (0xC0000034) to the no-matches branch of
FileSystemEnumerator<T>.GetData() on Windows, alongside STATUS_FILE_NOT_FOUND.
A filtered NtQueryDirectoryFile that matches zero entries can return this
status instead of STATUS_NO_SUCH_FILE on some file systems (for example,
certain read-only or virtual file systems); it is now treated as an empty
result instead of throwing FileNotFoundException.

Fixes dotnet#130134

Co-authored-by: Copilot <[email protected]>
@jeffhandley
jeffhandley force-pushed the jeffhandley/directory-getfiles-status-object branch from d02c35e to 354b8e0 Compare July 4, 2026 07:47
@jeffhandley

Copy link
Copy Markdown
Owner Author

Added the evidence to the PR description under Evidence that the culprit is STATUS_OBJECT_NAME_NOT_FOUND.

In short: the reported message is "Could not find file" = ERROR_FILE_NOT_FOUND (2). Calling RtlNtStatusToDosError directly shows the mappings — STATUS_NO_SUCH_FILE (0xC000000F) → 2, STATUS_OBJECT_NAME_NOT_FOUND (0xC0000034) → 2, STATUS_OBJECT_PATH_NOT_FOUND (0xC000003A) → 3, STATUS_OBJECT_NAME_INVALID (0xC0000033) → 123, STATUS_NO_MORE_FILES (0x80000006) → 18. Only 0xC000000F (already handled, so it can't throw) and 0xC0000034 map to ERROR_FILE_NOT_FOUND, so 0xC0000034 is the only possible culprit; 0xC000003A is ruled out because it maps to ERROR_PATH_NOT_FOUND (3). Empirically, NTFS/ReFS/UDF/CDFS all return the handled 0xC000000F for a zero-match filtered query.

Note

Reply generated by GitHub Copilot.

@jeffhandley jeffhandley closed this Jul 9, 2026
@jeffhandley
jeffhandley deleted the jeffhandley/directory-getfiles-status-object branch July 10, 2026 00:47
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