Skip to content

Alt: Fall back to unfiltered enumeration on STATUS_OBJECT_NAME_NOT_FOUND (#130134) - #10

Closed
jeffhandley wants to merge 1 commit into
jeffhandley/directory-getfilesfrom
jeffhandley/directory-getfiles-unfiltered-fallback
Closed

jeffhandley wants to merge 1 commit into
jeffhandley/directory-getfilesfrom
jeffhandley/directory-getfiles-unfiltered-fallback

Conversation

@jeffhandley

Copy link
Copy Markdown
Owner

Summary

Alternative approach for dotnet/runtime#130134 (zero-match Directory.GetFiles throws FileNotFoundException on the Azure App Service run-from-package zip mount; regression from dotnet/runtime#127974).

See the primary/recommended approach in the peer PR from jeffhandley/directory-getfiles-status-object.

Root cause

dotnet/runtime#127974 passes the search pattern to NtQueryDirectoryFile as an OS-level filter. A filtered query that matches zero entries returns STATUS_NO_SUCH_FILE (0xC000000F) on NTFS but STATUS_OBJECT_NAME_NOT_FOUND (0xC0000034) on the zip mount; only the former is handled, so the latter throws.

Change (this branch)

In FileSystemEnumerator<T>.GetData() (Windows), when the first filtered NtQueryDirectoryFile call returns STATUS_OBJECT_NAME_NOT_FOUND, re-issue the query with no FileName filter and RestartScan, reverting to the pre-#127974 behavior for that directory (enumerate everything, then filter in managed code — which always runs anyway). The empty result is then produced without throwing.

Trade-off vs. the recommended approach: behavior-preserving and also guards drivers that reject otherwise-valid filters, but adds an extra syscall on the affected path and is more code.

Validation

Validated with the same faithful NtQueryDirectoryFile repro harness: on the .NET 11 branch, the zero-match probe returns empty with count = 0 (was throwing) while a matching pattern still returns count = 1.

Note

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

…Windows)

In FileSystemEnumerator<T>.GetData() on Windows, when the first filtered
NtQueryDirectoryFile call returns STATUS_OBJECT_NAME_NOT_FOUND, re-issue the query
with no FileName filter and RestartScan, reverting to enumerate-all-then-filter for
that directory. Some file system drivers (notably the Azure App Service
run-from-package zip mount) report a zero-match filtered query with this status; the
unfiltered retry lets the managed matching produce the empty result instead of
throwing FileNotFoundException.

Alternative approach for dotnet#130134.

Co-authored-by: Copilot <[email protected]>
@jeffhandley

Copy link
Copy Markdown
Owner Author

Closing in favor of #8 (handle STATUS_OBJECT_NAME_NOT_FOUND) after determining that the affected share does honor the OS-level filter.

Does the package-backed share do the filtering?

Yes. Two independent lines of evidence:

Logical (from the issue's two probes): the behavior is asymmetric -- a matching pattern (*.dll) returns results, while a zero-match pattern (__no_such_file__*.dll) throws. That is only possible if the driver evaluates the filter. A driver that ignored the filter would return the full listing with STATUS_SUCCESS for both, so the zero-match case would come back empty and never throw.

Empirical (harness, shipped .NET 10.0.9): I simulated both driver models at the NtQueryDirectoryFile boundary and ran the unmodified 10.0.9 runtime against each:

Driver model zero-match __no_such_file__*.dll matching *.dll
Honors filter, reports zero-match as STATUS_OBJECT_NAME_NOT_FOUND (0xC0000034) THREW FileNotFoundException: Could not find file '<dir>' -- matches the report returned, count = 1
Ignores filter (managed filtering only) returned, count = 0 (empty, no throw) -- does not match the report returned, count = 1

Only the filter-honoring model reproduces the reported exception. Since the real run-from-package mount does throw, it must be honoring the filter.

Why that rules out this approach

Because the driver already evaluates the filter and reports "no matches," dotnet#127974's optimization is genuinely working on this share. This approach would discard that result and re-query with no filter, forcing the driver to return every entry to managed code to be filtered there -- exactly the O(all-entries) scan dotnet#127974 set out to eliminate, and precisely in the failing scenario (a selective pattern that matches nothing in a large directory, e.g. Ninject.*.dll in a big wwwroot).

#8 instead returns empty immediately on 0xC0000034 (1 syscall, no managed enumeration), fixing the bug while preserving the optimization. It is also safe regardless of filtering quality, since .NET's managed matching always runs as the authoritative filter on top.

Note

This comment was generated by GitHub Copilot.

@jeffhandley jeffhandley closed this Jul 4, 2026
jeffhandley added a commit to dotnet/runtime that referenced this pull request Jul 8, 2026
…130196)

Fixes #130134

## Summary

`Directory.GetFiles`/`Directory.EnumerateFiles` (and the
`DirectoryInfo`/`Directory.GetFileSystemEntries` variants) with a search
pattern that matches **zero** files throws
`System.IO.FileNotFoundException` -- naming the directory itself -- on
some read-only / virtual file systems, instead of returning an empty
result. This was reported on an Azure App Service Windows app deployed
with `WEBSITE_RUN_FROM_PACKAGE`, where the content root is a read-only
mounted zip. It is a regression in 10.0.9, introduced by #127974
(backport of #122947 / #123695).

## Root cause

#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**:

```csharp
// 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. The affected file systems **do**
honor the filter -- a matching pattern returns results while only the
zero-match case reports `0xC0000034` -- so the OS-level filtering added
by #127974 is working; the runtime simply needs to treat this status as
"no matches."

## Change

Add `case Interop.StatusOptions.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
`STATUS_FILE_NOT_FOUND` / `STATUS_OBJECT_NAME_INVALID` statuses are
already handled. The `STATUS_OBJECT_NAME_NOT_FOUND` constant already
exists in `Interop.NtStatus.cs`.

## Testing

Reproducing this requires a file system driver that returns `0xC0000034`
for a zero-match filtered `NtQueryDirectoryFile`; the built-in drivers
(NTFS, ReFS, UDF, CDFS) all return the already-handled `0xC000000F`, so
there is no in-repo regression test. The fix was validated out-of-band
by hooking `ntdll!NtQueryDirectoryFile` in-process to rewrite the
zero-match status `0xC000000F` -> `0xC0000034` for a single directory,
faithfully mirroring the reported driver behavior:

- The zero-match `Directory.GetFiles(dir, "__no_such_file__*.dll")`
probe **throws** on the shipped 10.0.9 runtime and on an unmodified
build of this branch, and **returns an empty array** with this fix,
while a matching pattern (`*.dll`) continues to return results in all
cases.
- Confirmed across `dotnet`-installed 10.0.7 / 10.0.8 (return empty;
pre-regression), 10.0.9 (throws), and this branch (throws without the
fix, empty with it) on both x64 and Arm64.
- Full `System.IO.FileSystem.Tests` shows no regressions from this
change.

## Alternatives considered

Two alternative approaches were prototyped and rejected (prototype PRs
on my fork:
[error-mapping](jeffhandley#9),
[unfiltered-fallback](jeffhandley#10)):

- **Treat any `ERROR_FILE_NOT_FOUND` in the `default` arm as
no-matches** -- broader than necessary; keys off the translated Win32
error rather than the specific status, so it could absorb a
file-not-found produced for an unrelated reason.
- **Fall back to an unfiltered enumeration on `0xC0000034`** -- because
the affected drivers honor the filter, this would discard the driver's
result and re-enumerate every entry into managed code for the zero-match
case, reintroducing the O(all-entries) scan that #127974 set out to
eliminate, precisely in the failing scenario (a selective pattern
matching nothing in a large directory).

> [!NOTE]
> This pull request was authored with assistance from GitHub Copilot.

Co-authored-by: Copilot <[email protected]>
eiriktsarpalis pushed a commit to dotnet/runtime that referenced this pull request Jul 15, 2026
…130196)

Fixes #130134

## Summary

`Directory.GetFiles`/`Directory.EnumerateFiles` (and the
`DirectoryInfo`/`Directory.GetFileSystemEntries` variants) with a search
pattern that matches **zero** files throws
`System.IO.FileNotFoundException` -- naming the directory itself -- on
some read-only / virtual file systems, instead of returning an empty
result. This was reported on an Azure App Service Windows app deployed
with `WEBSITE_RUN_FROM_PACKAGE`, where the content root is a read-only
mounted zip. It is a regression in 10.0.9, introduced by #127974
(backport of #122947 / #123695).

## Root cause

#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**:

```csharp
// 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. The affected file systems **do**
honor the filter -- a matching pattern returns results while only the
zero-match case reports `0xC0000034` -- so the OS-level filtering added
by #127974 is working; the runtime simply needs to treat this status as
"no matches."

## Change

Add `case Interop.StatusOptions.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
`STATUS_FILE_NOT_FOUND` / `STATUS_OBJECT_NAME_INVALID` statuses are
already handled. The `STATUS_OBJECT_NAME_NOT_FOUND` constant already
exists in `Interop.NtStatus.cs`.

## Testing

Reproducing this requires a file system driver that returns `0xC0000034`
for a zero-match filtered `NtQueryDirectoryFile`; the built-in drivers
(NTFS, ReFS, UDF, CDFS) all return the already-handled `0xC000000F`, so
there is no in-repo regression test. The fix was validated out-of-band
by hooking `ntdll!NtQueryDirectoryFile` in-process to rewrite the
zero-match status `0xC000000F` -> `0xC0000034` for a single directory,
faithfully mirroring the reported driver behavior:

- The zero-match `Directory.GetFiles(dir, "__no_such_file__*.dll")`
probe **throws** on the shipped 10.0.9 runtime and on an unmodified
build of this branch, and **returns an empty array** with this fix,
while a matching pattern (`*.dll`) continues to return results in all
cases.
- Confirmed across `dotnet`-installed 10.0.7 / 10.0.8 (return empty;
pre-regression), 10.0.9 (throws), and this branch (throws without the
fix, empty with it) on both x64 and Arm64.
- Full `System.IO.FileSystem.Tests` shows no regressions from this
change.

## Alternatives considered

Two alternative approaches were prototyped and rejected (prototype PRs
on my fork:
[error-mapping](jeffhandley#9),
[unfiltered-fallback](jeffhandley#10)):

- **Treat any `ERROR_FILE_NOT_FOUND` in the `default` arm as
no-matches** -- broader than necessary; keys off the translated Win32
error rather than the specific status, so it could absorb a
file-not-found produced for an unrelated reason.
- **Fall back to an unfiltered enumeration on `0xC0000034`** -- because
the affected drivers honor the filter, this would discard the driver's
result and re-enumerate every entry into managed code for the zero-match
case, reintroducing the O(all-entries) scan that #127974 set out to
eliminate, precisely in the failing scenario (a selective pattern
matching nothing in a large directory).

> [!NOTE]
> This pull request was authored with assistance from GitHub Copilot.

Co-authored-by: Copilot <[email protected]>
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