Skip to content

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

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
milescoviello:carry/flock-advisory-windows-4957
Oct 14, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
milescoviello:carry/flock-advisory-windows-4957

Conversation

@milescoviello

@milescoviello milescoviello commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

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 18c438b with feat(libkernel): add getdtablesize #5061 and feat(libkernel): report RLIMIT_NOFILE through getrlimit #5187. Without this PR the write after Mesa's cache flock fails with EACCES about 14 s in, and fflush throws. With it (8941af9) 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 30f9612: 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

@xXPBSXx

xXPBSXx commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

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).

@babyhuey

Copy link
Copy Markdown
Contributor

A title this fixes on Windows: XPSemu (PPSA97358, an xemu port). Windows 11, RTX 3080, WinLibs GCC 15.2, Release. Built on main 18c438b 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 (8941af9, 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 feat(libkernel): add getdtablesize #5061).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

libc: a flock makes reads and writes through other descriptors fail on Windows, where FreeBSD locks are advisory; fix branch ready

6 participants