fix(libkernel): lock directories with flock on Windows as FreeBSD does #12060
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12060"
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 Ragbie on 2026-10-27T18:15:44Z.
What
On Windows
flockon a directory descriptor returns 0 without taking a lock (#9101, which stopped it from aborting). Two opens of the same directory therefore never conflict. Afterflock(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 withEWOULDBLOCK. 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_flockonly requires a vnode, so a directory is locked like a file throughVOP_ADVLOCK(F_FLOCK). The lock belongs to the open file:dupshares it, and it is released at the last close.Windows
LockFileEx, which the file path uses, rejects a directory handle. SoGuestFileLock_nid_no_patchnow checks the handle withGetFileInformationByHandle, and for a directory it keeps the lock in a table of this process instead:ERROR_LOCK_VIOLATION, whichflockalready maps toEWOULDBLOCK.LockStatealready follows the open file:dupshares 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.lf_setlockreplaces the owner's own lock: a request conflicts only with other holders, a failedLOCK_EX | LOCK_NBupgrade keeps the shared lock, a downgrade is atomic, and a blocking upgrade releases the shared lock before it waits. Waiters wake on anotify_alland are not served in arrival order, unlike FreeBSD; the debt entry says so.flock_nid_postfixno longer returns early for a directory. The libkernel debt entry now records the remaining gap: the lock is visible only within this process. The libcflockentry says the same. Linux and macOS keep using the hostflock.Tested
Windows 11 Pro, Intel Core i5-14600KF, WinLibs GCC 15.2.0, full Release build:
guest_file_lock_directoryalready checked thatflockis accepted on a directory. It now also runs these cases:LOCK_EXheld, a second open of the directory cannot takeLOCK_EXorLOCK_SH.LOCK_SH, the second open can share the lock but cannot take it exclusively.LOCK_SH, an upgrade fails withEWOULDBLOCK.dupkeeps the lock after the original descriptor is closed.flockfrom another thread is still waiting after 200 ms and returns once the last reference is closed.LOCK_EX | LOCK_NBupgrade, the other open still cannot takeLOCK_EX.LOCK_EXreturns 0, the waiter keeps waiting, and a third open can share the lock.main's sources):guest_file_lock_directoryfails at the first conflict. The other sixguest_file_lock_*tests pass.guest_file_lock_*tests pass with--repeat until-fail:5.lock|directory|descriptor|dup|fcntl|close|open|filesystem|stream(withoutagc_andshader) pass with--repeat until-fail:3.ctestonmain228cd64b5:99% tests passed, 5 tests failed out of 2673.audio_out2_port_buffersandguest_video_out_high_frame_ratefailed only in the parallel run and pass alone (--repeat until-fail:3); neither usesflock.guest_wide_formattingandagc_driver_image_plane_address_on_linealso fail onmainon this machine.guest_wide_formattingfails because%Zgives the host's non-ASCII zone name, which #10987 addresses.agc_driver_mesh_barycentric_emulationfails every time on this machine's NVIDIA GPU withthe pixel shader did not emulate barycentrics. #9193 changed that test on 2026-10-27, and it does not use files.check_skips.pyreports 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
sceKernelGetProcessAbiTypeentry indocs/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'sflockentry unchanged. If #11939 goes first instead, this PR rebases after it andDepends onbecomes #11939.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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 --checkis 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 saysDepends on: none; that does not record an agreed order.CONTRIBUTING.mdrequires the authors to agree which PR goes first and declare that order withDepends 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.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T06:00:01Z.
Review: CHANGES_REQUESTED
Re-checked — changes requested
What I checked
f658dd8dadecdf8523c7b731d059c855cfa3499fagainst fetchedmain2960e9a6e7; the merge preview is clean andgit diff --checkpasses.Blocking items
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.main. The detailed Windows runs and integration suite in the description use older main228cd64b5. Please refresh onto currentmainand 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.