fix(libkernel): release joined threads beyond the 100 most recent, as libthr does #23953
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/23953"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by PostMalloy on 2026-11-19T22:48:12Z.
What
scePthreadJoinretains the joined thread so that a second join on the same handle answersESRCH. The thread is released only whenscePthreadCreatelater publishes a new thread into the same handle storage. A title that keeps its thread handles in memory it frees or reuses never triggers that release, so every joined thread is retained for the rest of the process. The debt entry already said so ("a guest that joins into a fresh location every time keeps every one"). Each retained thread keeps three things:PthreadPrivate;ReleaseThread;CpuClocks().threads.LockRegisteredGuestThreadandLockLiveGuestThreadscan that map linearly on every join, detach, cancel, getname and priority or affinity call, so each of those calls gets slower as threads accumulate.This is visible in
guest_pthread_mutex_contention. ItsCheckHandoffThenDestroykeeps each handle in a freshCountedobject, so all of its roughly 36,000 joined threads are retained. On Windows, its four identical 3000-round passes took 1.68, 2.52, 3.45 and 4.48 s. The test as a whole took 14 to 17 s, and timed out at its 60 s limit under a parallelctest -j 12.Joined threads are now released after the 100 most recent joins, oldest first, as well as when their storage is reused. 100 is FreeBSD libthr's
MAX_CACHED_THREADS(thr_list.c). Past that many cached finished threads, libthr frees them, and_thr_find_threadthen answersESRCHfor the old pointer, or finds the newer thread allocated at the same address.Depends on #21742. A released handle has to be looked up in the registry by pointer value before anything reads it. On
main,pthread_getname_npreadsthread->_joined, andpthread_getcpuclockidreadscpuClockThread, without that lookup. This was safe for joined threads only because they were never freed. #21742 moves both reads underLockRegisteredGuestThread, so this PR is built on it and includes its commit. I checked every other export that reads a thread handle's object: each already goes throughLockRegisteredGuestThread,LockLiveGuestThread,GuestThreadIsLive, or a validatedscePthread*call first.The debt fragment updates the
scePthreadJoin, scePthreadCreateentry with the bound and its libthr basis. It also replaces two clauses there that said a copied handle, and a join after a detached thread ended, "read freed memory". Both cases are answered through the registry. #21742's fragment keeps thepthread_getname_npentry's "the joined thread's object is retained" wording, so I left that entry to #21742. After this PR, that holds only for the 100 most recent joins.pr_overlap.py --branchreports no PR implementing the same exports. #12130, #19612, #21464 and #21941 conflict with this change only at neighbouring lines ofThread.cpp.Tested
Windows 11 x64 (10.0.26200), Ryzen 5 5600X, WinLibs MinGW-w64 GCC 15.2.0, Release.
guest_pthread_second_joinnow starts by joining 100 threads into distinct handle storage:ESRCH.APS5_Test_SetFinalThreadReleaseHookobserves.pthread_getname_npandpthread_getcpuclockidon the released handle then answerESRCHwithout writing their outputs.ESRCHto a second join.Timing
guest_pthread_mutex_contentionon the same machine:CheckHandoffThenDestroypasses took 1.10, 1.12, 1.10 and 1.09 s, against 1.68 to 4.48 s onmain.These figures come from the test binary, not a title trace, and are offered as evidence of the leak rather than as a speed claim.
The tests below were run on current
mainmerged with #21742 and this commit:pthread|thread|cpu_clock|job_manager|barrier|releasedpass, including #21742'sreleased_handletest.ctest -j 12of this change, before it was rebased onto #21742, failed only on:mainon this machine (#22508, #22950, #23751);agc_driver_rect_list_vertex_layer(3 of 3),guest_thread_cpu_clock(40 of 40 under the same load),patch_input_preservation_linuxandpatch_input_preservation_windows.tools/check_conventions.py --base origin/mainandtools/debt_fold.py --checkpass.Review follow-up,
ef3feb697303b:scePthreadSetschedparamandscePthreadGetschedparamnow store and read the priority and policy under one registry lock.guest_pthread_schedparamnow alternates a thread between FIFO/300 and RR/700 20000 times and requires every read pair to be one of the two.guest_sched_params,guest_pthread_released_handleandguest_pthread_second_joinpass 3 of 3.No PS5 hardware was used.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Summary by CodeRabbit
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-19T22:51:33Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
26a3d9fc01:tools/check_conventions.pypassed.guest_pthread_released_handle_tests,guest_pthread_second_join_testsbuilt cleanly.guest_pthread_released_handle,guest_pthread_second_join):core/libs/prx/libkernel/Pthread/Posix/Thread.cpp,core/libs/prx/libkernel/Pthread/include/Pthread.hpp,core/libs/prx/libkernel/Pthread/src/Thread.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-19T23:03:56Z.
Review: COMMENTED
Reviewed — comments
Changes are required before merge: address the scheduling update race described below.
What I ran
Reviewed head
26a3d9fc0192d2cb9a96ba9735a2b34e9188bccdagainst the prepared diff and source context. No commands or tests were run;repo_read.pywas used to inspect the review bundle and repository files.Results
Findings
Blocking
None.
Non-blocking
core/libs/prx/libkernel/Pthread/Posix/Thread.cpp:265—scePthreadSetschedparamfirst updates the priority throughscePthreadSetprio, which releases the registry lock on return, and only then reacquires it to store the policy. A concurrentsched_getschedulerorsched_getparamcan observe the new priority with the old policy. Consider updating both fields under one registry lock (after validating the live thread and requested values) so the operation is atomic to readers.Not verified
review/manifest.json, including every added line. Also readreview/description.md,review/commits.md,review/manifest.json,review/diff.patch, andreview/README.md. No changed files were excluded; the attemptedreview/files.jsonread was unavailable and was not needed because the directory listing and manifest showed all changed files included.To get this to a good state
Nothing is required. The scheduling consistency point above is optional.
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-19T23:33:08Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
ef3feb6973:tools/check_conventions.pypassed.guest_pthread_released_handle_tests,guest_pthread_schedparam_tests,guest_pthread_second_join_testsbuilt cleanly.guest_pthread_schedparam,guest_pthread_released_handle,guest_pthread_second_join):core/libs/prx/libkernel/Pthread/Posix/Thread.cpp,core/libs/prx/libkernel/Pthread/include/Pthread.hpp,core/libs/prx/libkernel/Pthread/src/Thread.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T04:48:13Z.
Review: APPROVED
Reviewed — looks good
Nothing in the inspected changes blocks merging; the registry locks protect joined-thread lookups and the retention bound releases the oldest cached thread.
What I ran
Reviewed head
ef3feb697303b1331e5091c7b9d0a86779847737. No tests or build commands were run;repo_read.pywas used to inspect the prepared review materials and targeted source ranges.Results
Findings
Blocking
None found in the inspected code.
Non-blocking
None.
Not verified
No tests, build, or PS5 hardware checks were run. All seven changed files listed in
manifest.jsonwere included; the implementation, tests, and debt fragments were inspected through the diff or targeted head source ranges. The full text of every changed file was not read.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-27T18:45:31Z.
Review: COMMENTED
Actionable comments posted: 1
755d0351-e723-47c5-8dd0-62c97a6766bbReviewing files that changed from the base of the PR and between
7da01bf27danddc9555bb4e.core/libs/prx/libkernel/Pthread/Posix/Thread.cppcore/libs/prx/libkernel/Pthread/src/Thread.cppcore/libs/tests/GuestPthreadSchedparam.cppcore/libs/tests/GuestPthreadSecondJoin.cppdocs/dev/debt/changes/fix-libkernel-bound-joined-thread-retention.mdIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-27T19:10:45Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the points below are optional.
What I ran
Reviewed head
dc9555bb4e568bd4279b0211bbb23bf4969e864c. No commands or tests were run in this review; test results in the description are author-reported. The supplied diff is the review source; the repository diff was not fetched again.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
The review folder’s
files.jsonwas not available through the reader. The prepared listing reports all five changed files as included, and all five were read. No tests, builds, operating systems, or PS5 hardware behavior were independently verified. The supplieddiff.patchandmanifest.jsonwere read;description.md,commits.md, andreview/README.mdwere also read.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T14:23:09Z.
Review: COMMENTED
Built the branch's pthread units against the existing Linux runtime.
GuestPthreadSchedparamandGuestPthreadSecondJoinboth passed. The fixtures cover retained handles and older handles that have been evicted from the joined-thread history.AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.