fix(libkernel): allow repeated thread dtor callback registration #7639

Closed
MarcZX10 wants to merge 2 commits from upstream-pr/7639 into main
MarcZX10 commented 2026-10-18 18:56:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T18:56:40Z.


What

Make registration of the same thread destructor callback idempotent while preserving the existing error for a different callback. The regression test covers both behaviors through the shared registration helper used by ThreadLifecycle::SetThreadDtors.

Issue #7636 reports a catalog-wide Linux startup abort after #6112 and identifies ThreadLifecycle::SetThreadDtors as the throw site. The report suspects duplicate libc startup registration, but does not establish that both calls pass the same callback; this change addresses that case only. It has not been validated against the full title startup/catalog, and Windows behavior is unverified.

Tested

  • Linux x86_64: built thread_dtor_registration_tests with Ninja.
  • Linux x86_64: focused CTest thread_dtor_registration passed (1/1).
  • python3 tools/check_conventions.py --base upstream/main passed locally before opening; git diff --check passed.

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
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none (the reported cause is not yet confirmed)
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4911880255 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7639). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-18T18:56:40Z. --- ### What Make registration of the same thread destructor callback idempotent while preserving the existing error for a different callback. The regression test covers both behaviors through the shared registration helper used by `ThreadLifecycle::SetThreadDtors`. Issue #7636 reports a catalog-wide Linux startup abort after #6112 and identifies `ThreadLifecycle::SetThreadDtors` as the throw site. The report suspects duplicate libc startup registration, but does not establish that both calls pass the same callback; this change addresses that case only. It has not been validated against the full title startup/catalog, and Windows behavior is unverified. ### Tested - Linux x86_64: built `thread_dtor_registration_tests` with Ninja. - Linux x86_64: focused CTest `thread_dtor_registration` passed (1/1). - `python3 tools/check_conventions.py --base upstream/main` passed locally before opening; `git diff --check` passed. ### 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 (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical debt file - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none (the reported cause is not yet confirmed) - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-18 18:58:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T18:58:10Z.


Source trace after opening: the only in-tree call to sceKernelSetThreadDtors is RegisterThreadExitHook in libSceLibcInternal/Export.cpp, and it always passes RunThreadDestructors_nid_no_patch. Its function-local static should suppress repeat calls within one loaded module. That makes identical callback registration a plausible failure path, but does not establish why the setter is reached a second time (or exclude guest code calling the export with another callback). The production startup repro is still needed to confirm this fixes #7636.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6253836362 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7639#issuecomment-6253836362). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-18T18:58:10Z. --- Source trace after opening: the only in-tree call to `sceKernelSetThreadDtors` is `RegisterThreadExitHook` in `libSceLibcInternal/Export.cpp`, and it always passes `RunThreadDestructors_nid_no_patch`. Its function-local static should suppress repeat calls within one loaded module. That makes identical callback registration a plausible failure path, but does not establish why the setter is reached a second time (or exclude guest code calling the export with another callback). The production startup repro is still needed to confirm this fixes #7636.
github-actions[bot] commented 2026-10-18 19:06:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T19:06:24Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 3986 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 5021 core/libs/prx/libSceLibcInternal/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 6044 core/libs/prx/libSceLibcInternal/CMakeLists.txt
PR 6879 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 7149 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 7201 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 7332 core/libs/prx/libSceLibcInternal/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6253921027 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7639#issuecomment-6253921027). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-18T19:06:24Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 3986](https://www.github.com/boykopovar/AnyPS5/pull/3986) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libSceLibcInternal/CMakeLists.txt`<br>`core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/prx/libSceLibcInternal/CMakeLists.txt` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 7149](https://www.github.com/boykopovar/AnyPS5/pull/7149) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `core/libs/prx/libSceLibcInternal/CMakeLists.txt`<br>`core/libs/prx/libkernel/Pthread/src/Thread.cpp` |
MarcZX10 commented 2026-10-18 19:09:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T19:09:26Z.


I manually reviewed the flagged overlap with #3986. Its Thread.cpp changes add per-thread signal-mask initialization in ThreadArgs, RunThread, and scePthreadCreate; this PR changes only ThreadLifecycle::SetThreadDtors and its registration helper. They do not change the same behavior or depend on each other. #3986 is currently conflicting with main, which is why the overlap bot could not compare those files. The latest conventions run for this PR passes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6253952351 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7639#issuecomment-6253952351). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-18T19:09:26Z. --- I manually reviewed the flagged overlap with #3986. Its `Thread.cpp` changes add per-thread signal-mask initialization in `ThreadArgs`, `RunThread`, and `scePthreadCreate`; this PR changes only `ThreadLifecycle::SetThreadDtors` and its registration helper. They do not change the same behavior or depend on each other. #3986 is currently conflicting with `main`, which is why the overlap bot could not compare those files. The latest conventions run for this PR passes.
oneandonlydean commented 2026-10-18 19:25:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-18T19:25:24Z.


Thanks for jumping on this. We traced #7636 to #6112: a recent libc.prx already runs its own preinit array from DT_INIT, and #6112 runs it again first, so libc's whole early start-up runs twice, not only the destructor registration. The revert is up in #7646 and fixes it at the source, so this one shouldn't be needed once that lands.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6254130840 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7639#issuecomment-6254130840). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-18T19:25:24Z. --- Thanks for jumping on this. We traced #7636 to #6112: a recent `libc.prx` already runs its own preinit array from `DT_INIT`, and #6112 runs it again first, so libc's whole early start-up runs twice, not only the destructor registration. The revert is up in #7646 and fixes it at the source, so this one shouldn't be needed once that lands.
MarcZX10 commented 2026-10-18 19:29:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T19:29:31Z.


Given the #7636 update that attributes the startup regression to #6112 executing libc preinit twice, this PR's same-callback registration test does not yet establish that the proposed idempotence change fixes the reproduced failure. The focused test covers the helper in isolation, but not the startup path. Could you keep this as a fallback until the #7646 revert is validated against Wind Waker, or add a production-path regression if the call trace shows this change is still needed? Also, this head currently has a conventions failure; its successful convention run is for an earlier run/head according to the status checks.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6254172053 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7639#issuecomment-6254172053). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-18T19:29:31Z. --- Given the #7636 update that attributes the startup regression to #6112 executing libc preinit twice, this PR's same-callback registration test does not yet establish that the proposed idempotence change fixes the reproduced failure. The focused test covers the helper in isolation, but not the startup path. Could you keep this as a fallback until the #7646 revert is validated against Wind Waker, or add a production-path regression if the call trace shows this change is still needed? Also, this head currently has a conventions failure; its successful convention run is for an earlier run/head according to the status checks.
MarcZX10 commented 2026-10-18 19:48:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T19:48:51Z.


The follow-up trace in #7636 now confirms the title module’s own DT_INIT calls the startup pointer already invoked through #6112’s module-preinit wrapper. A second repeated registration also reaches SetThreadAtexitCount, so this PR’s destructor-setter idempotence would not fix the full duplicated startup path. I agree this should remain a fallback; #7646 addresses the cause. My issue comment #6254359577 records the trace-to-fix connection. I will wait for #7646 validation before proposing any change here.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6254363538 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7639#issuecomment-6254363538). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-18T19:48:51Z. --- The follow-up trace in #7636 now confirms the title module’s own `DT_INIT` calls the startup pointer already invoked through #6112’s module-preinit wrapper. A second repeated registration also reaches `SetThreadAtexitCount`, so this PR’s destructor-setter idempotence would not fix the full duplicated startup path. I agree this should remain a fallback; #7646 addresses the cause. My issue comment #6254359577 records the trace-to-fix connection. I will wait for #7646 validation before proposing any change here.
ps54all commented 2026-10-18 20:01:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ps54all on 2026-10-18T20:01:08Z.


On Windows (MinGW-w64 GCC 15.2, WinLibs), thread_dtor_registration_tests fails to link: ThreadDtorRegistration.cpp:(.data+0x8): undefined reference to '__gxx_personality_v0'. The library code itself builds and works: with this PR, MGS4 (PPSA18194) gets past the destructor registration and then stops on SetThreadAtexitCount (details in #7636).

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6254484049 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7639#issuecomment-6254484049). Originally posted by [ps54all](https://github.com/ps54all) on 2026-10-18T20:01:08Z. --- On Windows (MinGW-w64 GCC 15.2, WinLibs), `thread_dtor_registration_tests` fails to link: `ThreadDtorRegistration.cpp:(.data+0x8): undefined reference to '__gxx_personality_v0'`. The library code itself builds and works: with this PR, MGS4 (PPSA18194) gets past the destructor registration and then stops on `SetThreadAtexitCount` (details in #7636). AI-assisted: yes
MarcZX10 commented 2026-10-18 20:06:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T20:06:42Z.

Review: COMMENTED


The new Windows data point in #7636 reports that thread_dtor_registration_tests does not link under MinGW (undefined reference to __gxx_personality_v0). It also confirms that, after this PR makes the destructor callback registration idempotent, startup proceeds only to the next duplicate registration (SetThreadAtexitCount). That supports keeping this as a fallback rather than treating it as a fix for the reproduced startup regression; #7646 addresses the duplicated preinit execution. I have not changed this PR or altered its status.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5564725700 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7639#pullrequestreview-5564725700). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-18T20:06:42Z. **Review:** COMMENTED --- The new Windows data point in #7636 reports that `thread_dtor_registration_tests` does not link under MinGW (`undefined reference to __gxx_personality_v0`). It also confirms that, after this PR makes the destructor callback registration idempotent, startup proceeds only to the next duplicate registration (`SetThreadAtexitCount`). That supports keeping this as a fallback rather than treating it as a fix for the reproduced startup regression; #7646 addresses the duplicated preinit execution. I have not changed this PR or altered its status.
MarcZX10 commented 2026-10-18 21:45:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T21:45:21Z.


Thanks for the Windows report. I pushed ed857e237 to link the test with the same libSceLibcInternal/libc runtime setup used by the neighboring guest tests, and added the Windows unwind configuration and runtime PATH entry. The focused test compiles and passes when built directly on Linux. I could not run the full CMake target here because this worktree has uninitialized SDL2 submodule contents; Windows CI will be the confirmation for the reported MinGW link failure.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6255527870 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7639#issuecomment-6255527870). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-18T21:45:21Z. --- Thanks for the Windows report. I pushed `ed857e237` to link the test with the same `libSceLibcInternal`/`libc` runtime setup used by the neighboring guest tests, and added the Windows unwind configuration and runtime PATH entry. The focused test compiles and passes when built directly on Linux. I could not run the full CMake target here because this worktree has uninitialized SDL2 submodule contents; Windows CI will be the confirmation for the reported MinGW link failure.
adriadam10 commented 2026-10-19 16:44:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T16:44:50Z.


Closing: the root cause of #7636 (libc's preinit array running twice after #6112) was fixed at the source by the revert in #7646, merged 2026-10-18, with Linux and Windows title evidence.

  • This change only makes re-registering the same callback silent. With the cause gone, the "already registered" error is the right guard and would flag any future double start-up again.
  • As you noted, startup would still hit the next duplicate registration (SetThreadAtexitCount), so it does not fix the reproduced path, and the test exercises the helper in isolation, not the startup path.

Feel free to reopen with a production-path reproduction if a case still needs it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6272317364 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7639#issuecomment-6272317364). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-19T16:44:50Z. --- Closing: the root cause of #7636 (libc's preinit array running twice after #6112) was fixed at the source by the revert in #7646, merged 2026-10-18, with Linux and Windows title evidence. - This change only makes re-registering the same callback silent. With the cause gone, the "already registered" error is the right guard and would flag any future double start-up again. - As you noted, startup would still hit the next duplicate registration (`SetThreadAtexitCount`), so it does not fix the reproduced path, and the test exercises the helper in isolation, not the startup path. Feel free to reopen with a production-path reproduction if a case still needs it.

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