libc: a flock makes reads and writes through other descriptors fail on Windows, where FreeBSD locks are advisory; fix branch ready #4957
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-13T06:27:47Z.
On Windows, while one descriptor holds a
flockon a file, reads and writes through any other open of that file fail: with an exclusive lock everything fails, with a shared lock every write. FreeBSD's flock(2) locks are advisory ("Advisory locks allow cooperating processes to perform consistent operations on files, but do not guarantee consistency (i.e., processes may still access files without using advisory locks possibly resulting in inconsistencies)"), and the Linux build reads and writes normally. A title that locks its save or database file through one descriptor and reads it through another, or through aFILE*, getsEIOorEACCES.This is the gap docs/dev/debt/libc.md records: "The host's LockFileEx locks also restrict ordinary reads and writes through other handles, unlike FreeBSD's advisory locks".
Cause.
GuestFileLock_nid_no_patchlocks the whole file (LockFileExfrom offset 0 forMAXDWORD:MAXDWORDbytes), and a Windows byte-range lock is enforced on every read and write that touches the range.Observed on
maine51428bf2, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release.firstandotherare twosceKernelOpen(path, O_RDWR)of one file;firstholds the lock:otherfirstholdsLOCK_EXfirstholdsLOCK_SHsceKernelPread0x80020005(EIO)sceKernelRead0x8002000D(EACCES)sceKernelPwrite0x800200050x80020005sceKernelWrite, also past the end0x8002000D0x8002000Dfgetcon a stream fromfopen(path, "r+")flock(other, LOCK_EX|LOCK_NB)I have not found a title that stops on this. It came out of running one probe of the file functions on the Windows build and on the Linux build and comparing the two outputs.
Fix, ready to cherry-pick.
DavidSamir:fix/libc-flock-advisory-windows(e4a1b54ad, three commits onmaine51428bf2, three files, +18/-3). I can't open it as a PR while #4532 is open (one open PR without a merged one).fix(libc): make flock advisory on Windows so other descriptors can read and write:FileLock.cpp.test(libc): read and write through another descriptor while a flock is held:guest_file_lock_conversion.docs(debt): describe the byte the Windows flock locks.Windows only. The lock and the unlock take one byte at offset 2^63 - 2 instead of the whole file. No file reaches that offset, so no read or write touches the locked range, while two
flockcalls still meet on the same byte: shared and exclusive locks exclude each other exactly as before, between descriptors, duplicates and processes. Nothing else in the function changes.Not changed: converting a lock from shared to exclusive still releases the first before taking the second, so another descriptor can take it in between; and descriptors that are not disk files fail as they do today.
guest_file_lock_conversiongains checks in each of its three duplicate kinds: with an exclusive lock held,sceKernelPwrite,sceKernelWriteandsceKernelPreadthrough the other open must succeed and read back what was written, andflock(LOCK_SH|LOCK_NB)through it must still fail with errno 35.Checked on Windows (e4a1b54ad on
maine51428bf2, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds):FileLock.cppas onmainand the test kept,guest_file_lock_conversionfails (exit 0xc0000409, the test'sRequireaborting). With the fix the sixguest_file_lock_*tests (duplicate,lifetime,conversion,threads,streams,process) pass 5/5 (--repeat until-fail:5), andguest_filesystem,guest_kernel_fcntl,guest_dup,guest_standard_streams,file_streamsandguest_file_vectorpass 3/3.processtakes the lock from a second process, so the byte is the same one across processes.main's libraries, the branch's and on the Linux build: ten lines change againstmain, the reads and writes in the table and a round of 2000 lock, contend, write, close, and with the branch every line equals the Linux build's. That includes each of pread, read, pwrite, write, a write past the end,ftruncate,fsync,fstatand a shared and an exclusiveflockattempt through the other open, with no lock, with an exclusive and with a shared lock held; the holder's own reads and writes; a stream; renaming onto and away from the locked file and unlinking it; and a blockedflockthat is granted when the holder unlocks 150 ms later.dup, unlocking through thedup, and closing one of the two: two lines change againstmain, the read and the write through the other open, and no lock line differs from the Linux build.fcntlstatus flags and 313 overfdopen, run with both sets of libraries: no line differs, apart from a racy file size in the last that changes from run to run with either set.main's libraries and 1.33 to 1.71 µs with the branch's; shared to exclusive and back 2.69 to 2.90 µs and 2.67 to 3.00 µs. No difference I can separate from the spread.poll: POLLPRI is not supported by WSAPollin both (#4478); its number of stderr lines varies from run to run with either set.ctest --parallel 4: 1026 of 1028 pass. The two that fail,agc_driver_bda_span_readsandagc_driver_pixel_interlock, fail the same way withmaine51428bf2built without this change on this machine (vkWaitForFences recorder: Vulkan result -4, RTX 3070); they do not touch this code.guest_file_lock_*tests pass 3/3.tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branchreports no overlap with open pull requests.Checked on Linux (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release):
libsbuilds, andguest_filesystemandguest_kernel_fcntlpass 3/3. The changed file and theguest_file_lock_*tests are Windows only, so this shows only that the Linux build is unaffected.Not checked on a console or with a commercial title.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-13T06:31:26Z.
Carried in #4958, keeping your three commits and authorship. I added a Linux run (Ubuntu 24.04, GCC 13.3): the build is clean and all 327
guest_*tests pass in two runs, and a MinGW-w64-fsyntax-only -Wall -Wextracompile ofFileLock.cppandGuestFileLock.cppis clean. The Windows results in the PR are yours from this issue. Thanks for the branch.AI-assisted: yes