fix(libkernel): run pthread key destructors before releasing thread state #3753
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3753"
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 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().currentis cleared and the native thread releases its reference. A destructor callingscePthreadSelf()therefore adopts a new thread object: the regression reproduces the wrong identity, ID and name on normal return and explicitscePthreadExit().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 reenterfinishThread.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 ona11c8f7f70a0ed4e71504a89de69e8cdae687817.guest_pthread_destructor_lifetime,guest_pthread_key,guest_thread_self,guest_thread_atexit,guest_pthread_cancel, andguest_pthread_start; each takes under 2 s. The new regression takes 0.40 s.guest_pthread_cancel_recoverycould not launch. CTest reportsBAD_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.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 run38155293966, workflow commit7b50b1edf11fe400ee23b53d612412eb735a6bc7; source commits are pinned above.These are synthetic runtime tests; no affected retail title, gameplay, FPS, or NVIDIA pipeline-compilation improvement is claimed.
Checklist
main; no other open PR implements the same functionsImported 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_lifetimetest fails without the change and passes with it, together withguest_pthread_key,guest_pthread_cancel*andguest_thread*. The order (thread dtors, then key destructors) matches FreeBSD libthrthr_exit.c; merging.