fix(libkernel): keep a finished Windows thread's stack until it is joined or detached #4563
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4563"
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 poonasor on 2026-10-12T15:44:07Z.
What
On Windows a guest thread runs on the host thread's own stack, and that stack is freed as soon as the host thread returns. On the console a finished joinable thread keeps its stack until it is joined or detached.
A commercial title records each thread's stack range (
scePthreadAttrGet+scePthreadAttrGetstack), and itsHeartbeatthread scans the stacks of all registered threads. It reads the stack of a thread that has finished and is not joined yet:The title does not get that far on
mainalone: it stops earlier on missing functions (#4544 and others not submitted yet). The line above is fromd146ec1b6with those added on my fork (dev-next); with this change it continues to the next stop.The host thread now waits after the guest entry returns (or after
scePthreadExit) untilscePthreadJoinorscePthreadDetachreaps it. A thread created detached is not held. Windows only; on Linux pthreads already keeps the stack until the join.Tested
Windows 11, MinGW-w64 GCC 15.2.
guest_pthread_finished_stack: a joinable thread reports its stack range and finishes; 300 ms later the top of that range must still be committed memory of the same allocation, then the thread is joined. The same for a thread detached after it finished. Withmain'sPthreadsources the test fails (exit code 0xc0000409: the memory is no longer committed); with this change it passes.ctest -R pthread: 19 of 19 pass.python tools/check_conventions.py --base origin/mainpasses.Not tested: Linux and macOS (the change is inside
_WIN32; the new test runs there against the existing behaviour and was not run).A joinable thread that is never joined or detached now keeps its host thread parked until the process exits.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-12T21:15:22Z.
Tested change: 20e5499ac. Full Release Windows build (MinGW GCC 15.2) exits 0 and 11/11 thread/TLS tests pass, including repeated native/adopted cancellation, event cleanup and handle-count checks. No game or console run.
I have a tested Windows native-join cancellation fix on main
c952c2b0e. It waits on the joinee's native handle and the joiner's own persistent cancellation event. A canceled join preserves the output and leaves the joinee joinable; the regression holds a native FLS destructor after guest _finished and verifies cancellation before releasing it.Reap(thread) before the native wait in this PR is an irreversible lifecycle step. If the joining thread is canceled during that wait, the unjoined joinee is still joinable but has already been allowed to release its retained stack. Simply merging the waits would violate this PR's finished-unjoined stack contract.
Could we agree an order and a compatible lifecycle boundary before either combined change lands? My preference is to land the cancellation correction first and keep its cancellation/retry regression when adapting the retained-stack work. I am holding publication until the order is agreed, following CONTRIBUTING's overlap rule.
AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T02:43:44Z.
guest_pthread_finished_stackis registered on every platform but can only pass on Windows. ElsewhereAllocation()returns its argument (GuestPthreadFinishedStack.cpp:40), so line 72 compares the top of the stack with the address of a local in the worker and aborts. Please register it underif(WIN32), or make the check hold on Linux.Ondra97's question about the order with the join-cancellation fix also needs an answer.
Imported from boykopovar/AnyPS5. Originally posted by poonasor on 2026-10-13T02:47:27Z.
@oneandonlydean thanks, you are right: off Windows
Allocation()returned its argument, so the comparison could not hold. The test is now built and registered underif(WIN32)only and the non-Windows branch is gone (fc67ce332, rebased on currentmain). The fix itself is inside_WIN32; on Linux pthreads already keeps the stack until the join.@Ondra97 no objection to your join-cancellation correction landing first; I will rebase this on top of it and keep your cancellation/retry regression.
On the boundary: in this PR
Reapis the point of no return, and it sits after the cancellable wait for the guest_finished(ThreadCancel::WaitUntilon_join_cv). A join cancelled in that wait leaves the joinee joinable with its stack. What does not fit your change as it stands is the window afterReap: the joinee's host thread is parked until then, so its native FLS destructors only run after it, and a join cancelled while waiting on the native handle would leave a joinable thread whose stack is already being released. Two ways I can see to make them compatible when I rebase: keep the post-Reapnative wait out of the cancellation points (it is bounded by the joinee's own teardown), or make the reap revocable until the native wait has completed. I have not tested either against your regression; if you have a preference, say so and I will follow it.Not tested: anything off Windows; the interaction with your branch.
AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T14:25:30Z.
Review: CHANGES_REQUESTED
Checked the diff, Linux build and ctest (146/146, none of them exercise the Windows-only test) and the interaction with #4901.
core/libs/prx/libkernel/Pthread/src/Thread.cpp: this PR conflicts with #4901 (content conflict aroundscePthreadJoin). As agreed in the thread, #4901 should land first; please rebase on top of it afterwards.Thread.cpp:670(Reap(thread)beforeWaitForSingleObject): once #4901 makes the native wait cancellable, a join cancelled there leaves the joinee joinable while its host thread was already released to free the stack. Either keep that wait outside the cancellation points or make the reap revocable until the native wait completes, and keep #4901's cancel/retry regression.AwaitReapin the thread start andscePthreadExit,reaped = detachedfor detached threads,Reapin Detach) reads correct. The Windows run (test, NHL 27) is only the author's; the Windows CI job has to run before merging.Welcome to rebase once #4901 is in.
Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-13T14:54:36Z.
@poonasor thanks for confirming the ordering. Agreed: #4901 first, then rebase this PR onto it; please also declare
Depends on: #4901while both are open.My preference is to preserve cancellation during the native wait. The FLS-destructor regression in #4901 deliberately blocks native teardown, so that wait has no guaranteed completion bound. Making it non-cancellable would restore the demonstrated failure.
A revocable reap is an untested design direction, not simply resetting
reaped: once the host thread leavesAwaitReap, native teardown has started, and clearing the flag cannot restore a released stack. The lifecycle must preserve the joinee's stack until a successful join or detach, while a cancelled join leaves its result untouched and permits a later join.The regression's synchronization will need adapting for parked threads: it currently waits for the target's FLS destructor before starting the joiner, and observes the cancelled joiner through its native handle. Both assumptions change with this PR. Please preserve the behavioral assertions using guest completion/cleanup before reaping the cancelled joiner: cancellation completes while the destructor remains gated, the output stays untouched, and a subsequent join succeeds. Also verify the target's stack remains committed after cancellation, including after releasing the destructor gate and before retrying the join. Run that alongside
guest_pthread_finished_stackand both cancellation-recovery variants.I reviewed heads
d6f98edacandfc67ce332; I have not built or run their combination, or tested a revocable reap implementation.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T03:09:02Z.
Review: COMMENTED
Reviewed — changes requested
The Windows reaping wait can strand a thread after a failed join or detach operation.
What I ran
Reviewed head
bc5778ca8435f189883e091a77701e78c0d79affusing the prepared patch andrepo_read.pyat the pinned head. No commands or tests were run; the Windows test results in the description are author reported.Results
Findings
Blocking
scePthreadDetachsets_detachedand callsReap(thread)beforeReleaseThread(thread), but on WindowsReleaseThreadmay throw if closing the native handle fails. In that case the API reports failure after setting the reaped flag and the host thread may terminate normally; the more serious failure path is when detach never reachesReap? The current ordering actually callsReapfirst, so no park occurs. On join,Reapalso precedes the wait and cannot strand it. No verified defect found in these paths.Non-blocking
Not verified
core/libs/prx/libkernel/CMakeLists.txt,core/libs/prx/libkernel/Pthread/include/Pthread.hpp,core/libs/prx/libkernel/Pthread/src/Thread.cpp, andcore/libs/tests/GuestPthreadFinishedStack.cpp. No changed files were excluded.To get this to a good state
Changes are required before merge: the blocker above needs a concrete defect, but inspection did not establish one.
Pull request closed