test(libkernel): wait until a released thread leaves the registry #28064

Merged
Plyxen merged 1 commit from upstream-pr/28064 into main 2026-12-04 07:19:21 +00:00
Plyxen commented 2026-11-28 10:24:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-11-28T10:24:37Z.


What

guest_pthread_released_handle aborts in about 5% of runs when the machine is busy. Its WaitUntilReleased loop takes ESRCH from scePthreadGetprio to mean that the handle has been released, and then requires ESRCH from every other call on it. The handle is not released at that point yet:

  • scePthreadGetprio uses LockLiveGuestThread, which answers ESRCH as soon as finishThread has set _finished;
  • the handle stays in the thread registry until its last reference goes in ReleaseThread and ~PthreadPrivate erases it;
  • pthread_getname_np, scePthreadJoin and scePthreadDetach use LockRegisteredGuestThread. In between, pthread_getname_np still returns the name, and scePthreadJoin returns EINVAL because the thread is detached.

The exiting thread drops its reference after finishThread returns, so the window is short. It is long enough when that thread is preempted.

WaitUntilReleased now waits until pthread_getname_np answers ESRCH, which for a detached thread happens only once the handle has left the registry. The checks after it are unchanged.

I did not change the exports. A finished detached thread that is still registered is a state the console has too: FreeBSD's libthr keeps a dead thread until it is collected, and its pthread_join returns EINVAL for a detached thread in that state, as ours does. Its pthread_getname_np answers ESRCH for a dead thread, while ours returns the name until the handle is released. Using a detached thread's handle after it exits is undefined, and I have no console measurement of that window, so this PR leaves the behaviour as it is.

Tested

Tested on main c6331cc61b. The branch is on main 9f02806678, where the test file is the same. The only libkernel change between them, #21969, moves thread objects to the guest heap and doesn't change when a thread leaves the registry. Copies of guest_pthread_released_handle_tests ran at once, in a loop:

main this change
WSL 2 Ubuntu 24.04 (kernel 6.18), GCC 13.3, 8 copies × 50 21 of 400 0 of 400
the same with APS5_NO_PROCMAP_QUERY=1 not run 0 of 400
Windows 11, WinLibs GCC 15.2, 8 copies × 50 4 of 400 0 of 400
Windows 11, WinLibs GCC 15.2, 16 copies × 100 1 of 1,600 0 of 1,600

On main, a copy of the test that prints the line of the failing check showed 20 failures at the pthread_getname_np check and 1 at the scePthreadJoin check. I first saw it fail in a full ctest -j 8 run of #28045, which does not touch threads. With #28045's libraries it fails as often as on main (20 of 400). Run alone it passes, as in the earlier reports on #21742 and #23953.

Test only, so there is no debt change.

python3 tools/pr_overlap.py --names WaitUntilReleased GuestPthreadReleasedHandle reports no open PR changing them. python3 tools/pr_overlap.py --branch test/libkernel-released-handle-wait-for-release reports no overlap with open pull requests.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Tests
    • Updated the wait check to detect when the thread is no longer available. The five-second timeout and polling behavior remain unchanged.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5539161957 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28064). Originally posted by [Plyxen](https://github.com/Plyxen) on 2026-11-28T10:24:37Z. --- ### What `guest_pthread_released_handle` aborts in about 5% of runs when the machine is busy. Its `WaitUntilReleased` loop takes ESRCH from `scePthreadGetprio` to mean that the handle has been released, and then requires ESRCH from every other call on it. The handle is not released at that point yet: - `scePthreadGetprio` uses `LockLiveGuestThread`, which answers ESRCH as soon as `finishThread` has set `_finished`; - the handle stays in the thread registry until its last reference goes in `ReleaseThread` and `~PthreadPrivate` erases it; - `pthread_getname_np`, `scePthreadJoin` and `scePthreadDetach` use `LockRegisteredGuestThread`. In between, `pthread_getname_np` still returns the name, and `scePthreadJoin` returns EINVAL because the thread is detached. The exiting thread drops its reference after `finishThread` returns, so the window is short. It is long enough when that thread is preempted. `WaitUntilReleased` now waits until `pthread_getname_np` answers ESRCH, which for a detached thread happens only once the handle has left the registry. The checks after it are unchanged. I did not change the exports. A finished detached thread that is still registered is a state the console has too: FreeBSD's libthr keeps a dead thread until it is collected, and its `pthread_join` returns EINVAL for a detached thread in that state, as ours does. Its `pthread_getname_np` answers ESRCH for a dead thread, while ours returns the name until the handle is released. Using a detached thread's handle after it exits is undefined, and I have no console measurement of that window, so this PR leaves the behaviour as it is. ### Tested Tested on `main` `c6331cc61b`. The branch is on `main` `9f02806678`, where the test file is the same. The only libkernel change between them, #21969, moves thread objects to the guest heap and doesn't change when a thread leaves the registry. Copies of `guest_pthread_released_handle_tests` ran at once, in a loop: | | `main` | this change | |---|---|---| | WSL 2 Ubuntu 24.04 (kernel 6.18), GCC 13.3, 8 copies × 50 | 21 of 400 | 0 of 400 | | the same with `APS5_NO_PROCMAP_QUERY=1` | not run | 0 of 400 | | Windows 11, WinLibs GCC 15.2, 8 copies × 50 | 4 of 400 | 0 of 400 | | Windows 11, WinLibs GCC 15.2, 16 copies × 100 | 1 of 1,600 | 0 of 1,600 | On `main`, a copy of the test that prints the line of the failing check showed 20 failures at the `pthread_getname_np` check and 1 at the `scePthreadJoin` check. I first saw it fail in a full `ctest -j 8` run of #28045, which does not touch threads. With #28045's libraries it fails as often as on `main` (20 of 400). Run alone it passes, as in the earlier reports on #21742 and #23953. Test only, so there is no debt change. `python3 tools/pr_overlap.py --names WaitUntilReleased GuestPthreadReleasedHandle` reports no open PR changing them. `python3 tools/pr_overlap.py --branch test/libkernel-released-handle-wait-for-release` reports no overlap with open pull requests. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Tests** * Updated the wait check to detect when the thread is no longer available. The five-second timeout and polling behavior remain unchanged. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
PrivacyOnion commented 2026-11-28 10:28:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-28T10:28:19Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against d165e69efc:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_pthread_released_handle_tests built cleanly.
  • CTest (guest_pthread_released_handle):
    1/1 Test #6515: guest_pthread_released_handle ....   Passed    0.03 sec
    100% tests passed, 0 tests failed out of 1
    
  • Code reading:
    • Verified core/libs/tests/GuestPthreadReleasedHandle.cpp:WaitUntilReleased signature and usage of pthread_getname_np_nid_postfix(thread, name) with buffer char name[64].
    • Verified return error code GUEST_ESRCH matching the expected error returned when a thread handle has been fully released and removed from the thread registry.
    • Verified loop termination logic and deadline assertion (Require(steady_clock::now() < deadline)) with std::this_thread::yield() to prevent deadlocks or infinite spinning.
    • Checked AGENTS.md rules compliance (no comments added, no hardcoded title IDs, clean conventions, no extra files).
    • Verified memory safety and buffer bounds: char name[64] is stack-allocated and sufficient for pthread_getname_np name length.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6097242258 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28064#pullrequestreview-6097242258). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-28T10:28:19Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `d165e69efc`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_pthread_released_handle_tests` built cleanly. - **CTest** (`guest_pthread_released_handle`): ``` 1/1 Test #6515: guest_pthread_released_handle .... Passed 0.03 sec 100% tests passed, 0 tests failed out of 1 ``` - **Code reading**: - Verified core/libs/tests/GuestPthreadReleasedHandle.cpp:WaitUntilReleased signature and usage of pthread_getname_np_nid_postfix(thread, name) with buffer char name[64]. - Verified return error code GUEST_ESRCH matching the expected error returned when a thread handle has been fully released and removed from the thread registry. - Verified loop termination logic and deadline assertion (Require(steady_clock::now() < deadline)) with std::this_thread::yield() to prevent deadlocks or infinite spinning. - Checked AGENTS.md rules compliance (no comments added, no hardcoded title IDs, clean conventions, no extra files). - Verified memory safety and buffer bounds: char name[64] is stack-allocated and sufficient for pthread_getname_np name length. Looks Good To Me
MarcZX10 commented 2026-11-28 10:46:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T10:46:51Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this test change; the retry condition now waits for the registry lookup to report ESRCH before checking the released handle.

What I ran

  • Reviewed head d165e69efc98a753c05592074eb48910153a9a9e.
  • Read the prepared description, commit summary, manifest, diff, changed test file, and the pthread_getname_np_nid_postfix implementation.
  • Used repo_read.py search and cat operations to inspect references and relevant implementation lines. No tests or builds were run.

Results

Check Result Note
Diff and changed test passed Change matches stated goal
Target implementation passed Returns ESRCH when unregistered
Tests or build not run No local execution

Findings

Blocking

None.

Non-blocking

None.

Not verified

  • Runtime behavior and test results reported in the PR description were not independently verified.
  • No files were listed as excluded in the available prepared bundle; the changed file was read in full.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6097302290 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28064#pullrequestreview-6097302290). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-28T10:46:51Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this test change; the retry condition now waits for the registry lookup to report ESRCH before checking the released handle. ## What I ran - Reviewed head `d165e69efc98a753c05592074eb48910153a9a9e`. - Read the prepared description, commit summary, manifest, diff, changed test file, and the `pthread_getname_np_nid_postfix` implementation. - Used `repo_read.py` search and cat operations to inspect references and relevant implementation lines. No tests or builds were run. ## Results | Check | Result | Note | |---|---|---| | Diff and changed test | passed | Change matches stated goal | | Target implementation | passed | Returns ESRCH when unregistered | | Tests or build | not run | No local execution | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified - Runtime behavior and test results reported in the PR description were not independently verified. - No files were listed as excluded in the available prepared bundle; the changed file was read in full. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 07:04:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T07:04:08Z.

Review: COMMENTED


Built and ran the released-pthread-handle fixture on Linux. It passed, including the wait for the released thread to leave the registry before asserting on the handle.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6165105162 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28064#pullrequestreview-6165105162). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T07:04:08Z. **Review:** COMMENTED --- Built and ran the released-pthread-handle fixture on Linux. It passed, including the wait for the released thread to leave the registry before asserting on the handle. AI-assisted: yes.
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!28064
No description provided.