fix(libkernel): run pthread key destructors before releasing thread state #3753

Merged
jweidenh merged 2 commits from upstream-pr/3753 into main 2026-10-11 21:58:34 +00:00
jweidenh commented 2026-10-11 16:28:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jweidenh on 2026-10-11T16:28:05Z.


What

Run guest pthread key destructors while the exiting thread still owns its identity and metadata. On current main, key destructors are deferred to host TLS/FLS teardown, after CurrentGuestState().current is cleared and the native thread releases its reference. A destructor calling scePthreadSelf() therefore adopts a new thread object: the regression reproduces the wrong identity, ID and name on normal return and explicit scePthreadExit().

Drain the existing destructor loop from finishThread, after the guest thread destructor callback and before publishing completion. Retain the four-pass limit and host-thread fallback, and prevent a second drain at host TLS/FLS destruction. Mark normal-return teardown as exiting so a pending cancellation encountered by a destructor's wait cannot reenter finishThread.

The regression checks identity, ID, name, waiting from a destructor, null-before-callback semantics, repeated key registration through exactly four passes, normal return with pending cancellation, explicit exit, and ordinary host-thread cleanup. This is independent of #3623's key-numbering change and #3711's Windows creation handshake.

Tested

Pinned baseline with only the new regression: 6ae2a784afc05398a9450450bf60d40604b2ced7. Candidate: 503043ca003759c50e904b0a756b5352b52aa499, based on a11c8f7f70a0ed4e71504a89de69e8cdae687817.

  • Native Windows 11 Pro 10.0.26300, Ryzen 7 5800X, RTX 5090, driver 617.42, WinLibs GCC 15.2.0 UCRT/POSIX/SEH, Release: the baseline fails the expected identity/ID/name assertions on both normal return and explicit exit (0.59 s). The candidate passes guest_pthread_destructor_lifetime, guest_pthread_key, guest_thread_self, guest_thread_atexit, guest_pthread_cancel, and guest_pthread_start; each takes under 2 s. The new regression takes 0.40 s.
  • Windows limitation: both modes of guest_pthread_cancel_recovery could not launch. CTest reports BAD_COMMAND; direct loader diagnostics report WinError 4551, “An Application Control policy has blocked this file.” An unchanged retry stayed blocked. These are not counted as passes; no security policy was changed.
  • Linux x86-64 local self-hosted runner, Ubuntu GCC 13.3.0, Release, targeted executables plus all runtime libs: baseline reproduces the expected assertion (0.013 s); candidate passes all eight focused tests without skips or fatal output, each below 0.35 s. This includes both cancellation-recovery modes blocked on Windows. The new regression passes in 0.015 s. Private validation run 38155293966, workflow commit 7b50b1edf11fe400ee23b53d612412eb735a6bc7; source commits are pinned above.
  • Conventions, whitespace checks, and independent cleanup-order review pass.

These are synthetic runtime tests; no affected retail title, gameplay, FPS, or NVIDIA pipeline-compilation 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; no new stubs
  • No notes, investigation files or images added to the repository
  • No new third-party code
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4820551048 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3753). Originally posted by [jweidenh](https://github.com/jweidenh) on 2026-10-11T16:28:05Z. --- ### What Run guest pthread key destructors while the exiting thread still owns its identity and metadata. On current main, key destructors are deferred to host TLS/FLS teardown, after `CurrentGuestState().current` is cleared and the native thread releases its reference. A destructor calling `scePthreadSelf()` therefore adopts a new thread object: the regression reproduces the wrong identity, ID and name on normal return and explicit `scePthreadExit()`. Drain the existing destructor loop from `finishThread`, after the guest thread destructor callback and before publishing completion. Retain the four-pass limit and host-thread fallback, and prevent a second drain at host TLS/FLS destruction. Mark normal-return teardown as exiting so a pending cancellation encountered by a destructor's wait cannot reenter `finishThread`. The regression checks identity, ID, name, waiting from a destructor, null-before-callback semantics, repeated key registration through exactly four passes, normal return with pending cancellation, explicit exit, and ordinary host-thread cleanup. This is independent of #3623's key-numbering change and #3711's Windows creation handshake. ### Tested Pinned baseline with only the new regression: `6ae2a784afc05398a9450450bf60d40604b2ced7`. Candidate: `503043ca003759c50e904b0a756b5352b52aa499`, based on `a11c8f7f70a0ed4e71504a89de69e8cdae687817`. - Native Windows 11 Pro 10.0.26300, Ryzen 7 5800X, RTX 5090, driver 617.42, WinLibs GCC 15.2.0 UCRT/POSIX/SEH, Release: the baseline fails the expected identity/ID/name assertions on both normal return and explicit exit (0.59 s). The candidate passes `guest_pthread_destructor_lifetime`, `guest_pthread_key`, `guest_thread_self`, `guest_thread_atexit`, `guest_pthread_cancel`, and `guest_pthread_start`; each takes under 2 s. The new regression takes 0.40 s. - Windows limitation: both modes of `guest_pthread_cancel_recovery` could not launch. CTest reports `BAD_COMMAND`; direct loader diagnostics report WinError 4551, “An Application Control policy has blocked this file.” An unchanged retry stayed blocked. These are not counted as passes; no security policy was changed. - Linux x86-64 local self-hosted runner, Ubuntu GCC 13.3.0, Release, targeted executables plus all runtime `libs`: baseline reproduces the expected assertion (0.013 s); candidate passes all eight focused tests without skips or fatal output, each below 0.35 s. This includes both cancellation-recovery modes blocked on Windows. The new regression passes in 0.015 s. Private validation run `38155293966`, workflow commit `7b50b1edf11fe400ee23b53d612412eb735a6bc7`; source commits are pinned above. - Conventions, whitespace checks, and independent cleanup-order review pass. These are synthetic runtime tests; no affected retail title, gameplay, FPS, or NVIDIA pipeline-compilation 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 - [x] Unimplemented paths throw; no new stubs - [x] No notes, investigation files or images added to the repository - [x] No new third-party code - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
adriadam10 commented 2026-10-11 21:58:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T21:58:29Z.


Checked on Linux (PR merged onto current main): the new guest_pthread_destructor_lifetime test fails without the change and passes with it, together with guest_pthread_key, guest_pthread_cancel* and guest_thread*. The order (thread dtors, then key destructors) matches FreeBSD libthr thr_exit.c; merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6114260932 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3753#issuecomment-6114260932). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T21:58:29Z. --- Checked on Linux (PR merged onto current main): the new `guest_pthread_destructor_lifetime` test fails without the change and passes with it, together with `guest_pthread_key`, `guest_pthread_cancel*` and `guest_thread*`. The order (thread dtors, then key destructors) matches FreeBSD libthr `thr_exit.c`; merging.
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!3753
No description provided.