fix(libkernel): leave out directory entries removed while getdents lists them on Windows #12139

Merged
dantdmdarealguy merged 1 commit from upstream-pr/12139 into main 2026-11-05 11:04:45 +00:00
dantdmdarealguy commented 2026-10-27 23:26:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by dantdmdarealguy on 2026-10-27T23:26:53Z.


What

On Windows, sceKernelGetdents, getdirentries and readdir on a host directory fail with ENOENT or EACCES if another thread or process removes an entry while the listing is being read.

Since #8520, the directory snapshot in DirectoryDescriptor.cpp Load lists the directory and then lstats each entry. When an entry is gone by the time of its lstat, the whole read fails with that error. FreeBSD getdents never fails this way: an entry removed during the read is either listed or not.

Two cases cause it:

  • The entry is already gone. FillLinkStat fails with ENOENT, and Load threw. It now leaves the entry out, as if it had been removed before the read.
  • The entry is being deleted. Its delete is pending until the deleting handle closes. UCRT _wstat64 already reports ENOENT for such a file, but DoStat/DoLstat then open it with CreateFileW. That open fails with ERROR_ACCESS_DENIED, and RtlGetLastNtStatus reports STATUS_DELETE_PENDING. That path returned EACCES. It now returns ENOENT, the same answer _wstat64 gives. So stat/lstat of a file being deleted no longer report EACCES in that window.

Errors other than a removed entry still fail the read as before.

This is the guest_filesystem abort on Windows CI in job 122249163051 (main e2a5118 + #10616). The abort is at GuestFilesystem.cpp:1379 Require(read > 0), after sceKernelOpen(".", O_DIRECTORY). Symbolized from the same test source and toolchain: Check returns to guest_filesystem_tests.exe+0x19fe, and the caller's return address +0x2643e follows Check(…, 1379). Both offsets match the CI log. The open failed path=…/missing/../present.txt lines before the abort are expected failures logged by TestParentTraversal, not the cause. The test lists the working directory that the other libkernel tests share, while ctest runs them in parallel and they delete their temporary directories there.

Linux host directories use getdents64 in Stdio.cpp, which already tolerates removed entries. On Linux, Load only serves directories that the app archive also covers.

New test guest_directory_removed_entries:

  • On Windows, it holds a file in a pending delete and checks that sceKernelStat and lstat return ENOENT and that getdents lists only . and ...
  • Then, for 12 rounds, a thread removes 256 files while getdents reads the directory. The read must succeed, list every file that is not removed, and list no name twice.

Tested

Windows 11 Pro, Intel Core i9-14900K, 128 GB RAM, NVIDIA GeForce RTX 5080 (driver 32.0.16.1742), WinLibs GCC 15.2.0, Release.

  • guest_directory_removed_entries: main 100/100 abort at its getdents check. With this change, 0/100, about 1.6 s per run.
  • Stress rounds only (pending-delete case compiled out, 24 rounds): main 100/100 abort with getdents 0x80020002 (ENOENT). With the ENOENT part of the fix alone, 6/20 still aborted with 0x8002000d (EACCES) from the pending-delete window. With the whole change, 0/100.
  • guest_filesystem with two processes creating and removing directories in its working directory: main 68/100 abort, all at +0x19fe/+0x2643e as in CI. With this change, 0/100.
  • Linux (Arch WSL, GCC): guest_directory_removed_entries passes 100/100 on both main and this change, since host directories there use getdents64.
  • ctest: ctest -R "^guest_directory|^guest_filesystem$|^guest_truncate|^guest_stat|^guest_fdopendir|patched_libraries" --parallel 2 passes 13/13; patched_libraries reports 368 patched libraries loaded, 0 failed.
  • Wider ctest: ctest -R "stat|file|dir|link|rename|unlink|open|path|chmod|utime|access|kqueue|aio|mkdir" --parallel 2 passes 197/198. The one failure is conventions_env_reference_test_a_check_from_a_subdirectory_reports_the_same_findings, a Python tools test whose git tag base fails here because this machine's global git config signs tags; it fails the same way without this change.
  • Builds: cmake --build and --target libs succeed with no warnings in the touched files.
  • Checks: check_test_registration.py and check_conventions.py --base origin/main pass.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes, some AI assistance was used
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5042644892 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12139). Originally posted by [dantdmdarealguy](https://github.com/dantdmdarealguy) on 2026-10-27T23:26:53Z. --- ### What On Windows, `sceKernelGetdents`, `getdirentries` and `readdir` on a host directory fail with `ENOENT` or `EACCES` if another thread or process removes an entry while the listing is being read. Since #8520, the directory snapshot in `DirectoryDescriptor.cpp` `Load` lists the directory and then `lstat`s each entry. When an entry is gone by the time of its `lstat`, the whole read fails with that error. FreeBSD `getdents` never fails this way: an entry removed during the read is either listed or not. Two cases cause it: - **The entry is already gone.** `FillLinkStat` fails with `ENOENT`, and `Load` threw. It now leaves the entry out, as if it had been removed before the read. - **The entry is being deleted.** Its delete is pending until the deleting handle closes. UCRT `_wstat64` already reports `ENOENT` for such a file, but `DoStat`/`DoLstat` then open it with `CreateFileW`. That open fails with `ERROR_ACCESS_DENIED`, and `RtlGetLastNtStatus` reports `STATUS_DELETE_PENDING`. That path returned `EACCES`. It now returns `ENOENT`, the same answer `_wstat64` gives. So `stat`/`lstat` of a file being deleted no longer report `EACCES` in that window. Errors other than a removed entry still fail the read as before. This is the `guest_filesystem` abort on Windows CI in job 122249163051 (main e2a5118 + #10616). The abort is at `GuestFilesystem.cpp:1379` `Require(read > 0)`, after `sceKernelOpen(".", O_DIRECTORY)`. Symbolized from the same test source and toolchain: `Check` returns to `guest_filesystem_tests.exe+0x19fe`, and the caller's return address `+0x2643e` follows `Check(…, 1379)`. Both offsets match the CI log. The `open failed path=…/missing/../present.txt` lines before the abort are expected failures logged by `TestParentTraversal`, not the cause. The test lists the working directory that the other libkernel tests share, while ctest runs them in parallel and they delete their temporary directories there. Linux host directories use `getdents64` in `Stdio.cpp`, which already tolerates removed entries. On Linux, `Load` only serves directories that the app archive also covers. New test `guest_directory_removed_entries`: - On Windows, it holds a file in a pending delete and checks that `sceKernelStat` and `lstat` return `ENOENT` and that `getdents` lists only `.` and `..`. - Then, for 12 rounds, a thread removes 256 files while `getdents` reads the directory. The read must succeed, list every file that is not removed, and list no name twice. ### Tested Windows 11 Pro, Intel Core i9-14900K, 128 GB RAM, NVIDIA GeForce RTX 5080 (driver 32.0.16.1742), WinLibs GCC 15.2.0, Release. - **`guest_directory_removed_entries`:** `main` 100/100 abort at its `getdents` check. With this change, 0/100, about 1.6 s per run. - **Stress rounds only** (pending-delete case compiled out, 24 rounds): `main` 100/100 abort with getdents `0x80020002` (`ENOENT`). With the `ENOENT` part of the fix alone, 6/20 still aborted with `0x8002000d` (`EACCES`) from the pending-delete window. With the whole change, 0/100. - **`guest_filesystem` with two processes creating and removing directories in its working directory:** `main` 68/100 abort, all at `+0x19fe`/`+0x2643e` as in CI. With this change, 0/100. - **Linux (Arch WSL, GCC):** `guest_directory_removed_entries` passes 100/100 on both `main` and this change, since host directories there use `getdents64`. - **ctest:** `ctest -R "^guest_directory|^guest_filesystem$|^guest_truncate|^guest_stat|^guest_fdopendir|patched_libraries" --parallel 2` passes 13/13; `patched_libraries` reports 368 patched libraries loaded, 0 failed. - **Wider ctest:** `ctest -R "stat|file|dir|link|rename|unlink|open|path|chmod|utime|access|kqueue|aio|mkdir" --parallel 2` passes 197/198. The one failure is `conventions_env_reference_test_a_check_from_a_subdirectory_reports_the_same_findings`, a Python tools test whose `git tag base` fails here because this machine's global git config signs tags; it fails the same way without this change. - **Builds:** `cmake --build` and `--target libs` succeed with no warnings in the touched files. - **Checks:** `check_test_registration.py` and `check_conventions.py --base origin/main` pass. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes, some AI assistance was used
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!12139
No description provided.