fix(libkernel): serialize positioned file transfers on Windows #4973

Merged
ducloc080202 merged 4 commits from upstream-pr/4973 into main 2026-10-13 22:28:15 +00:00
ducloc080202 commented 2026-10-13 06:58:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ducloc080202 on 2026-10-13T06:58:07Z.


What

Rebases #3861 (open but conflicting since Oct 12) onto current main. Serializes Windows file-position transfers with a mutex per descriptor, shared across duplicated descriptors: positioned transfers (NativePositioned_nid_no_patch), plain transfers (NativeTransfer, NativeRead, NativeWrite) and seeks (NativeLseek) of one descriptor can no longer interleave SetFilePointerEx positioning. Entries persist per descriptor number; a lock is only ever held alone, so no new lock order.

Supersedes #3861; that PR can be closed if this one is accepted.

Tested

  • Windows (MinGW 15.2.0, Release): full build passes; new guest_positioned_race (concurrent pread against read+lseek on one descriptor, verifying both sides) fails on main on the first run and passes 10/10 with the fix; file_position, guest_posix_filesystem, guest_filesystem, guest_file_vector, file_streams, guest_aio all pass (7/7). Linux/macOS rely on CI.
  • Cat Quest III (PPSA15065), Windows: loads all 454 asset bundles to the loading screen with no file-read errors. Linux/macOS rely on CI.

Checklist

  • Based on current main; no other open PR implements the same functions (rebases conflicting #3861; supersedes it)
  • 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 under Silent stubs in their library's technical debt file
  • 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: boykopovar/AnyPS5#4909 (needed to reach the loading screen; the title still stalls there)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4839581749 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4973). Originally posted by [ducloc080202](https://github.com/ducloc080202) on 2026-10-13T06:58:07Z. --- ### What Rebases #3861 (open but conflicting since Oct 12) onto current `main`. Serializes Windows file-position transfers with a mutex per descriptor, shared across duplicated descriptors: positioned transfers (`NativePositioned_nid_no_patch`), plain transfers (`NativeTransfer`, `NativeRead`, `NativeWrite`) and seeks (`NativeLseek`) of one descriptor can no longer interleave `SetFilePointerEx` positioning. Entries persist per descriptor number; a lock is only ever held alone, so no new lock order. Supersedes #3861; that PR can be closed if this one is accepted. ### Tested - Windows (MinGW 15.2.0, Release): full build passes; new `guest_positioned_race` (concurrent `pread` against `read`+`lseek` on one descriptor, verifying both sides) fails on `main` on the first run and passes 10/10 with the fix; `file_position`, `guest_posix_filesystem`, `guest_filesystem`, `guest_file_vector`, `file_streams`, `guest_aio` all pass (7/7). Linux/macOS rely on CI. - Cat Quest III (PPSA15065), Windows: loads all 454 asset bundles to the loading screen with no file-read errors. Linux/macOS rely on CI. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions (rebases conflicting #3861; supersedes it) - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical debt file - [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: boykopovar/AnyPS5#4909 (needed to reach the loading screen; the title still stalls there) - [x] AI-assisted: yes
oneandonlydean commented 2026-10-13 08:05:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T08:05:03Z.


Taking this one over #3861. Three things before it can go in:

  • The branch has a merge commit (e2e4ba2a0); please rebase the original commit onto main instead, keeping its author.
  • Open.cpp:46-48: NativeLseek can still run between the save and the restore in NativePositioned_nid_no_patch (Stdio.cpp:186-197), and the restore then overwrites the seek. Take the same lock there.
  • A Windows test that runs pread against read and lseek on one descriptor from two threads and fails on main.

A lock per descriptor instead of one for all files would be better, as asked on #3861.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6141137174 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4973#issuecomment-6141137174). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-13T08:05:03Z. --- Taking this one over #3861. Three things before it can go in: - The branch has a merge commit (`e2e4ba2a0`); please rebase the original commit onto main instead, keeping its author. - `Open.cpp:46-48`: `NativeLseek` can still run between the save and the restore in `NativePositioned_nid_no_patch` (`Stdio.cpp:186-197`), and the restore then overwrites the seek. Take the same lock there. - A Windows test that runs `pread` against `read` and `lseek` on one descriptor from two threads and fails on main. A lock per descriptor instead of one for all files would be better, as asked on #3861.
ducloc080202 commented 2026-10-13 08:37:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ducloc080202 on 2026-10-13T08:37:51Z.


Done, pushed as 31bcf985a..e69306fee (branch rebased, no merge commit): 1) original commit cherry-picked with its author kept; 2) the same per-descriptor lock now covers NativeLseek, so a seek between the positioned save and restore can no longer clobber either side; 3) new guest_positioned_race test (pread vs read+lseek on one descriptor, both sides verified) fails on main first run, passes 10/10 with the fix; 4) one mutex per descriptor number, shared across dup, entries persist so close/reuse cannot strand a waiter. The debt entry for the Windows positioned race now describes the serialized behavior.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6141663560 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4973#issuecomment-6141663560). Originally posted by [ducloc080202](https://github.com/ducloc080202) on 2026-10-13T08:37:51Z. --- Done, pushed as 31bcf985a..e69306fee (branch rebased, no merge commit): 1) original commit cherry-picked with its author kept; 2) the same per-descriptor lock now covers NativeLseek, so a seek between the positioned save and restore can no longer clobber either side; 3) new guest_positioned_race test (pread vs read+lseek on one descriptor, both sides verified) fails on main first run, passes 10/10 with the fix; 4) one mutex per descriptor number, shared across dup, entries persist so close/reuse cannot strand a waiter. The debt entry for the Windows positioned race now describes the serialized behavior.
nicolasfalesy commented 2026-10-13 09:04:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nicolasfalesy on 2026-10-13T09:04:48Z.


Linux build (Arch, GCC 16.2.1, Release, BUILD_TESTING=ON), head f9881adaa merged alone onto main 4bb2c7f39: 592 targets fail to link, everything that links libkernel.prx:

/usr/bin/ld: core/libs/libs/unpatched/libkernel.prx: undefined reference to `File::SharePositionedTransfer(int, int)'

File::SharePositionedTransfer is defined in File/src/Stdio.cpp inside the #ifdef _WIN32 block that starts at line 50, but ShareDescriptor in File/src/DescriptorControl.cpp now calls it on every platform (the call sits before that function's own #ifdef _WIN32). Either guarding the call with #ifdef _WIN32 like the GuestFileLockShare_nid_no_patch line below it, or giving Linux an empty definition, should fix it. The CI linux job is skipped for this PR, so it would not show there.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6142126989 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4973#issuecomment-6142126989). Originally posted by [nicolasfalesy](https://github.com/nicolasfalesy) on 2026-10-13T09:04:48Z. --- Linux build (Arch, GCC 16.2.1, Release, `BUILD_TESTING=ON`), head `f9881adaa` merged alone onto `main` 4bb2c7f39: **592 targets fail to link**, everything that links `libkernel.prx`: ``` /usr/bin/ld: core/libs/libs/unpatched/libkernel.prx: undefined reference to `File::SharePositionedTransfer(int, int)' ``` `File::SharePositionedTransfer` is defined in `File/src/Stdio.cpp` inside the `#ifdef _WIN32` block that starts at line 50, but `ShareDescriptor` in `File/src/DescriptorControl.cpp` now calls it on every platform (the call sits before that function's own `#ifdef _WIN32`). Either guarding the call with `#ifdef _WIN32` like the `GuestFileLockShare_nid_no_patch` line below it, or giving Linux an empty definition, should fix it. The CI `linux` job is skipped for this PR, so it would not show there.
xXPBSXx commented 2026-10-13 12:00:21 +00:00 (Migrated from github.com)

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


Windows counter-test (follow-up): Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, this PR merged onto main 2a2a3dc65. guest_positioned_race passes with the PR; with the PR's source files replaced by main's, same test, it aborts at line 74.

Not tested: Linux, a console, a title run.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6145053731 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4973#issuecomment-6145053731). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T12:00:21Z. --- Windows counter-test (follow-up): Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, this PR merged onto `main` `2a2a3dc65`. `guest_positioned_race` passes with the PR; with the PR's source files replaced by `main`'s, same test, it aborts at line 74. Not tested: Linux, a console, a title run. AI-assisted (Claude Code).
p3n1cillin commented 2026-10-13 21:03:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by p3n1cillin on 2026-10-13T21:03:01Z.


Data point from a third Unity title, for scope: Cult of the Lamb (PPSA06465) also faults intermittently on Loading.PreloadManager at start-up on Windows. This PR merged onto main a0f5916a2 doesn't change its rate: in 8 alternated 40 s runs per side, there were 5 faults with the PR and 4 without. So its fault is a different race from the one fixed here; details in https://github.com/boykopovar/AnyPS5/issues/5490. This says nothing against the PR for Hotline Miami or Cat Quest III.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6155143647 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4973#issuecomment-6155143647). Originally posted by [p3n1cillin](https://github.com/p3n1cillin) on 2026-10-13T21:03:01Z. --- Data point from a third Unity title, for scope: Cult of the Lamb (PPSA06465) also faults intermittently on `Loading.PreloadManager` at start-up on Windows. This PR merged onto `main` `a0f5916a2` doesn't change its rate: in 8 alternated 40 s runs per side, there were 5 faults with the PR and 4 without. So its fault is a different race from the one fixed here; details in https://github.com/boykopovar/AnyPS5/issues/5490. This says nothing against the PR for Hotline Miami or Cat Quest III.
oneandonlydean commented 2026-10-13 22:20:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T22:20:04Z.


Rebased onto main (bf8dc610e6) and force-pushed. The only conflict was the include list in File/src/Stdio.cpp, which now keeps both <unordered_map> and <utility>. Your three commits keep their authors.
Added one commit: DescriptorControl.cpp:568 now calls File::SharePositionedTransfer under _WIN32, next to GuestFileLockShare_nid_no_patch. The lock is only defined in the Windows part of Stdio.cpp, so libkernel didn't link on Linux. Windows behaviour is unchanged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6156434447 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4973#issuecomment-6156434447). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-13T22:20:04Z. --- Rebased onto main (bf8dc610e6) and force-pushed. The only conflict was the include list in `File/src/Stdio.cpp`, which now keeps both `<unordered_map>` and `<utility>`. Your three commits keep their authors. Added one commit: `DescriptorControl.cpp:568` now calls `File::SharePositionedTransfer` under `_WIN32`, next to `GuestFileLockShare_nid_no_patch`. The lock is only defined in the Windows part of `Stdio.cpp`, so libkernel didn't link on Linux. Windows behaviour is unchanged.
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!4973
No description provided.