fix(libkernel): lock directories with flock on Windows as FreeBSD does #12060

Merged
Ragbie merged 3 commits from upstream-pr/12060 into main 2026-10-30 00:04:03 +00:00
Ragbie commented 2026-10-27 18:15:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ragbie on 2026-10-27T18:15:44Z.


What

On Windows flock on a directory descriptor returns 0 without taking a lock (#9101, which stopped it from aborting). Two opens of the same directory therefore never conflict. After flock(a, LOCK_EX | LOCK_NB), flock(b, LOCK_EX | LOCK_NB) on a second open of the directory also returns 0, where FreeBSD fails it with EWOULDBLOCK. A blocking request never waits. A title that locks a directory to keep a single instance or to serialize work on it gets no exclusion. The libkernel debt entry records this gap.

FreeBSD 11.4's sys_flock only requires a vnode, so a directory is locked like a file through VOP_ADVLOCK(F_FLOCK). The lock belongs to the open file: dup shares it, and it is released at the last close.

Windows LockFileEx, which the file path uses, rejects a directory handle. So GuestFileLock_nid_no_patch now checks the handle with GetFileInformationByHandle, and for a directory it keeps the lock in a table of this process instead:

  • The key is the directory's volume serial number and file index, so separate opens and different spellings of the path meet.
  • A request conflicts with an exclusive holder, and an exclusive request also conflicts with shared holders. A blocking request waits on a condition variable. A busy non-blocking request fails with ERROR_LOCK_VIOLATION, which flock already maps to EWOULDBLOCK.
  • The per-descriptor LockState already follows the open file: dup shares it, and close, dup2, descriptor reuse and stream close drop references. Its destructor now releases a directory lock, so the lock ends with the last reference, as on FreeBSD.
  • A conversion happens in place under the table's mutex, as FreeBSD 11.4's lf_setlock replaces the owner's own lock: a request conflicts only with other holders, a failed LOCK_EX | LOCK_NB upgrade keeps the shared lock, a downgrade is atomic, and a blocking upgrade releases the shared lock before it waits. Waiters wake on a notify_all and are not served in arrival order, unlike FreeBSD; the debt entry says so.

flock_nid_postfix no longer returns early for a directory. The libkernel debt entry now records the remaining gap: the lock is visible only within this process. The libc flock entry says the same. Linux and macOS keep using the host flock.

Tested

Windows 11 Pro, Intel Core i5-14600KF, WinLibs GCC 15.2.0, full Release build:

  • guest_file_lock_directory already checked that flock is accepted on a directory. It now also runs these cases:
    • With LOCK_EX held, a second open of the directory cannot take LOCK_EX or LOCK_SH.
    • After a downgrade to LOCK_SH, the second open can share the lock but cannot take it exclusively.
    • With both opens holding LOCK_SH, an upgrade fails with EWOULDBLOCK.
    • A dup keeps the lock after the original descriptor is closed.
    • A blocking flock from another thread is still waiting after 200 ms and returns once the last reference is closed.
    • A third open sees the second open's lock until that open is closed.
    • After a failed LOCK_EX | LOCK_NB upgrade, the other open still cannot take LOCK_EX.
    • A downgrade while another thread waits for LOCK_EX returns 0, the waiter keeps waiting, and a third open can share the lock.
    • A blocking upgrade releases its shared lock while it waits, so the other open can upgrade, and it returns once that open unlocks.
  • Without the fix (main's sources): guest_file_lock_directory fails at the first conflict. The other six guest_file_lock_* tests pass.
  • With the fix:
    • All seven guest_file_lock_* tests pass with --repeat until-fail:5.
    • The 120 tests matching lock|directory|descriptor|dup|fcntl|close|open|filesystem|stream (without agc_ and shader) pass with --repeat until-fail:3.
    • Full ctest on main 228cd64b5: 99% tests passed, 5 tests failed out of 2673.
      • audio_out2_port_buffers and guest_video_out_high_frame_rate failed only in the parallel run and pass alone (--repeat until-fail:3); neither uses flock.
      • guest_wide_formatting and agc_driver_image_plane_address_on_line also fail on main on this machine. guest_wide_formatting fails because %Z gives the host's non-ASCII zone name, which #10987 addresses.
      • agc_driver_mesh_barycentric_emulation fails every time on this machine's NVIDIA GPU with the pixel shader did not emulate barycentrics. #9193 changed that test on 2026-10-27, and it does not use files.
    • check_skips.py reports one skip, guest_fopen_errors. It skips itself on this machine because creating symbolic links needs Developer Mode.

Linux and macOS: not affected. The changed code and the test are Windows-only.

Overlaps: #11939 edits the neighbouring sceKernelGetProcessAbiType entry in docs/dev/debt/libkernel.md; neither PR touches the other's code. Proposed order, asked of #11939's author in this PR's conversation: this PR first, since #11939 waits for #9326; #11939 then keeps its entry and this PR's flock entry unchanged. If #11939 goes first instead, this PR rebases after it and Depends on becomes #11939.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5038931996 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12060). Originally posted by [Ragbie](https://github.com/Ragbie) on 2026-10-27T18:15:44Z. --- ### What On Windows `flock` on a directory descriptor returns 0 without taking a lock (#9101, which stopped it from aborting). Two opens of the same directory therefore never conflict. After `flock(a, LOCK_EX | LOCK_NB)`, `flock(b, LOCK_EX | LOCK_NB)` on a second open of the directory also returns 0, where FreeBSD fails it with `EWOULDBLOCK`. A blocking request never waits. A title that locks a directory to keep a single instance or to serialize work on it gets no exclusion. The libkernel debt entry records this gap. FreeBSD 11.4's [`sys_flock`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_descrip.c#L2889-L2929) only requires a vnode, so a directory is locked like a file through `VOP_ADVLOCK(F_FLOCK)`. The lock belongs to the open file: `dup` shares it, and it is released at the last close. Windows `LockFileEx`, which the file path uses, rejects a directory handle. So `GuestFileLock_nid_no_patch` now checks the handle with `GetFileInformationByHandle`, and for a directory it keeps the lock in a table of this process instead: - The key is the directory's volume serial number and file index, so separate opens and different spellings of the path meet. - A request conflicts with an exclusive holder, and an exclusive request also conflicts with shared holders. A blocking request waits on a condition variable. A busy non-blocking request fails with `ERROR_LOCK_VIOLATION`, which `flock` already maps to `EWOULDBLOCK`. - The per-descriptor `LockState` already follows the open file: `dup` shares it, and close, `dup2`, descriptor reuse and stream close drop references. Its destructor now releases a directory lock, so the lock ends with the last reference, as on FreeBSD. - A conversion happens in place under the table's mutex, as FreeBSD 11.4's [`lf_setlock`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_lockf.c#L1379-L1544) replaces the owner's own lock: a request conflicts only with other holders, a failed `LOCK_EX | LOCK_NB` upgrade keeps the shared lock, a downgrade is atomic, and a blocking upgrade releases the shared lock before it waits. Waiters wake on a `notify_all` and are not served in arrival order, unlike FreeBSD; the debt entry says so. `flock_nid_postfix` no longer returns early for a directory. The libkernel debt entry now records the remaining gap: the lock is visible only within this process. The libc `flock` entry says the same. Linux and macOS keep using the host `flock`. ### Tested Windows 11 Pro, Intel Core i5-14600KF, WinLibs GCC 15.2.0, full Release build: - `guest_file_lock_directory` already checked that `flock` is accepted on a directory. It now also runs these cases: - With `LOCK_EX` held, a second open of the directory cannot take `LOCK_EX` or `LOCK_SH`. - After a downgrade to `LOCK_SH`, the second open can share the lock but cannot take it exclusively. - With both opens holding `LOCK_SH`, an upgrade fails with `EWOULDBLOCK`. - A `dup` keeps the lock after the original descriptor is closed. - A blocking `flock` from another thread is still waiting after 200 ms and returns once the last reference is closed. - A third open sees the second open's lock until that open is closed. - After a failed `LOCK_EX | LOCK_NB` upgrade, the other open still cannot take `LOCK_EX`. - A downgrade while another thread waits for `LOCK_EX` returns 0, the waiter keeps waiting, and a third open can share the lock. - A blocking upgrade releases its shared lock while it waits, so the other open can upgrade, and it returns once that open unlocks. - **Without the fix (`main`'s sources):** `guest_file_lock_directory` fails at the first conflict. The other six `guest_file_lock_*` tests pass. - **With the fix:** - All seven `guest_file_lock_*` tests pass with `--repeat until-fail:5`. - The 120 tests matching `lock|directory|descriptor|dup|fcntl|close|open|filesystem|stream` (without `agc_` and `shader`) pass with `--repeat until-fail:3`. - Full `ctest` on `main` `228cd64b5`: `99% tests passed, 5 tests failed out of 2673`. - `audio_out2_port_buffers` and `guest_video_out_high_frame_rate` failed only in the parallel run and pass alone (`--repeat until-fail:3`); neither uses `flock`. - `guest_wide_formatting` and `agc_driver_image_plane_address_on_line` also fail on `main` on this machine. `guest_wide_formatting` fails because `%Z` gives the host's non-ASCII zone name, which #10987 addresses. - `agc_driver_mesh_barycentric_emulation` fails every time on this machine's NVIDIA GPU with `the pixel shader did not emulate barycentrics`. #9193 changed that test on 2026-10-27, and it does not use files. - `check_skips.py` reports one skip, `guest_fopen_errors`. It skips itself on this machine because creating symbolic links needs Developer Mode. Linux and macOS: not affected. The changed code and the test are Windows-only. **Overlaps:** #11939 edits the neighbouring `sceKernelGetProcessAbiType` entry in `docs/dev/debt/libkernel.md`; neither PR touches the other's code. Proposed order, asked of #11939's author in this PR's conversation: this PR first, since #11939 waits for #9326; #11939 then keeps its entry and this PR's `flock` entry unchanged. If #11939 goes first instead, this PR rebases after it and `Depends on` becomes #11939. ### 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
MarcZX10 commented 2026-10-27 18:26:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T18:26:49Z.

Review: CHANGES_REQUESTED


The Windows-only implementation and the added directory-lock tests are coherent on inspection. The Windows hosted build passes and the PR reports repeated full Windows lock tests; I did not run the Windows-only runtime tests locally. git diff --check is clean. Several macOS relinker/register-context checks are still queued.

There is a contribution-rule blocker in the reported overlap with open #11939. I fetched both exact heads and ran a pairwise merge preview: their code changes are distinct, but the merge conflicts in docs/dev/debt/libkernel.md, the shared file both PRs edit. The PR body says “whichever lands second” and the checklist says Depends on: none; that does not record an agreed order. CONTRIBUTING.md requires the authors to agree which PR goes first and declare that order with Depends on.

Please coordinate the order with #11939's author, record the dependency on the checklist, and ensure the final debt file preserves both entries. I am requesting this change for the documented repository rule, not for a source-code defect.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5672306828 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12060#pullrequestreview-5672306828). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T18:26:49Z. **Review:** CHANGES_REQUESTED --- The Windows-only implementation and the added directory-lock tests are coherent on inspection. The Windows hosted build passes and the PR reports repeated full Windows lock tests; I did not run the Windows-only runtime tests locally. `git diff --check` is clean. Several macOS relinker/register-context checks are still queued. There is a contribution-rule blocker in the reported overlap with open #11939. I fetched both exact heads and ran a pairwise merge preview: their code changes are distinct, but the merge conflicts in `docs/dev/debt/libkernel.md`, the shared file both PRs edit. The PR body says “whichever lands second” and the checklist says `Depends on: none`; that does not record an agreed order. `CONTRIBUTING.md` requires the authors to agree which PR goes first and declare that order with `Depends on`. Please coordinate the order with #11939's author, record the dependency on the checklist, and ensure the final debt file preserves both entries. I am requesting this change for the documented repository rule, not for a source-code defect.
MarcZX10 commented 2026-10-29 06:00:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T06:00:01Z.

Review: CHANGES_REQUESTED


Re-checked — changes requested

What I checked

  • Reviewed exact head f658dd8dadecdf8523c7b731d059c855cfa3499f against fetched main 2960e9a6e7; the merge preview is clean and git diff --check passes.
  • The new directory-lock conversion updates the holder in place, preserves a shared lock after a failed nonblocking upgrade, and tests blocking upgrades/downgrades and release behavior. I found no specific source defect in this revision.
  • Hosted Windows, conventions, eligibility, labels, and report checks pass; product Linux and specialized jobs were skipped.

Blocking items

  • You proposed #12060 first, then #11939. That order is clear in your comment, but #11939's checklist currently lists only Depends on: #9326; its author still needs to record #12060 as a prerequisite and confirm the order. The shared debt file has a textual conflict, so the dependency must be explicit on the second PR.
  • This branch is 1396 commits behind fetched main. The detailed Windows runs and integration suite in the description use older main 228cd64b5. Please refresh onto current main and rerun the focused directory-lock tests before merge.

The author and an independent reviewer report repeated Windows tests on the older head; I did not run Windows tests locally. I am requesting changes for the unresolved declared order and current-main validation, not for a confirmed locking defect.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5693492056 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12060#pullrequestreview-5693492056). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T06:00:01Z. **Review:** CHANGES_REQUESTED --- # Re-checked — changes requested ## What I checked - Reviewed exact head `f658dd8dadecdf8523c7b731d059c855cfa3499f` against fetched `main` `2960e9a6e7`; the merge preview is clean and `git diff --check` passes. - The new directory-lock conversion updates the holder in place, preserves a shared lock after a failed nonblocking upgrade, and tests blocking upgrades/downgrades and release behavior. I found no specific source defect in this revision. - Hosted Windows, conventions, eligibility, labels, and report checks pass; product Linux and specialized jobs were skipped. ## Blocking items - You proposed #12060 first, then #11939. That order is clear in your comment, but #11939's checklist currently lists only `Depends on: #9326`; its author still needs to record #12060 as a prerequisite and confirm the order. The shared debt file has a textual conflict, so the dependency must be explicit on the second PR. - This branch is 1396 commits behind fetched `main`. The detailed Windows runs and integration suite in the description use older main `228cd64b5`. Please refresh onto current `main` and rerun the focused directory-lock tests before merge. The author and an independent reviewer report repeated Windows tests on the older head; I did not run Windows tests locally. I am requesting changes for the unresolved declared order and current-main validation, not for a confirmed locking defect.
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!12060
No description provided.