fix(libs): share Windows flock state between duplicated descriptors #4516

Merged
Ondra97 merged 1 commit from upstream-pr/4516 into main 2026-10-12 18:30:49 +00:00
Ondra97 commented 2026-10-12 14:09:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-12T14:09:33Z.


What

On Windows, flock kept its state per descriptor. After dup, dup2 or F_DUPFD, LOCK_UN through the copy returned success but left the file locked, and a repeated exclusive lock through the copy returned EWOULDBLOCK or waited on its own lock. fclose also left stale state behind: a new file reusing that descriptor could report a successful lock without acquiring one.

Share one lock state between descriptor aliases and serialize its transitions. Keep independent opens separate and discard a descriptor's reference when it is closed or its stream is reopened, including a failed freopen. The shared owner lives in libc so both stdio and libkernel can reach it; the existing libkernel helpers delegate to it. Blocking lock calls hold only that open file description's mutex. The Linux path continues to use the host's flock.

Six Windows regression cases cover duplication before and after acquisition, alias chains, last-close release, descriptor reuse, dup2 replacement, repeated locking, shared/exclusive conversions and failed upgrades, concurrent callers, independent blocking waits, fdopen, fclose, successful and failed freopen, and contention between separate processes. The helper exports use _nid_no_patch.

Tested

Windows, Release, supported WinLibs MinGW-w64 GCC 15.2.0 (x86_64-ucrt-posix-seh, r7). No title-specific run.

  • All six new regression cases failed with the original implementation; all pass with the fix. Each also passed ten consecutive runs.
  • An independent 176-check matrix changed from 59 incorrect results to 176 correct results, including direct Win32 controls and separate processes.
  • Full build: cmake --build build-runtime --parallel 8.
  • Full CTest run: ctest --test-dir build-runtime --output-on-failure --parallel 4 --timeout 120; 967 passed, 7 expected skips, 0 failures across 974 registered tests.
  • tools/check_skips.py --job windows, tools/check_test_registration.py, tools/check_conventions.py --base origin/main, and tools/pr_overlap.py --branch HEAD --repo boykopovar/AnyPS5 passed. No overlap with open pull requests.

Validation for this PR is limited to the Windows runtime. Full Linux runtime build and tests were not run and are not planned for this PR. The new lock manager and lifecycle hooks are guarded by _WIN32; Linux flock continues to use the host implementation.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4830611664 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4516). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-10-12T14:09:33Z. --- ### What On Windows, `flock` kept its state per descriptor. After `dup`, `dup2` or `F_DUPFD`, `LOCK_UN` through the copy returned success but left the file locked, and a repeated exclusive lock through the copy returned `EWOULDBLOCK` or waited on its own lock. `fclose` also left stale state behind: a new file reusing that descriptor could report a successful lock without acquiring one. Share one lock state between descriptor aliases and serialize its transitions. Keep independent opens separate and discard a descriptor's reference when it is closed or its stream is reopened, including a failed `freopen`. The shared owner lives in libc so both stdio and libkernel can reach it; the existing libkernel helpers delegate to it. Blocking lock calls hold only that open file description's mutex. The Linux path continues to use the host's `flock`. Six Windows regression cases cover duplication before and after acquisition, alias chains, last-close release, descriptor reuse, `dup2` replacement, repeated locking, shared/exclusive conversions and failed upgrades, concurrent callers, independent blocking waits, `fdopen`, `fclose`, successful and failed `freopen`, and contention between separate processes. The helper exports use `_nid_no_patch`. ### Tested Windows, Release, supported WinLibs MinGW-w64 GCC 15.2.0 (`x86_64-ucrt-posix-seh`, r7). No title-specific run. - All six new regression cases failed with the original implementation; all pass with the fix. Each also passed ten consecutive runs. - An independent 176-check matrix changed from 59 incorrect results to 176 correct results, including direct Win32 controls and separate processes. - Full build: `cmake --build build-runtime --parallel 8`. - Full CTest run: `ctest --test-dir build-runtime --output-on-failure --parallel 4 --timeout 120`; 967 passed, 7 expected skips, 0 failures across 974 registered tests. - `tools/check_skips.py --job windows`, `tools/check_test_registration.py`, `tools/check_conventions.py --base origin/main`, and `tools/pr_overlap.py --branch HEAD --repo boykopovar/AnyPS5` passed. No overlap with open pull requests. Validation for this PR is limited to the Windows runtime. Full Linux runtime build and tests were not run and are not planned for this PR. The new lock manager and lifecycle hooks are guarded by `_WIN32`; Linux `flock` continues to use the host implementation. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes
xXPBSXx commented 2026-10-12 16:31:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T16:31:31Z.


Windows test: head bd842bc7d merged onto current main d592d54e1 (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, -j2: full build 0 errors; ctest with the filter guest_file_lock: guest_file_lock_duplicate passed, guest_file_lock_lifetime passed, guest_file_lock_conversion passed, guest_file_lock_threads passed, guest_file_lock_streams passed, guest_file_lock_process passed (6 test(s), 0 failed); python tools/check_conventions.py --base upstream/main passed. I did not run the new tests against main without the change.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6128391369 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4516#issuecomment-6128391369). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-12T16:31:31Z. --- Windows test: head `bd842bc7d` merged onto current `main` `d592d54e1` (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, `-j2`: full build 0 errors; ctest with the filter `guest_file_lock`: guest_file_lock_duplicate passed, guest_file_lock_lifetime passed, guest_file_lock_conversion passed, guest_file_lock_threads passed, guest_file_lock_streams passed, guest_file_lock_process passed (6 test(s), 0 failed); `python tools/check_conventions.py --base upstream/main` passed. I did not run the new tests against `main` without the change. AI-assisted (Claude Code).
github-actions[bot] commented 2026-10-12 16:40:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T16:40:51Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#3694 docs/dev/TechnicalDebt.md L726
#3720 docs/dev/TechnicalDebt.md L726
#3980 docs/dev/TechnicalDebt.md L726
#4197 docs/dev/TechnicalDebt.md L726
#4254 docs/dev/TechnicalDebt.md L726
#4374 docs/dev/TechnicalDebt.md L726
#4376 docs/dev/TechnicalDebt.md L726
#4378 docs/dev/TechnicalDebt.md L726
#4379 docs/dev/TechnicalDebt.md L726
#4384 docs/dev/TechnicalDebt.md L726
#4386 docs/dev/TechnicalDebt.md L726
#4640 core/libs/prx/libc/include/FileStream.hpp L14, L78-79
#4651 docs/dev/TechnicalDebt.md L726
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6128535489 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4516#issuecomment-6128535489). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T16:40:51Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #3694 | `docs/dev/TechnicalDebt.md` [L726](https://github.com/boykopovar/AnyPS5/blob/4756c051eff1c702dbfc678ddba314b19770fae6/docs/dev/TechnicalDebt.md#L726) | | | | #3720 | `docs/dev/TechnicalDebt.md` [L726](https://github.com/boykopovar/AnyPS5/blob/4756c051eff1c702dbfc678ddba314b19770fae6/docs/dev/TechnicalDebt.md#L726) | | | | #3980 | `docs/dev/TechnicalDebt.md` [L726](https://github.com/boykopovar/AnyPS5/blob/4756c051eff1c702dbfc678ddba314b19770fae6/docs/dev/TechnicalDebt.md#L726) | | | | #4197 | `docs/dev/TechnicalDebt.md` [L726](https://github.com/boykopovar/AnyPS5/blob/4756c051eff1c702dbfc678ddba314b19770fae6/docs/dev/TechnicalDebt.md#L726) | | | | #4254 | `docs/dev/TechnicalDebt.md` [L726](https://github.com/boykopovar/AnyPS5/blob/4756c051eff1c702dbfc678ddba314b19770fae6/docs/dev/TechnicalDebt.md#L726) | | | | #4374 | `docs/dev/TechnicalDebt.md` [L726](https://github.com/boykopovar/AnyPS5/blob/4756c051eff1c702dbfc678ddba314b19770fae6/docs/dev/TechnicalDebt.md#L726) | | | | #4376 | `docs/dev/TechnicalDebt.md` [L726](https://github.com/boykopovar/AnyPS5/blob/4756c051eff1c702dbfc678ddba314b19770fae6/docs/dev/TechnicalDebt.md#L726) | | | | #4378 | `docs/dev/TechnicalDebt.md` [L726](https://github.com/boykopovar/AnyPS5/blob/4756c051eff1c702dbfc678ddba314b19770fae6/docs/dev/TechnicalDebt.md#L726) | | | | #4379 | `docs/dev/TechnicalDebt.md` [L726](https://github.com/boykopovar/AnyPS5/blob/4756c051eff1c702dbfc678ddba314b19770fae6/docs/dev/TechnicalDebt.md#L726) | | | | #4384 | `docs/dev/TechnicalDebt.md` [L726](https://github.com/boykopovar/AnyPS5/blob/4756c051eff1c702dbfc678ddba314b19770fae6/docs/dev/TechnicalDebt.md#L726) | | | | #4386 | `docs/dev/TechnicalDebt.md` [L726](https://github.com/boykopovar/AnyPS5/blob/4756c051eff1c702dbfc678ddba314b19770fae6/docs/dev/TechnicalDebt.md#L726) | | | | #4640 | `core/libs/prx/libc/include/FileStream.hpp` [L14](https://github.com/boykopovar/AnyPS5/blob/4756c051eff1c702dbfc678ddba314b19770fae6/core/libs/prx/libc/include/FileStream.hpp#L14), [L78-79](https://github.com/boykopovar/AnyPS5/blob/4756c051eff1c702dbfc678ddba314b19770fae6/core/libs/prx/libc/include/FileStream.hpp#L78-L79) | | | | #4651 | `docs/dev/TechnicalDebt.md` [L726](https://github.com/boykopovar/AnyPS5/blob/4756c051eff1c702dbfc678ddba314b19770fae6/docs/dev/TechnicalDebt.md#L726) | | |
Sign in to join this conversation.
No reviewers
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!4516
No description provided.