Skip to content

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

Open
Thimo188 wants to merge 1 commit into
mainfrom
upstream-pr/28064
Open

Thimo188 wants to merge 1 commit into
mainfrom
upstream-pr/28064

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#28064, originally opened by Plyxen.

Source commit: d165e69efc98a753c05592074eb48910153a9a9e. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


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, boykopovar#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 boykopovar#28045, which does not touch threads. With boykopovar#28045's libraries it fails as often as on main (20 of 400). Run alone it passes, as in the earlier reports on boykopovar#21742 and boykopovar#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
    • Improved synchronization checks for guest thread handle release, helping ensure tests wait until the handle is fully released.

guest_pthread_released_handle waited until scePthreadGetprio answered
ESRCH and then expected every call on the handle to answer ESRCH.
scePthreadGetprio answers ESRCH as soon as the thread has finished,
but the handle stays registered until its last reference is released.
In that window pthread_getname_np still returns the name and
scePthreadJoin returns EINVAL for the detached thread, so the test
aborted when the exiting thread was slow to drop its reference.

Wait until pthread_getname_np answers ESRCH, which it does only once
the handle has left the registry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Dec 2, 2026

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Guest pthread release detection

Layer / File(s) Summary
Update release polling
core/libs/tests/GuestPthreadReleasedHandle.cpp
WaitUntilReleased now polls pthread_getname_np_nid_postfix for GUEST_ESRCH instead of checking scePthreadGetprio for SCE_KERNEL_ERROR_ESRCH. The five-second deadline and yielding remain unchanged.

Change: Other

Merge Risk: Minimal · up to d165e

This is a test-only change that makes a flaky thread-release test wait for the thread to leave the registry. It does not affect production behavior, and no merge-blocking risk remains.

Security architecture risk: Minimal · up to d165e

The regression test now waits for the detached thread’s handle to leave the registry before checking that calls reject it. The change does not alter libkernel’s public API or thread lifecycle.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Usage-based review receipt

  • Mode: Continue automatically
  • Reviewed files: 1
  • Waived: $0.25 (charged $0.00)
  • View usage details

Note

This review exceeded your plan’s limits and used usage-based reviews—free during trial, billed after paid activation unless disabled. Manage usage-based reviews.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8d390e6f-5c78-452c-8c19-633dd46af026
Commits

Reviewing files that changed from the base of the PR and between 4131f51 and d165e69.

Files selected for processing: 1

  • core/libs/tests/GuestPthreadReleasedHandle.cpp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants