fix(libkernel): initialize close observer registry on first use #13668

Merged
notHaze merged 2 commits from upstream-pr/13668 into main 2026-10-31 11:51:50 +00:00
notHaze commented 2026-10-30 22:44:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by notHaze on 2026-10-30T22:44:39Z.


What

Fix a DLL initialization failure (0xC0000142) with MSYS2 UCRT64 GCC 16.2.0.

Ksem.cpp registers a descriptor close observer during global initialization, potentially before g_closeObserverMutex in Equeue.cpp has been constructed. Locking the uninitialized mutex throws std::system_error (EINVAL) during libkernel.prx initialization.

Move the mutex and observer list into a function-local static registry, ensuring they are initialized on first use without depending on global constructor order.

Following review, the registry now uses process-lifetime storage to prevent exit callbacks from accessing an already-destroyed mutex or observer list.

Related: #9284 (introduced the global registration).

Tested

Windows, Release, BUILD_TESTING=ON.

Initial fix — full CTest suites on main 9605300f29, with and without the fix:

Result MSYS2 before MSYS2 after WinLibs before and after
Total tests 3,139 3,139 3,139
Passed 1,382 3,121 3,122
Failed 1,753 8 7
Skipped 4 10 10
0xC0000142 failures 1,739 0 0

All 1,739 DLL initialization failures were eliminated. No regressions were observed with WinLibs GCC 15.2.0.

Review follow-up — process-lifetime registry (a6fd9994f7):

Full rebuild and CTest suite on both toolchains.

Result MSYS2 GCC 16.2.0 WinLibs GCC 15.2.0
Total tests 3,159 3,159
Passed 3,143 3,142
Failed 6 7
Skipped 10 10

The six MSYS2 failures also occur on WinLibs. The additional WinLibs failure, agc_driver, passed when rerun individually, suggesting a possible intermittent or parallel-execution issue.

No DLL initialization failures were reported. No regressions attributable to the registry lifetime change were identified.

Conventions checks passed.

PR overlap: The checker could not automatically compare five PRs that already conflict with main. Manual review found that #7509 and #10645 modify unrelated sections of Equeue.cpp, while #9293, #9297 and #9302 carry the original close-observer code from the already-merged #9284. None implements this initialization fix.

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 (n/a)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5091999825 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13668). Originally posted by [notHaze](https://github.com/notHaze) on 2026-10-30T22:44:39Z. --- ### What Fix a DLL initialization failure (`0xC0000142`) with MSYS2 UCRT64 GCC 16.2.0. `Ksem.cpp` registers a descriptor close observer during global initialization, potentially before `g_closeObserverMutex` in `Equeue.cpp` has been constructed. Locking the uninitialized mutex throws `std::system_error` (`EINVAL`) during `libkernel.prx` initialization. Move the mutex and observer list into a function-local static registry, ensuring they are initialized on first use without depending on global constructor order. Following review, the registry now uses process-lifetime storage to prevent exit callbacks from accessing an already-destroyed mutex or observer list. Related: #9284 (introduced the global registration). ### Tested Windows, Release, `BUILD_TESTING=ON`. **Initial fix — full CTest suites on main `9605300f29`, with and without the fix:** | Result | MSYS2 before | MSYS2 after | WinLibs before and after | |---|---:|---:|---:| | Total tests | 3,139 | 3,139 | 3,139 | | Passed | 1,382 | 3,121 | 3,122 | | Failed | 1,753 | 8 | 7 | | Skipped | 4 | 10 | 10 | | **`0xC0000142` failures** | **1,739** | **0** | **0** | All 1,739 DLL initialization failures were eliminated. No regressions were observed with WinLibs GCC 15.2.0. **Review follow-up — process-lifetime registry (`a6fd9994f7`):** Full rebuild and CTest suite on both toolchains. | Result | MSYS2 GCC 16.2.0 | WinLibs GCC 15.2.0 | |---|---:|---:| | Total tests | 3,159 | 3,159 | | Passed | 3,143 | 3,142 | | Failed | 6 | 7 | | Skipped | 10 | 10 | The six MSYS2 failures also occur on WinLibs. The additional WinLibs failure, `agc_driver`, passed when rerun individually, suggesting a possible intermittent or parallel-execution issue. No DLL initialization failures were reported. No regressions attributable to the registry lifetime change were identified. Conventions checks passed. **PR overlap:** The checker could not automatically compare five PRs that already conflict with main. Manual review found that #7509 and #10645 modify unrelated sections of `Equeue.cpp`, while #9293, #9297 and #9302 carry the original close-observer code from the already-merged #9284. None implements this initialization fix. ### 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 (n/a) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-30 22:56:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T22:56:31Z.

Review: CHANGES_REQUESTED


Reviewed — changes requested

The lazy registry fixes initialization order, but its normal static destruction can leave later process-exit callbacks using a destroyed registry.

What I ran

Reviewed head 1caa5a7cab4e1635f0359939512ba66cccded7a4 against base bd5d95a5e47fcb0ec442d7d5e504ccfa8b6b258a. Environment: Debian GNU/Linux 12, x86_64, Python 3.11.2; no C++ compiler invocation or build was run.

python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13668
git diff --check 1caa5a7cab4e1635f0359939512ba66cccded7a4^..1caa5a7cab4e1635f0359939512ba66cccded7a4

Results

Check Result
Precheck on exact head/base Diff, conventions/template and live stack/mergeability passed. ABI audit and test-quality helper could not execute because those workspace-only scripts are absent from the isolated PR checkout; neither applies materially to this one-file implementation change.
git diff --check Passed (no output).
Hosted checks Windows, conventions, portability, intake, labels, report and eligibility passed. Linux and specialized jobs were skipped. The Windows workflow did not run the full library build/CTest suite.
CTest suites Author reports before/after Windows runs of 3,139 tests on main 9605300f29; not independently reproduced in this review.
Unedited output from local checks
/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13668-1caa5a7cab4e/report.md
/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13668-1caa5a7cab4e/report.json
Conclusion: checks failed

Findings

Blocking

GetCloseObserverRegistry() returns a destructible function-local static. If an earlier global initializer registers an atexit callback that later notifies a descriptor close, and a later initializer first creates this registry, C++ exit ordering can destroy the registry before that callback runs. The subsequent notification locks a destroyed mutex and copies a destroyed vector. A reviewer reports an ASan lifecycle probe reproducing this on the exact head; I did not independently run that probe. Keep the registry alive through process exit (for example, process-lifetime storage) and add a focused regression for notification during exit where the test environment permits.

Non-blocking

The description says #10194 reported the failure, but #10194 is an unrelated PR. Please correct or remove that reference.

Sources

Not verified

I did not build or run CTest, reproduce the ASan lifecycle probe, or test Linux, macOS, or console behavior. The full CTest results in the PR description are author-reported; the successful hosted Windows job skipped the full library build and broad CTest steps.

To get this to a good state

Please give the registry process lifetime, add a regression for process-exit notification if feasible, and fix the unrelated #10194 reference. I will rerun the same relevant checks against the updated head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5719560162 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13668#pullrequestreview-5719560162). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-30T22:56:31Z. **Review:** CHANGES_REQUESTED --- # Reviewed — changes requested The lazy registry fixes initialization order, but its normal static destruction can leave later process-exit callbacks using a destroyed registry. ## What I ran Reviewed head `1caa5a7cab4e1635f0359939512ba66cccded7a4` against base `bd5d95a5e47fcb0ec442d7d5e504ccfa8b6b258a`. Environment: Debian GNU/Linux 12, x86_64, Python 3.11.2; no C++ compiler invocation or build was run. ```sh python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13668 git diff --check 1caa5a7cab4e1635f0359939512ba66cccded7a4^..1caa5a7cab4e1635f0359939512ba66cccded7a4 ``` ## Results | Check | Result | |---|---| | Precheck on exact head/base | Diff, conventions/template and live stack/mergeability passed. ABI audit and test-quality helper could not execute because those workspace-only scripts are absent from the isolated PR checkout; neither applies materially to this one-file implementation change. | | `git diff --check` | Passed (no output). | | Hosted checks | Windows, conventions, portability, intake, labels, report and eligibility passed. Linux and specialized jobs were skipped. The Windows workflow did not run the full library build/CTest suite. | | CTest suites | Author reports before/after Windows runs of 3,139 tests on `main` `9605300f29`; not independently reproduced in this review. | <details> <summary>Unedited output from local checks</summary> ```text /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13668-1caa5a7cab4e/report.md /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13668-1caa5a7cab4e/report.json Conclusion: checks failed ``` </details> ## Findings ### Blocking `GetCloseObserverRegistry()` returns a destructible function-local static. If an earlier global initializer registers an `atexit` callback that later notifies a descriptor close, and a later initializer first creates this registry, C++ exit ordering can destroy the registry before that callback runs. The subsequent notification locks a destroyed mutex and copies a destroyed vector. A reviewer reports an ASan lifecycle probe reproducing this on the exact head; I did not independently run that probe. Keep the registry alive through process exit (for example, process-lifetime storage) and add a focused regression for notification during exit where the test environment permits. ### Non-blocking The description says #10194 reported the failure, but #10194 is an unrelated PR. Please correct or remove that reference. ## Sources - [Registry implementation at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/1caa5a7cab4e1635f0359939512ba66cccded7a4/core/libs/prx/libkernel/Equeue/Equeue.cpp) - [Current PR discussion, including the reported lifecycle probe](https://github.com/boykopovar/AnyPS5/pull/13668#issuecomment-6535235568) ## Not verified I did not build or run CTest, reproduce the ASan lifecycle probe, or test Linux, macOS, or console behavior. The full CTest results in the PR description are author-reported; the successful hosted Windows job skipped the full library build and broad CTest steps. ## To get this to a good state Please give the registry process lifetime, add a regression for process-exit notification if feasible, and fix the unrelated #10194 reference. I will rerun the same relevant checks against the updated head.
MarcZX10 commented 2026-10-31 01:34:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-31T01:34:11Z.

Review: APPROVED


Re-checked — new head, looks good

The new head keeps the lazily initialized close-observer registry alive through process shutdown, addressing the static-destruction-order blocker from my previous review.

What I ran

Reviewed head a6fd9994f7a1ea25fb728fecf45937ef38f2dd12 against PR base bd5d95a5e47fcb0ec442d7d5e504ccfa8b6b258a; the merge preview against current main 539c3425f886a9fed61f8e33448e09f21504be91 was clean. Environment: Linux 6.1.0-54-amd64 x86_64, Python 3.11.2, GCC 12.2 available; no local build or C++ tests were run.

python3 /marc/AnyPS5/tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13668

Results

Check Result
Diff, conventions, and PR template Passed.
Current-main merge preview Clean.
ABI and test-quality helper scripts Not run; those scripts are absent from the isolated PR checkout and do not materially apply to this one-file lifetime fix. The aggregate precheck therefore reports a tooling failure.
Hosted checks Eligibility, conventions, and portability passed; Windows was still in progress. Linux and specialized jobs were skipped.
Build and CTest The author reports full builds with MSYS2 UCRT64 GCC 16.2 and WinLibs GCC 15.2, and 3,159 tests on each. They report six common failures on both toolchains and one additional WinLibs failure that passed when rerun individually. I did not independently reproduce these runs.

The fix intentionally retains a small registry allocation for process lifetime; the static pointer remains reachable, while the registry object no longer runs a destructor before later exit callbacks.

Findings

Blocking

None found in the updated diff.

Non-blocking

The Windows hosted check was still running when checked, and I did not independently run the author-reported build or CTest suites.

Sources

Not verified

I did not run a local build, C++ tests, Linux runtime, or console behavior.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5720380486 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13668#pullrequestreview-5720380486). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-31T01:34:11Z. **Review:** APPROVED --- # Re-checked — new head, looks good The new head keeps the lazily initialized close-observer registry alive through process shutdown, addressing the static-destruction-order blocker from my previous review. ## What I ran Reviewed head `a6fd9994f7a1ea25fb728fecf45937ef38f2dd12` against PR base `bd5d95a5e47fcb0ec442d7d5e504ccfa8b6b258a`; the merge preview against current `main` `539c3425f886a9fed61f8e33448e09f21504be91` was clean. Environment: Linux 6.1.0-54-amd64 x86_64, Python 3.11.2, GCC 12.2 available; no local build or C++ tests were run. ```sh python3 /marc/AnyPS5/tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13668 ``` ## Results | Check | Result | |---|---| | Diff, conventions, and PR template | Passed. | | Current-main merge preview | Clean. | | ABI and test-quality helper scripts | Not run; those scripts are absent from the isolated PR checkout and do not materially apply to this one-file lifetime fix. The aggregate precheck therefore reports a tooling failure. | | Hosted checks | Eligibility, conventions, and portability passed; Windows was still in progress. Linux and specialized jobs were skipped. | | Build and CTest | The author reports full builds with MSYS2 UCRT64 GCC 16.2 and WinLibs GCC 15.2, and 3,159 tests on each. They report six common failures on both toolchains and one additional WinLibs failure that passed when rerun individually. I did not independently reproduce these runs. | The fix intentionally retains a small registry allocation for process lifetime; the static pointer remains reachable, while the registry object no longer runs a destructor before later exit callbacks. ## Findings ### Blocking None found in the updated diff. ### Non-blocking The Windows hosted check was still running when checked, and I did not independently run the author-reported build or CTest suites. ## Sources - [Process-lifetime close-observer registry at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/a6fd9994f7a1ea25fb728fecf45937ef38f2dd12/core/libs/prx/libkernel/Equeue/Equeue.cpp#L939-L962) - [Author's exact-head validation report](https://github.com/boykopovar/AnyPS5/pull/13668#issuecomment-6537198417) ## Not verified I did not run a local build, C++ tests, Linux runtime, or console behavior.
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!13668
No description provided.