libkernel: flock on a directory descriptor aborts the process on Windows, where FreeBSD succeeds; fix branch ready #9100

Closed
opened 2026-10-21 12:16:18 +00:00 by xXPBSXx · 0 comments
xXPBSXx commented 2026-10-21 12:16:18 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T12:16:18Z.


On Windows, flock(fd, LOCK_SH) on a descriptor opened on a directory (open(path, O_DIRECTORY)) throws std::runtime_error (flock failed ... error=87) and the process aborts. FreeBSD 11.4's sys_flock only rejects descriptors that are not vnodes (EOPNOTSUPP) and an invalid how (EBADF), so flock on a directory returns 0. A caller that locks a directory it opened, which some programs do for a lock directory, crashes instead of getting a lock.

Cause. flock_nid_postfix passes every descriptor to GuestFileLock, which calls LockFileEx; Windows rejects a directory handle there with ERROR_INVALID_PARAMETER (87), and the function turns any unexpected failure into a runtime_error.

Observed on main 3e91c8edf5, Windows 10 (10.0.19045), WinLibs GCC 15.2.0 r7, Release (probe calling flock_nid_postfix on a directory descriptor, other calls from the same probe for comparison):

call main on Windows FreeBSD 11.4 source
flock(pipe, LOCK_SH) -1, EOPNOTSUPP (45) EOPNOTSUPP
flock(file, 0) / flock(file, LOCK_NB) -1, EBADF (9) EBADF
flock(file, LOCK_SH | LOCK_EX) 0 0
flock(dir, LOCK_SH) process abort (runtime_error, error 87) 0

I have not found a title that stops on this.

Fix. xXPBSXx:fix/flock-directory-windows (0e2252f638, one commit on main 3e91c8edf5, four files, +23/-1). A directory descriptor returns 0 on Windows without taking a lock; the debt entry records that two descriptors of one directory never conflict (a second LOCK_EX | LOCK_NB succeeds where FreeBSD fails with EWOULDBLOCK). The new guest_file_lock_directory case fails without the change (exit code 0xc0000409, 3 of 3) and passes with it (3 of 3); the other guest_file_lock_* tests and the filesystem, fcntl, dup and append tests pass (13 of 13).

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5960294527 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/9100). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T12:16:18Z. --- On Windows, `flock(fd, LOCK_SH)` on a descriptor opened on a directory (`open(path, O_DIRECTORY)`) throws `std::runtime_error` (`flock failed ... error=87`) and the process aborts. FreeBSD 11.4's [`sys_flock`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/kern/kern_descrip.c#L2889-L2930) only rejects descriptors that are not vnodes (`EOPNOTSUPP`) and an invalid `how` (`EBADF`), so `flock` on a directory returns 0. A caller that locks a directory it opened, which some programs do for a lock directory, crashes instead of getting a lock. **Cause.** [`flock_nid_postfix`](https://github.com/boykopovar/AnyPS5/blob/3e91c8edf5/core/libs/prx/libkernel/File/src/Stdio.cpp#L832-L862) passes every descriptor to `GuestFileLock`, which calls `LockFileEx`; Windows rejects a directory handle there with `ERROR_INVALID_PARAMETER` (87), and the function turns any unexpected failure into a `runtime_error`. Observed on `main` 3e91c8edf5, Windows 10 (10.0.19045), WinLibs GCC 15.2.0 r7, Release (probe calling `flock_nid_postfix` on a directory descriptor, other calls from the same probe for comparison): | call | main on Windows | FreeBSD 11.4 source | |---|---|---| | `flock(pipe, LOCK_SH)` | -1, EOPNOTSUPP (45) | EOPNOTSUPP | | `flock(file, 0)` / `flock(file, LOCK_NB)` | -1, EBADF (9) | EBADF | | `flock(file, LOCK_SH \| LOCK_EX)` | 0 | 0 | | `flock(dir, LOCK_SH)` | process abort (`runtime_error`, error 87) | 0 | I have not found a title that stops on this. **Fix.** [`xXPBSXx:fix/flock-directory-windows`](https://github.com/boykopovar/AnyPS5/compare/main...xXPBSXx:AnyPS5:fix/flock-directory-windows) (0e2252f638, one commit on `main` 3e91c8edf5, four files, +23/-1). A directory descriptor returns 0 on Windows without taking a lock; the debt entry records that two descriptors of one directory never conflict (a second `LOCK_EX | LOCK_NB` succeeds where FreeBSD fails with `EWOULDBLOCK`). The new `guest_file_lock_directory` case fails without the change (exit code 0xc0000409, 3 of 3) and passes with it (3 of 3); the other `guest_file_lock_*` tests and the filesystem, fcntl, dup and append tests pass (13 of 13). AI-assisted (Claude Code).
Sign in to join this conversation.
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#9100
No description provided.