fix(libkernel): report an ended adopted host thread as gone on Windows #14190

Merged
JJenkx merged 3 commits from upstream-pr/14190 into main 2026-11-09 20:17:12 +00:00
JJenkx commented 2026-10-31 22:44:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-31T22:44:43Z.


What

On Windows, scePthreadAttrGet and pthread_attr_get_np kept answering 0 for a host thread the guest had adopted, even after that thread ended.

The guest adopts a host thread when it calls scePthreadSelf on a thread it never created. On Windows that path:

  • duplicates the thread's handle;
  • creates a PthreadPrivate, which registers itself in the live-thread registry;
  • deliberately leaks it (adopted.release()).

When the host thread ended without scePthreadExit, nothing marked that record finished or removed it, so both calls kept answering 0. FreeBSD 11.4's _pthread_attr_get_np answers ESRCH for a thread _thr_find_thread no longer finds.

On Linux the adopted record is a thread_local unique_ptr. Its destructor removes it from the registry when the thread ends, so the same calls already answer ESRCH there.

The "scePthreadAttrGet and pthread_attr_get_np" debt entry notes that Monster Hunter Wilds (PPSA24191) reuses a slot of its thread table only when scePthreadAttrGet returns 0x80020003. On Windows, an adopted thread's slot would never become free.

The change:

  • Adopted Windows threads get a hostAdopted flag.
  • LockLiveGuestThread now treats such a thread as ended once its duplicated handle is signalled (WaitForSingleObject(handle, 0) == WAIT_OBJECT_0). That handle is never closed for an adopted thread, so the check is always valid.
  • A shared GuestThreadEnded applies the same answer to every registry scan in Thread.cpp, after review: the by-id affinity and scheduling lookups and setters, LiveGuestThreadExists, the affinity mask scans, the main-thread lookup and the CPU time lookup.
  • Guest-created threads keep the existing _finished check, and Linux and macOS are unchanged.
  • The Windows sentence is removed from that debt entry.

Tested

Current-main integration — 2026-11-09 UTC. Head 56c7372672508f17d29c4922567d75e4ba6aa587 preserves public head d9ba1fbf5aadf2b9800048c10d274cf4e5c7eda8 and integrates pinned main 7259cce11195f360698772da8c0b3af756bf7c8f. Conflict resolution retains both the topic and main's newer source/test/debt changes; maintainer commits and attribution remain intact.

Linux x86-64, GCC 16.2.1, Ninja Release: actual libc/libkernel and focused fixture owners rebuilt. 12/12 CPU executions pass, zero skips (4 groups × three repetitions): guest_pthread_attr, guest_pthread_attr_stack, guest_pthread_attr_errors, guest_pthread_schedparam. public_headers, conventions and whitespace checks pass. The matching plain-main control passed 102 selected CPU executions and its header audit. CPU fixtures execute unpatched production libraries; this is not a full SDK or patched-title runtime qualification.

The Linux attribute/scheduling fixtures do not execute the Windows-only adopted-handle check. Its production additions/removals are unchanged from the previously tested public head; older Windows evidence retains its original head and base. No new Windows/macOS, console, GPU, title or performance result is claimed. Source checks preserve the existing production topic; historical reports below retain their original pins.

Build/test logs, exact heads, source/ancestry checks and provider pins, SHA-256 52727be6594b9c15bf9a5535fef374b985e9526aabe5f3ef5cf5857dfeeb1a0d. The public archive and all 178 manifest files were download/hash verified.

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, full build with BUILD_TESTING=ON, on main 7a5d9bf02a.

  • New check in guest_pthread_attr CheckEndedThreads: a std::thread calls scePthreadSelf() and returns, and the test joins it. scePthreadAttrGet must then report ESRCH within 5 s (the existing ReportedEnded helper), and so must pthread_attr_get_np.
    • With this change: guest_pthread_attr, guest_pthread_attr_stack and guest_pthread_attr_errors pass 3/3.
    • With Thread.cpp restored to main (the control build relinked libkernel.prx): guest_pthread_attr aborts after 5.65 s, because the adopted thread is still reported live.
  • Review follow-up d9ba1fbf5a: the check also requires cpuset_getaffinity with the ended thread's id to fail with ESRCH. It aborts with Thread.cpp at 7122664e7e and passes 3/3 with the follow-up. ctest -R "pthread|thread|kernel|mutex|cond|sema|equeue|cpuset|affinity|sched" -j8 --repeat until-fail:3 passes 169/169.
  • Regression set: ctest -R "pthread|thread|kernel|mutex|cond|sema|equeue" -j8 --repeat until-fail:3: 167/167 pass.
  • Not run:
    • Linux and macOS. The new check should already pass there through the thread_local record's destructor, but I have not run it; CI will.
    • Monster Hunter Wilds.

Independent reports on this head (their runs, summarised here):

  • @bunbeans2-arch (report): Windows 11 Pro, MSYS2 UCRT64 GCC 16.1.0, head d9ba1fbf5a merged into main 5bae1a4bc2: guest_pthread_attr passes 5/5; with Thread.cpp and Pthread.hpp from main and this test kept it aborts 5/5.

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 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5102241153 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14190). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-31T22:44:43Z. --- ### What On Windows, `scePthreadAttrGet` and `pthread_attr_get_np` kept answering 0 for a host thread the guest had adopted, even after that thread ended. The guest adopts a host thread when it calls `scePthreadSelf` on a thread it never created. On Windows that path: - duplicates the thread's handle; - creates a `PthreadPrivate`, which registers itself in the live-thread registry; - deliberately leaks it (`adopted.release()`). When the host thread ended without `scePthreadExit`, nothing marked that record finished or removed it, so both calls kept answering 0. FreeBSD 11.4's `_pthread_attr_get_np` answers `ESRCH` for a thread `_thr_find_thread` no longer finds. On Linux the adopted record is a `thread_local` `unique_ptr`. Its destructor removes it from the registry when the thread ends, so the same calls already answer `ESRCH` there. The "scePthreadAttrGet and pthread_attr_get_np" debt entry notes that Monster Hunter Wilds (PPSA24191) reuses a slot of its thread table only when `scePthreadAttrGet` returns `0x80020003`. On Windows, an adopted thread's slot would never become free. The change: - Adopted Windows threads get a `hostAdopted` flag. - `LockLiveGuestThread` now treats such a thread as ended once its duplicated handle is signalled (`WaitForSingleObject(handle, 0) == WAIT_OBJECT_0`). That handle is never closed for an adopted thread, so the check is always valid. - A shared `GuestThreadEnded` applies the same answer to every registry scan in `Thread.cpp`, after review: the by-id affinity and scheduling lookups and setters, `LiveGuestThreadExists`, the affinity mask scans, the main-thread lookup and the CPU time lookup. - Guest-created threads keep the existing `_finished` check, and Linux and macOS are unchanged. - The Windows sentence is removed from that debt entry. ### Tested <!-- codex-maintenance-20261109-cpu-14190 --> **Current-main integration — 2026-11-09 UTC.** Head `56c7372672508f17d29c4922567d75e4ba6aa587` preserves public head `d9ba1fbf5aadf2b9800048c10d274cf4e5c7eda8` and integrates pinned main `7259cce11195f360698772da8c0b3af756bf7c8f`. Conflict resolution retains both the topic and main's newer source/test/debt changes; maintainer commits and attribution remain intact. Linux x86-64, GCC 16.2.1, Ninja Release: actual libc/libkernel and focused fixture owners rebuilt. **12/12 CPU executions pass, zero skips** (4 groups × three repetitions): `guest_pthread_attr`, `guest_pthread_attr_stack`, `guest_pthread_attr_errors`, `guest_pthread_schedparam`. `public_headers`, conventions and whitespace checks pass. The matching plain-main control passed 102 selected CPU executions and its header audit. CPU fixtures execute unpatched production libraries; this is not a full SDK or patched-title runtime qualification. The Linux attribute/scheduling fixtures do not execute the Windows-only adopted-handle check. Its production additions/removals are unchanged from the previously tested public head; older Windows evidence retains its original head and base. No new Windows/macOS, console, GPU, title or performance result is claimed. Source checks preserve the existing production topic; historical reports below retain their original pins. [Build/test logs, exact heads, source/ancestry checks and provider pins](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/cpu8-main7259-linux-20261109.tar.gz), SHA-256 `52727be6594b9c15bf9a5535fef374b985e9526aabe5f3ef5cf5857dfeeb1a0d`. The public archive and all 178 manifest files were download/hash verified. Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, full build with `BUILD_TESTING=ON`, on `main` `7a5d9bf02a`. - **New check in `guest_pthread_attr` `CheckEndedThreads`:** a `std::thread` calls `scePthreadSelf()` and returns, and the test joins it. `scePthreadAttrGet` must then report `ESRCH` within 5 s (the existing `ReportedEnded` helper), and so must `pthread_attr_get_np`. - With this change: `guest_pthread_attr`, `guest_pthread_attr_stack` and `guest_pthread_attr_errors` pass 3/3. - With `Thread.cpp` restored to `main` (the control build relinked `libkernel.prx`): `guest_pthread_attr` aborts after 5.65 s, because the adopted thread is still reported live. - **Review follow-up `d9ba1fbf5a`:** the check also requires `cpuset_getaffinity` with the ended thread's id to fail with `ESRCH`. It aborts with `Thread.cpp` at `7122664e7e` and passes 3/3 with the follow-up. `ctest -R "pthread|thread|kernel|mutex|cond|sema|equeue|cpuset|affinity|sched" -j8 --repeat until-fail:3` passes 169/169. - **Regression set:** `ctest -R "pthread|thread|kernel|mutex|cond|sema|equeue" -j8 --repeat until-fail:3`: 167/167 pass. - **Not run:** - Linux and macOS. The new check should already pass there through the `thread_local` record's destructor, but I have not run it; CI will. - Monster Hunter Wilds. **Independent reports** on this head (their runs, summarised here): - @bunbeans2-arch ([report](https://github.com/boykopovar/AnyPS5/pull/14190#issuecomment-6564559563)): Windows 11 Pro, MSYS2 UCRT64 GCC 16.1.0, head `d9ba1fbf5a` merged into `main` `5bae1a4bc2`: `guest_pthread_attr` passes 5/5; with `Thread.cpp` and `Pthread.hpp` from `main` and this test kept it aborts 5/5. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-31 23:06:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-31T23:06:44Z.

Review: APPROVED


Reviewed — looks good

The Windows-specific handle check now recognizes an adopted host thread as ended, and the exact-head Windows CI run passed the test that covers it.

What I ran

Reviewed head 7122664e7e93e8de67af2e611659937d8124c190 against current main 381891927c2b3ff494a9853cb79fde8d5342b92d; the merge preview is clean. Environment: Debian Linux x86_64, glibc 2.36, Python 3.11.2, GCC/G++ 12.2.0. I did not build locally.

git diff --check 381891927c2b3ff494a9853cb79fde8d5342b92d..refs/review/pr-14190-head
python3 tools/check_conventions.py --base 381891927c2b3ff494a9853cb79fde8d5342b92d --head refs/review/pr-14190-head
git merge-tree --write-tree 381891927c2b3ff494a9853cb79fde8d5342b92d refs/review/pr-14190-head
python3 tools/pr_precheck.py --repo boykopovar/AnyPS5

Results

Check Result
Diff check Passed (exit 0; no output).
Conventions and PR template Passed locally and in hosted CI.
Current-main merge preview Clean.
ABI lexical audit No changed exports; the audit reports one warning for ResetOnce, which is a static function and therefore intentionally has no public header declaration.
Test-quality heuristic It flags the changed C++ test as possibly unregistered; CMake explicitly registers GuestPthreadAttr.cpp as guest_pthread_attr, so this is a heuristic false positive.
Hosted Windows build and CTest Passed: guest_pthread_attr passed, and the changed-test run passed 2,539/2,539 tests.
Other hosted product jobs Linux, relinker, ASAN, register-context, and write-watch were skipped.
Precheck helper availability Its isolated snapshot lacks the workspace-only ABI and test-quality helper scripts; I ran the ABI audit separately and inspected the test registration directly.
Author-reported control The author reports the new test fails after restoring Thread.cpp to main; I did not independently run that control.
Unedited check output

Conventions:

All conventions checks passed.

Merge preview tree:

c92e6abbfe28383337b79a5f239cb4c45b7e9a73

ABI audit:

ABI audit: passed_with_warnings

Windows CTest:

           Start 2729: guest_pthread_attr
2283/2539 Test #2729: guest_pthread_attr .......................................................   Passed    0.54 sec
100% tests passed, 0 tests failed out of 2539
Total Test time (real) = 145.53 sec

Negative control

Assertion Change made to code under test Did the new test fail?
An ended adopted host thread is reported as ESRCH The author reports restoring Thread.cpp to main and relinking libkernel.prx on Windows. Author reports it aborted as expected; not independently exercised here.

Findings

Blocking

None found.

Non-blocking

None found in the reviewed change.

Sources

Not verified

I did not build or run tests locally. Hosted Linux and specialized product jobs were skipped; the author did not run Linux/macOS or a console test.

To get this to a good state

No further change is needed for the reviewed behavior. If the head changes, I will rerun the same checks against the new head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5725378974 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14190#pullrequestreview-5725378974). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-31T23:06:44Z. **Review:** APPROVED --- # Reviewed — looks good The Windows-specific handle check now recognizes an adopted host thread as ended, and the exact-head Windows CI run passed the test that covers it. ## What I ran Reviewed head `7122664e7e93e8de67af2e611659937d8124c190` against current `main` `381891927c2b3ff494a9853cb79fde8d5342b92d`; the merge preview is clean. Environment: Debian Linux x86_64, glibc 2.36, Python 3.11.2, GCC/G++ 12.2.0. I did not build locally. ```sh git diff --check 381891927c2b3ff494a9853cb79fde8d5342b92d..refs/review/pr-14190-head python3 tools/check_conventions.py --base 381891927c2b3ff494a9853cb79fde8d5342b92d --head refs/review/pr-14190-head git merge-tree --write-tree 381891927c2b3ff494a9853cb79fde8d5342b92d refs/review/pr-14190-head python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 ``` ## Results | Check | Result | |---|---| | Diff check | Passed (exit 0; no output). | | Conventions and PR template | Passed locally and in hosted CI. | | Current-main merge preview | Clean. | | ABI lexical audit | No changed exports; the audit reports one warning for `ResetOnce`, which is a `static` function and therefore intentionally has no public header declaration. | | Test-quality heuristic | It flags the changed C++ test as possibly unregistered; CMake explicitly registers `GuestPthreadAttr.cpp` as `guest_pthread_attr`, so this is a heuristic false positive. | | Hosted Windows build and CTest | Passed: `guest_pthread_attr` passed, and the changed-test run passed 2,539/2,539 tests. | | Other hosted product jobs | Linux, relinker, ASAN, register-context, and write-watch were skipped. | | Precheck helper availability | Its isolated snapshot lacks the workspace-only ABI and test-quality helper scripts; I ran the ABI audit separately and inspected the test registration directly. | | Author-reported control | The author reports the new test fails after restoring `Thread.cpp` to `main`; I did not independently run that control. | <details><summary>Unedited check output</summary> Conventions: ```text All conventions checks passed. ``` Merge preview tree: ```text c92e6abbfe28383337b79a5f239cb4c45b7e9a73 ``` ABI audit: ```text ABI audit: passed_with_warnings ``` Windows CTest: ```text Start 2729: guest_pthread_attr 2283/2539 Test #2729: guest_pthread_attr ....................................................... Passed 0.54 sec 100% tests passed, 0 tests failed out of 2539 Total Test time (real) = 145.53 sec ``` </details> ## Negative control | Assertion | Change made to code under test | Did the new test fail? | |---|---|---| | An ended adopted host thread is reported as `ESRCH` | The author reports restoring `Thread.cpp` to `main` and relinking `libkernel.prx` on Windows. | Author reports it aborted as expected; not independently exercised here. | ## Findings ### Blocking None found. ### Non-blocking None found in the reviewed change. ## Sources - [The adopted-thread liveness check at this head](https://github.com/boykopovar/AnyPS5/blob/7122664e7e93e8de67af2e611659937d8124c190/core/libs/prx/libkernel/Pthread/src/Thread.cpp#L559-L569) - [The new ended-adopted-thread assertion](https://github.com/boykopovar/AnyPS5/blob/7122664e7e93e8de67af2e611659937d8124c190/core/libs/tests/GuestPthreadAttr.cpp#L321-L325) - [CMake registration of `guest_pthread_attr`](https://github.com/boykopovar/AnyPS5/blob/7122664e7e93e8de67af2e611659937d8124c190/core/libs/prx/libkernel/CMakeLists.txt#L698-L703) - [Hosted Windows build and CTest job](https://github.com/boykopovar/AnyPS5/actions/runs/41249542377/job/124680246216) ## Not verified I did not build or run tests locally. Hosted Linux and specialized product jobs were skipped; the author did not run Linux/macOS or a console test. ## To get this to a good state No further change is needed for the reviewed behavior. If the head changes, I will rerun the same checks against the new head.
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!14190
No description provided.