Windows: flock does not follow BSD semantics; LOCK_UN without a lock aborts the process #1754

Closed
opened 2026-10-08 18:33:40 +00:00 by Zurisen · 0 comments
Zurisen commented 2026-10-08 18:33:40 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by Zurisen on 2026-10-08T18:33:40Z.


On main (17350cf5), flock_nid_postfix on Windows maps each call onto LockFileEx/UnlockFileEx over the whole file (Stdio.cpp). Windows byte-range locks stack per handle and are not converted, so the result differs from FreeBSD flock(2), where a descriptor holds at most one lock that a later call converts or releases. Linux calls the host flock and already behaves like FreeBSD. Not specific to one title.

Call sequence on one descriptor FreeBSD / Linux Windows on main
LOCK_UN without a lock 0 UnlockFileEx fails with ERROR_NOT_LOCKED (158); flock_nid_postfix throws and the process aborts
LOCK_EX, LOCK_EX 0 second call blocks forever; with LOCK_NB, fails with ERROR_LOCK_VIOLATION (33) and throws
LOCK_SH, LOCK_EX | LOCK_NB 0 (converted) fails with 33 and throws
LOCK_SH, LOCK_SH, LOCK_UN unlocked one shared lock still held; another descriptor cannot take LOCK_EX

Measured on Windows 11 (10.0.26200), MinGW-w64 GCC 15.2.0, with guest_filesystem_tests:

terminate called after throwing an instance of 'std::runtime_error'
  what():  flock_nid_postfix: flock failed, fd=3, error=158

Also: when _get_osfhandle fails, errno is not set.

Fixed in #1755.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5766998859 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1754). Originally posted by [Zurisen](https://github.com/Zurisen) on 2026-10-08T18:33:40Z. --- On main (`17350cf5`), `flock_nid_postfix` on Windows maps each call onto `LockFileEx`/`UnlockFileEx` over the whole file ([Stdio.cpp](https://github.com/boykopovar/AnyPS5/blob/17350cf5/core/libs/prx/libkernel/File/src/Stdio.cpp#L86-L98)). Windows byte-range locks stack per handle and are not converted, so the result differs from FreeBSD `flock(2)`, where a descriptor holds at most one lock that a later call converts or releases. Linux calls the host `flock` and already behaves like FreeBSD. Not specific to one title. | Call sequence on one descriptor | FreeBSD / Linux | Windows on main | |---|---|---| | `LOCK_UN` without a lock | 0 | `UnlockFileEx` fails with `ERROR_NOT_LOCKED` (158); `flock_nid_postfix` throws and the process aborts | | `LOCK_EX`, `LOCK_EX` | 0 | second call blocks forever; with `LOCK_NB`, fails with `ERROR_LOCK_VIOLATION` (33) and throws | | `LOCK_SH`, `LOCK_EX \| LOCK_NB` | 0 (converted) | fails with 33 and throws | | `LOCK_SH`, `LOCK_SH`, `LOCK_UN` | unlocked | one shared lock still held; another descriptor cannot take `LOCK_EX` | Measured on Windows 11 (10.0.26200), MinGW-w64 GCC 15.2.0, with `guest_filesystem_tests`: ``` terminate called after throwing an instance of 'std::runtime_error' what(): flock_nid_postfix: flock failed, fd=3, error=158 ``` Also: when `_get_osfhandle` fails, `errno` is not set. Fixed in #1755.
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#1754
No description provided.