fix(libs): keep thread_local objects with destructors out of module teardown on Windows #4029
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4029"
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 AlxcBTW on 2026-10-11T21:35:55Z.
What
Fixes #2980 and the rare
0xc0000374exit of #3171. On Windows (MinGW, emulated TLS) athread_localwith a destructor in a module DLL is destroyed after its storage is freed: when a thread ends, winpthread runs libgcc's emutls key destructor (emutls_destroyfrees the thread'sthread_localobjects), and only then does the loader'sDLL_THREAD_DETACHrun the module'stls_atexitlist, so the destructors run on freed memory. In libSceAgcDriver this corrupts the heap whenDriver::stopjoins the queue worker.fix(agc): the driver's dispatch-ahead map, pending waits, draw-snapshot compare vectors and image decode memo move toHostThreadLocal. The memo keeps itsunique_ptr, now owned byHostThreadLocal, so Linux builds the samethread_localasmain.fix(libs): the same forPreciseSleepUs's timer, libkernel's directory path, socket waker and UUID random device, libSceRandom's random device and libScePosixForWebKit's random device and host lookup. Every otherthread_localincore/is trivially destructible or Linux-only.test(libs):prx_thread_local_destructorschecks every built module for calls to__cxa_thread_atexit(outside libc's registration of guest destructors) and fails with the registering function.Confirmed on #3171:
mainfault in~vector<PendingWait>viatls_callback/run_dtor_list; 6 of 6 runs on the branch end without a fault, and 20 runs without page heap give no0xc0000374.agc_driver_dword_pattern_fill,agc_driver_draw_cache_reuseandagc_driver_prepared_shaders --deferredstop withc0000374every time under gdb onmainand are clean on the branch; the 8-way--deferredrun goes from 2 of 64 failures to 0 of 64.Tested
Windows 11 (26200), WinLibs GCC 15.2.0, Release, rebased onto
maina11c8f7f7.main0f863c102):agc_driver_draw_cache_reuseunder cdb (debug heap): before 127 of 127 runs stopped with heap corruption at the worker's exit (also withAPS5_NO_PIPELINE_LTO=1andAPS5_NO_GPL=1), after 38 of 38 clean. Normal runs with a cold NVIDIA shader cache: before 2 of 143 exited0xc0000374, after 98 of 98 passed.prx_thread_local_destructorsfails withmain's libSceRandom (it reportssceRandomGetRandomNumber) and passes on the branch for all 122 modules.ctest -E "^agc_driver|^video_out|^guest_video"): 528/531 pass, includingprx_thread_local_destructorsandhost_thread_local. The three failures,guest_kernel_sys,guest_filesystem(access denied on a created file) andwindows_exceptions, fail the same way (0xc0000409) on unrelated trees there;windows_exceptionsalso fails onmain(CI).agc_driver_graphics,agc_driver_draw_cache_reuseandagc_driver_draw_cache_reuse_off: 3/3 pass.tools/check_conventions.py --base origin/mainpasses.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 Amoenus on 2026-10-11T22:16:18Z.
Independent Windows validation of the three production commits
3408e4856,1385c40a3,28e73c2b3from head28e73c2b3ca46ba9a25bae97e6a1490aed974477, applied with author credit to a diagnostic integration based on maina672688e(local head274d3f9b2). One adjacent include conflict inShaderResources.cppwas resolved by retaining both includes. The PR's CMake test registration was not imported; its originalThreadLocalDestructors.cmakewas run externally.Environment: Windows 11 / RTX 5080, MinGW GCC 15.2 UCRT POSIX SEH r7, Release,
BUILD_TESTING=ON; native Vulkan required, shader cache off.Before: the exact same original production module hashes, with a separate test-fixture correction described on #2261, produced
0xC0000374in prepared-shader debug mode in a parallel repeated run and deferred-undecodable mode in a sequential repeated run. Earlier pre-fixture runs also failed in deferred mode. The preserved original patched AGC module fails this PR's scanner, reporting destructor registrations inresolvedAhead,pendingWaits, draw-snapshot scratch and image decoding. This is a structural fault control, not a debugger trace of our crashes.After:
libs, registration and convention checks pass. Original scanner:122 modules register no thread_local destructorson the built unpatched modules.agc_driver_draw_cache_reuse, andagc_driver_dword_pattern_fill: 100/100 runs pass,--repeat until-fail:20 --parallel 4 --timeout 30, no skips.The integration includes experimental multisample support and the exact #4021 Windows select correction. These results do not validate the unchanged whole PR head or Linux execution, and do not independently establish the faulting teardown stack. Four PPSA99902 startup controls (before / after twice / restored before) still stop at the intentional native depth fast-clear guard, without timeouts; no additional title startup or frames claimed. Original dump/module, converted guest libc and exact 123-file runtime baseline restored and hash verified. No push or new PR.
Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-11T22:17:16Z.
@Amoenus thank you, that's a thorough campaign. The before/after on the three prepared-shader modes (failing → 100/100) and the scanner catching
resolvedAhead,pendingWaits, the draw-snapshot scratch and image decoding on the unpatched module are exactly the evidence this PR needed. Noted on scope too: Windows only, three production commits on your integration, and no claim about the teardown stack itself.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T23:20:32Z.
Conflicts with main in
ShaderResources.cppandUdp.cpp; please rebase.Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T00:22:49Z.
@oneandonlydean done: I merged current
main(bab7b3285) into the branch asb6a860f92rather than force-pushing. The conflicts were include lists only:ShaderResources.cpp: kept bothHostThreadLocal.hppand main'sMultisampleTarget.hpp;Udp.cpp: kept bothHostThreadLocal.hppand main'sSocketError.hpp.The
imageMemoandThreadWakerchanges are unchanged. The only newthread_localon main is a plainintintests/shared_memory/Support.cpp, which has no destructor.Windows build (WinLibs GCC 15.2, Release, with #4021's
Socket.cppguard applied locally, sincemaindoesn't compile on MinGW without it):prx_thread_local_destructorspasses;ctest576/579. The 3 failures fail identically onmain+ #4021 without this PR:log_macros,guest_kernel_stack_protect, andagc_write_tracking, which #4131 fixes.If you'd rather have it rebased to a linear history, say so and I'll do that instead.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by winstrex on 2026-10-12T01:11:08Z.
Hi @AlxcBTW, a heads-up about an overlap: #4200 takes guest entropy from the host OS (
getentropy,rand_son Windows) instead ofstd::random_device, so it removes thethread_local std::random_deviceinarc4random_buf,sceRandomGetRandomNumberandsceKernelUuidCreate. If it merges first, those three hunks here are no longer needed; if this PR merges first, I'll resolve the conflict on my side. The rest of your change doesn't overlap with mine.Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T01:12:20Z.
@winstrex thanks for the heads-up, agreed. If #4200 merges first, I'll drop the three
random_devicehunks here (arc4random_buf,sceRandomGetRandomNumber,sceKernelUuidCreate). Taking entropy from the host OS removes thosethread_locals altogether, which is the better fix for them. This PR's scanner (ThreadLocalDestructors.cmake) passes either way, since it only fails on a remainingthread_localwith a destructor.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T03:20:29Z.
Merged current
maininto this branch (28fea1ce) so it can build again: the Windows heap corruption at worker exit this fixes (agc_driver_flip_compute, exit code 0xc0000374) is turning unrelated full builds red, three so far today. The only conflicts were the threerandom_devicehunks (arc4random_buf,sceRandomGetRandomNumber,sceKernelUuidCreate); I tookmain's side there, as agreed above with #4200 in. Full build started on this head.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T04:37:51Z.
Merged
mainin again (15f345ce): #3661 landed and changedDriver::resolvedAheadinDispatchAhead.cppto aThreadOwnedpointer, which has no destructor to run at teardown, so I tookmain's version of that function there. The rest is unchanged. New full build started.Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-12T05:45:12Z.
Windows integration cross-test: your production changes applied cleanly to our older retail 007 stack (
3408e4856+28e73c2b3+1385c40a3, original author retained).The upstream binary inspection also caught two extra destructor-bearing containers in our local performance patches, outside this PR: the stale vector in ShaderResources::MovedReadOnlyBuffers and ColumnMemos in ResourceMaterializer. Local adaptation d7a0d0183 moves them to the same HostThreadLocal pattern. Our intermediate 903d5034b graphics binary fails the inspector; rebuilt d29fd5fe4 passes it. Inspection was scoped to libSceAgcDriver, not all runtime modules.
14 selected native Vulkan tests passed on RTX 5070 Ti in 9.04 s with no skips, including draw-cache reuse GPL on/off, recorder alias lifetime/memory, upload lifetime/failure, idle write-back, graphics, BDA and clip/cull fixtures. A guarded 007 run is underway; no claim of reproducing or resolving all title teardown faults. Public integration: https://github.com/Scramblrrrr/AnyPS5/tree/codex/first-light-thread-lifetime-cross-test . AI-assisted: Codex.