fix(libc): make flock advisory on Windows so other descriptors can read and write #4958

Merged
milescoviello merged 3 commits from upstream-pr/4958 into main 2026-10-14 01:24:09 +00:00
milescoviello commented 2026-10-13 06:31:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-13T06:31:16Z.


What

Fixes #4957. Carries @DavidSamir's fix branch from that issue (DavidSamir:fix/libc-flock-advisory-windows, e4a1b54a, three commits), keeping the commits and their authorship. The fix and the test apply to current main without changes. After rebasing, the debt commit keeps the entries main has added or changed next to the flock line it edits.

On Windows, while one descriptor held a flock on a file, reads and writes through any other open of that file failed: with an exclusive lock everything failed (EIO/EACCES), with a shared lock every write. GuestFileLock_nid_no_patch locked the whole file with LockFileEx, and a Windows byte-range lock is enforced on every read and write that touches it. FreeBSD's flock(2) locks are advisory ("processes may still access files without using advisory locks"), and the Linux build reads and writes normally; docs/dev/debt/libc.md recorded the gap. The lock and the unlock now take one byte at offset 2^63 - 2 instead of the whole file: no read or write reaches it, while two flock calls still meet on the same byte, so shared and exclusive locks exclude each other as before between descriptors, duplicates and processes. Linux is unchanged.

The commits are the fix (FileLock.cpp), a test in guest_file_lock_conversion (with an exclusive lock held, sceKernelPwrite, sceKernelWrite and sceKernelPread through the other open succeed and read back what was written, and flock(LOCK_SH|LOCK_NB) through it still fails with errno 35), and the debt entry describing the byte.

Tested

  • From the issue: Windows 11, WinLibs MinGW-w64 GCC 15.2.0, Release: with main's FileLock.cpp guest_file_lock_conversion fails and with the fix the six guest_file_lock_* tests pass 5/5, including process, which takes the lock from a second process; a 54-line probe matches the Linux build on every line, a 95-line probe of shared/exclusive/upgrade/dup cases shows no lock line differing from Linux, and probes of 122, 188, 15 and 313 lines over the descriptor, file and stream functions are unchanged; no measurable cost; twelve catalog titles end the same way with both sets of libraries; full ctest 1026/1028, the two failures being GPU tests that fail the same way on main on that machine.
  • Titles, from @p3n1cillin's run on Windows 11 with MinGW GCC 15.2.0, with the other open PRs these titles need to load: VLC for PS5 (PPSA85300) and XPSemu (PPSA97358) both bundle Mesa. Its shader cache opens mesa_cache.db and mesa_cache.idx, fdopens each with "r+b" and locks them. Without this PR, the next fflush on one of those streams fails and libc throws fflush: flush failed: VLC after 2 s, XPSemu after 6 s. With it, VLC runs for the whole 60 s run, and XPSemu goes on to compile its SPIR-V cache and start its emulation; it then stops on a GPU timeout, a separate issue.
  • @babyhuey confirmed XPSemu separately on Windows 11 (RTX 3080, WinLibs GCC 15.2), on main 18c438b1 with #5061 and #5187. Without this PR the write after Mesa's cache flock fails with EACCES about 14 s in, and fflush throws. With it (8941af9d) the cache files get real data, and the title runs on to its GPU probe.
  • Linux, added for this carry: Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, rebased onto main 30f961266: the build is clean and all 345 guest_* tests pass in two runs. The changed code and the new test are Windows only, so this shows only that the Linux build is unaffected; a MinGW-w64 GCC 13 -fsyntax-only -Wall -Wextra compile of FileLock.cpp and GuestFileLock.cpp is clean. I could not run Windows myself.
  • tools/check_conventions.py --base upstream/main passes.

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 in TechnicalDebt
  • 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
  • Unblocks: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4839329660 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4958). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-13T06:31:16Z. --- ### What Fixes #4957. Carries @DavidSamir's fix branch from that issue (`DavidSamir:fix/libc-flock-advisory-windows`, `e4a1b54a`, three commits), keeping the commits and their authorship. The fix and the test apply to current `main` without changes. After rebasing, the debt commit keeps the entries `main` has added or changed next to the `flock` line it edits. On Windows, while one descriptor held a `flock` on a file, reads and writes through any other open of that file failed: with an exclusive lock everything failed (`EIO`/`EACCES`), with a shared lock every write. `GuestFileLock_nid_no_patch` locked the whole file with `LockFileEx`, and a Windows byte-range lock is enforced on every read and write that touches it. FreeBSD's `flock(2)` locks are advisory ("processes may still access files without using advisory locks"), and the Linux build reads and writes normally; `docs/dev/debt/libc.md` recorded the gap. The lock and the unlock now take one byte at offset 2^63 - 2 instead of the whole file: no read or write reaches it, while two `flock` calls still meet on the same byte, so shared and exclusive locks exclude each other as before between descriptors, duplicates and processes. Linux is unchanged. The commits are the fix (`FileLock.cpp`), a test in `guest_file_lock_conversion` (with an exclusive lock held, `sceKernelPwrite`, `sceKernelWrite` and `sceKernelPread` through the other open succeed and read back what was written, and `flock(LOCK_SH|LOCK_NB)` through it still fails with errno 35), and the debt entry describing the byte. ### Tested - From the issue: Windows 11, WinLibs MinGW-w64 GCC 15.2.0, Release: with `main`'s `FileLock.cpp` `guest_file_lock_conversion` fails and with the fix the six `guest_file_lock_*` tests pass 5/5, including `process`, which takes the lock from a second process; a 54-line probe matches the Linux build on every line, a 95-line probe of shared/exclusive/upgrade/`dup` cases shows no lock line differing from Linux, and probes of 122, 188, 15 and 313 lines over the descriptor, file and stream functions are unchanged; no measurable cost; twelve catalog titles end the same way with both sets of libraries; full `ctest` 1026/1028, the two failures being GPU tests that fail the same way on `main` on that machine. - Titles, from @p3n1cillin's [run](https://github.com/boykopovar/AnyPS5/pull/4958#issuecomment-6148512952) on Windows 11 with MinGW GCC 15.2.0, with the other open PRs these titles need to load: VLC for PS5 (PPSA85300) and XPSemu (PPSA97358) both bundle Mesa. Its shader cache opens `mesa_cache.db` and `mesa_cache.idx`, `fdopen`s each with `"r+b"` and locks them. Without this PR, the next `fflush` on one of those streams fails and libc throws `fflush: flush failed`: VLC after 2 s, XPSemu after 6 s. With it, VLC runs for the whole 60 s run, and XPSemu goes on to compile its SPIR-V cache and start its emulation; it then stops on a GPU timeout, a separate issue. - @babyhuey [confirmed XPSemu](https://github.com/boykopovar/AnyPS5/pull/4958#issuecomment-6152737807) separately on Windows 11 (RTX 3080, WinLibs GCC 15.2), on `main` 18c438b1 with #5061 and #5187. Without this PR the write after Mesa's cache `flock` fails with `EACCES` about 14 s in, and `fflush` throws. With it (8941af9d) the cache files get real data, and the title runs on to its GPU probe. - Linux, added for this carry: Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, rebased onto `main` 30f961266: the build is clean and all 345 `guest_*` tests pass in two runs. The changed code and the new test are Windows only, so this shows only that the Linux build is unaffected; a MinGW-w64 GCC 13 `-fsyntax-only -Wall -Wextra` compile of `FileLock.cpp` and `GuestFileLock.cpp` is clean. I could not run Windows myself. - `tools/check_conventions.py --base upstream/main` passes. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] Unblocks: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
xXPBSXx commented 2026-10-13 07:05:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T07:05:43Z.


Windows test of this PR.

Environment: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, BUILD_TESTING=ON, -j2, libusb-cmake from a local checkout. Built from main ea2bbefb2 with this PR and six other open PRs (#4958, #4956, #4954, #4953, #4946, #4942, #4927) merged together; the merges had no conflict.

Result: guest_file_lock_duplicate, guest_file_lock_lifetime, guest_file_lock_conversion, guest_file_lock_threads, guest_file_lock_streams, guest_file_lock_process (6 of 6) pass.

Not checked: whether the test fails without the change in the library code; the PR was not tested alone, only in the combined merge (the tests of the other PRs in the batch also pass); no title run; Linux not tested.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140227222 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4958#issuecomment-6140227222). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T07:05:43Z. --- Windows test of this PR. Environment: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, `BUILD_TESTING=ON`, `-j2`, `libusb-cmake` from a local checkout. Built from `main` `ea2bbefb2` with this PR and six other open PRs (#4958, #4956, #4954, #4953, #4946, #4942, #4927) merged together; the merges had no conflict. Result: guest_file_lock_duplicate, guest_file_lock_lifetime, guest_file_lock_conversion, guest_file_lock_threads, guest_file_lock_streams, guest_file_lock_process (6 of 6) pass. Not checked: whether the test fails without the change in the library code; the PR was not tested alone, only in the combined merge (the tests of the other PRs in the batch also pass); no title run; Linux not tested. AI-assisted (Claude Code).
p3n1cillin commented 2026-10-13 15:00:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by p3n1cillin on 2026-10-13T15:00:29Z.


Title evidence for this PR, Windows 11, MinGW GCC 15.2.0, Release, on main 3faa9cb40 merged with the open PRs these titles need to load (#4377, #4105, #5061, #5187 and others) and converted with --windows-fixed-base 0x400000 (#4140, for their fixed-base GOT reads):

  • VLC for PS5 (PPSA85300) and XPSemu (PPSA97358) both bundle Mesa. Mesa's shader cache database opens mesa_cache.db and mesa_cache.idx with O_RDWR|O_CREAT|O_CLOEXEC, fdopens each with "r+b" (VLC guest 0x1827B70) and locks them. Without this PR, the next fflush on one of those streams fails and libc throws fflush: flush failed (VLC after 2 s, XPSemu after 6 s).
  • With this PR: VLC runs the whole 60 s run, and XPSemu goes on to compile its SPIR-V cache and start the nv2a emulation (it then stops on a GPU timeout, a separate issue).

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6148512952 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4958#issuecomment-6148512952). Originally posted by [p3n1cillin](https://github.com/p3n1cillin) on 2026-10-13T15:00:29Z. --- Title evidence for this PR, Windows 11, MinGW GCC 15.2.0, Release, on `main` `3faa9cb40` merged with the open PRs these titles need to load (#4377, #4105, #5061, #5187 and others) and converted with `--windows-fixed-base 0x400000` (#4140, for their fixed-base GOT reads): - **VLC for PS5 (PPSA85300)** and **XPSemu (PPSA97358)** both bundle Mesa. Mesa's shader cache database opens `mesa_cache.db` and `mesa_cache.idx` with `O_RDWR|O_CREAT|O_CLOEXEC`, `fdopen`s each with `"r+b"` (VLC guest 0x1827B70) and locks them. Without this PR, the next `fflush` on one of those streams fails and libc throws `fflush: flush failed` (VLC after 2 s, XPSemu after 6 s). - With this PR: VLC runs the whole 60 s run, and XPSemu goes on to compile its SPIR-V cache and start the nv2a emulation (it then stops on a GPU timeout, a separate issue). AI-assisted (Claude Code).
babyhuey commented 2026-10-13 18:48:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-13T18:48:13Z.


A title this fixes on Windows: XPSemu (PPSA97358, an xemu port). Windows 11, RTX 3080, WinLibs GCC 15.2, Release. Built on main 18c438b1 with #5061 (getdtablesize) and #5187 (RLIMIT_NOFILE) merged, relinked with --windows-fixed-base 0x400000 (#4013).

  • Without this PR, about 14 s in, Mesa's shader disk cache (radv_builtin_shaders/part0/mesa_cache.db and .idx) takes its flock, and the next write fails with EACCES. The fflush then throws fflush: flush failed and the title exits with 0xC0000409.
  • With this PR (8941af9d, clean merge) the cache writes real data (.db 1663 B and 4209 B, .idx 216 B and 244 B, in part0 of each cache). The title runs on to xemu's GPU probe, a separate stop (see #5061).
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6152737807 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4958#issuecomment-6152737807). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-13T18:48:13Z. --- A title this fixes on Windows: XPSemu (PPSA97358, an xemu port). Windows 11, RTX 3080, WinLibs GCC 15.2, Release. Built on `main` 18c438b1 with #5061 (`getdtablesize`) and #5187 (`RLIMIT_NOFILE`) merged, relinked with `--windows-fixed-base 0x400000` (#4013). - Without this PR, about 14 s in, Mesa's shader disk cache (`radv_builtin_shaders/part0/mesa_cache.db` and `.idx`) takes its `flock`, and the next write fails with `EACCES`. The `fflush` then throws `fflush: flush failed` and the title exits with 0xC0000409. - With this PR (8941af9d, clean merge) the cache writes real data (`.db` 1663 B and 4209 B, `.idx` 216 B and 244 B, in part0 of each cache). The title runs on to xemu's GPU probe, a separate stop (see #5061).
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!4958
No description provided.