Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
Plyxen:test/libkernel-released-handle-wait-for-release
Dec 4, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
Plyxen:test/libkernel-released-handle-wait-for-release

Conversation

@Plyxen

@Plyxen Plyxen commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

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.

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 Nov 28, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Pthread release check

Layer / File(s) Summary
Poll for pthread release
core/libs/tests/GuestPthreadReleasedHandle.cpp
WaitUntilReleased now checks pthread_getname_np_nid_postfix for GUEST_ESRCH instead of checking scePthreadGetprio for SCE_KERNEL_ERROR_ESRCH. The deadline and yield logic remain unchanged.

Suggested reviewers: marczx10, aasishdairelsahayagrinspan, privacyonion

Change: Other

Merge Risk: Minimal · up to d165e

This is a test-only change that makes the released-handle test wait until the thread is actually deregistered, which removes the intermittent failures the author reported. It does not change production behavior, and no merge-blocking risk remains.

Security architecture risk: Minimal · up to d165e

The test now waits for the handle to leave the registry before checking that later calls reject it. Production behavior is unchanged.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
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: 5d25cec4-ff7a-4041-9529-ae463df49e71
Commits

Reviewing files that changed from the base of the PR and between 9f02806 and d165e69.

Files selected for processing: 1

  • core/libs/tests/GuestPthreadReleasedHandle.cpp

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@JJenkx

JJenkx commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Linux check of the exact head d165e69efc98; the description has no Linux results. Arch Linux 7.3.5 x86-64, GCC 16.2.1, Release, -DBUILD_TESTING=ON:

  • guest_pthread_released_handle (this PR's GuestPthreadReleasedHandle.cpp): pass, not skipped.

AI-assisted: yes

@ligmaSec ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@oneandonlydean
oneandonlydean merged commit 72c7561 into boykopovar:main Dec 4, 2026
14 of 16 checks passed
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.

6 participants