fix(libkernel): cancel Windows joins during native thread cleanup #4901

Closed
Ondra97 wants to merge 1 commit from upstream-pr/4901 into main
Ondra97 commented 2026-10-13 03:04:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-13T03:04:51Z.


What

On Windows, scePthreadJoin can reach native thread cleanup after the guest result is ready (_finished). The final WaitForSingleObject ignored guest cancellation: a joiner canceled during a gated FLS destructor waited for the destructor to finish, returned success and overwrote its output instead of exiting with PTHREAD_CANCELED.

Wait on the joinee's native handle and the joining thread's existing wakeEvent, created lazily and signaled under cancelLock. Keep cancellation checks outside the join/cancel mutexes and before storing the result or releasing the joinee, so a canceled join leaves its output untouched and its target joinable. Disabled cancellation and cleanup handlers already exiting use the ordinary native wait.

Cancel holds an existing ownership reference before publishing the request and releases it after unlocking, including exceptions. This prevents an adopted/detached target from deleting its cancellation mutex while the signaling caller still uses it. FLS cleanup closes the event on an adopted host thread's ordinary return. Existing retention of that adopted thread object and duplicated native handle remains recorded in libkernel debt. The thread-state type is internal so its destructor/template helpers do not become NIDs.

Authors agreed to land this correction first: #4481 / #4533 and #4563. Their lifecycle adaptations must preserve the cancellation/retry regression. Native FLS cleanup can block after _finished; making that wait non-cancellable would reintroduce the demonstrated failure. A revocable reap is an untested proposal, and combined compatibility is not claimed here.

Tested

Windows, MinGW-w64 GCC 15.2, Release, BUILD_TESTING=ON, rebased on main 068ab2aba.

The rebase resolves adjacent edits in libkernel debt by preserving main's updated cpuset text and this PR's cancellation entry. The cancellation code and recovery fixture are unchanged. A clean merge with main 27477e5f4 was verified before publishing.

  • Full build exits 0; 18 related thread/TLS CTests pass (16.64 s; recovery 4.08 s, coarse 4.21 s). This includes the original 11 tests plus stack detection/protection, pthread stack and scheduling, available CPU mask, adopted thread identity and thread sanitizer tests.
  • The native recovery test gates an actual FLS destructor after _finished and uses Windows Wait Chain Traversal to establish that the joiner reached the native wait. The original join failed with canceledBeforeRelease=0 returned=1 outputUntouched=0; the fix exits before releasing the target, preserves the output and permits a later join.
  • Normal, disabled, canceled, cleanup-reentry and adopted-host cancellation cases run a warmup plus 16 rounds with stable process handle counts. A normal-return adopted-host case separately verifies that its previously valid event is closed and cleared; that case failed before the FLS cleanup fix.
  • Patched/unpatched PE export audit finds 1022 exports in each artifact, no GuestThreadState helpers and none of the four legacy helper NIDs. The committed conventions check and git diff --check pass. Independent source review found no remaining issue with the event/reference ownership.

Not tested: Linux/macOS execution, a PS5 console, a game, or this change combined with the pending lifecycle PRs. FreeBSD libthr is the existing cancellation reference, linked in libkernel debt. No title unblock or performance improvement is claimed.

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
  • 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
  • Unblocks: none
  • AI-assisted: yes. — Codex
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4837752239 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4901). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-10-13T03:04:51Z. --- ### What On Windows, `scePthreadJoin` can reach native thread cleanup after the guest result is ready (`_finished`). The final `WaitForSingleObject` ignored guest cancellation: a joiner canceled during a gated FLS destructor waited for the destructor to finish, returned success and overwrote its output instead of exiting with `PTHREAD_CANCELED`. Wait on the joinee's native handle and the joining thread's existing `wakeEvent`, created lazily and signaled under `cancelLock`. Keep cancellation checks outside the join/cancel mutexes and before storing the result or releasing the joinee, so a canceled join leaves its output untouched and its target joinable. Disabled cancellation and cleanup handlers already exiting use the ordinary native wait. `Cancel` holds an existing ownership reference before publishing the request and releases it after unlocking, including exceptions. This prevents an adopted/detached target from deleting its cancellation mutex while the signaling caller still uses it. FLS cleanup closes the event on an adopted host thread's ordinary return. Existing retention of that adopted thread object and duplicated native handle remains recorded in libkernel debt. The thread-state type is internal so its destructor/template helpers do not become NIDs. Authors agreed to land this correction first: [#4481 / #4533](https://github.com/boykopovar/AnyPS5/pull/4481#issuecomment-6133194308) and [#4563](https://github.com/boykopovar/AnyPS5/pull/4563#issuecomment-6137089512). Their lifecycle adaptations must preserve the cancellation/retry regression. Native FLS cleanup can block after `_finished`; making that wait non-cancellable would reintroduce the demonstrated failure. A revocable reap is an untested proposal, and combined compatibility is not claimed here. ### Tested Windows, MinGW-w64 GCC 15.2, Release, BUILD_TESTING=ON, rebased on main 068ab2aba. The rebase resolves adjacent edits in libkernel debt by preserving main's updated cpuset text and this PR's cancellation entry. The cancellation code and recovery fixture are unchanged. A clean merge with main 27477e5f4 was verified before publishing. - Full build exits 0; 18 related thread/TLS CTests pass (16.64 s; recovery 4.08 s, coarse 4.21 s). This includes the original 11 tests plus stack detection/protection, pthread stack and scheduling, available CPU mask, adopted thread identity and thread sanitizer tests. - The native recovery test gates an actual FLS destructor after `_finished` and uses Windows Wait Chain Traversal to establish that the joiner reached the native wait. The original join failed with `canceledBeforeRelease=0 returned=1 outputUntouched=0`; the fix exits before releasing the target, preserves the output and permits a later join. - Normal, disabled, canceled, cleanup-reentry and adopted-host cancellation cases run a warmup plus 16 rounds with stable process handle counts. A normal-return adopted-host case separately verifies that its previously valid event is closed and cleared; that case failed before the FLS cleanup fix. - Patched/unpatched PE export audit finds 1022 exports in each artifact, no GuestThreadState helpers and none of the four legacy helper NIDs. The committed conventions check and `git diff --check` pass. Independent source review found no remaining issue with the event/reference ownership. Not tested: Linux/macOS execution, a PS5 console, a game, or this change combined with the pending lifecycle PRs. FreeBSD libthr is the existing cancellation reference, linked in libkernel debt. No title unblock or performance improvement is claimed. ### Checklist - [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) - [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] Unblocks: none - [x] AI-assisted: yes. — Codex
xXPBSXx commented 2026-10-13 06:42:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T06:42:56Z.


Windows test: head d6f98edac merged onto current main ea2bbefb2 (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, -j2: full build 0 errors; ctest with the filter guest_pthread_cancel_recovery: guest_pthread_cancel_recovery passed, guest_pthread_cancel_recovery_coarse passed (2 test(s), 0 failed); python tools/check_conventions.py --base upstream/main passed. I did not run the new tests against main without the change.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6139897204 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4901#issuecomment-6139897204). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T06:42:56Z. --- Windows test: head `d6f98edac` merged onto current `main` `ea2bbefb2` (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, `-j2`: full build 0 errors; ctest with the filter `guest_pthread_cancel_recovery`: guest_pthread_cancel_recovery passed, guest_pthread_cancel_recovery_coarse passed (2 test(s), 0 failed); `python tools/check_conventions.py --base upstream/main` passed. I did not run the new tests against `main` without the change. AI-assisted (Claude Code).
github-actions[bot] commented 2026-10-13 15:55:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T15:55:23Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#4563 core/libs/prx/libkernel/Pthread/src/Thread.cpp L792-793
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6149599548 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4901#issuecomment-6149599548). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T15:55:23Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #4563 | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` [L792-793](https://github.com/boykopovar/AnyPS5/blob/0df6e295c52e7cd6afa394ed35513bd252045c74/core/libs/prx/libkernel/Pthread/src/Thread.cpp#L792-L793) | | |
oneandonlydean commented 2026-10-14 02:36:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:36:25Z.


Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159628445 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4901#issuecomment-6159628445). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:36:25Z. --- Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

Pull request closed

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!4901
No description provided.