fix(agc): keep the shader preparation lifecycle mutex alive through exit #9738

Merged
mugurc merged 1 commit from upstream-pr/9738 into main 2026-10-29 23:56:52 +00:00
mugurc commented 2026-10-22 11:07:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mugurc on 2026-10-22T11:07:06Z.


What

agc_driver_registration_prepare_stack passes its checks on macOS and then ends in terminate with "mutex lock failed: Invalid argument" as the process exits. Part of the macOS series announced on #1 (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6124344298).

The driver singleton's destructor runs at exit, and Driver::stop closes the shader preparation pools through CloseRegistrationPreparation, which locks the pools' lifecycle mutex. That mutex is a function-local static, constructed on its first use, after the driver, so it is destroyed before the driver's destructor runs. glibc locks a destroyed mutex without complaint; macOS fails the lock with EINVAL, and std::mutex::lock throws.

The mutex is now allocated once and never destroyed, as the pools it guards already are.

Tested

macOS 26.5.1, Apple M5 Max, Apple clang 21, x86-64 build under Rosetta 2, no Vulkan loader. The libraries do not link on macOS without the rest of the macOS series, so the run is on main 7e2ba73e4 with the series (ci/macos-series-2 on my fork) and the libSceSsl change of #7332.

  • lldb, with a breakpoint on __cxa_throw, shows the throw at exit: exit → __cxa_finalize_ranges → Driver::~Driver → Driver::stop → CloseRegistrationPreparation → std::mutex::lock.
  • agc_driver_registration_prepare_stack fails without this PR and passes with it, 3 runs of 3.
  • Linux and Windows: not run. The mutex is the same object on every host and is no longer destroyed at exit, which no code after the driver's destructor relies on.

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 (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: none; the change is in the shutdown of the shader preparation pools, which the test runs without a GPU
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4966348836 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9738). Originally posted by [mugurc](https://github.com/mugurc) on 2026-10-22T11:07:06Z. --- ### What `agc_driver_registration_prepare_stack` passes its checks on macOS and then ends in `terminate` with "mutex lock failed: Invalid argument" as the process exits. Part of the macOS series announced on #1 (https://github.com/boykopovar/AnyPS5/issues/1#issuecomment-6124344298). The driver singleton's destructor runs at exit, and `Driver::stop` closes the shader preparation pools through `CloseRegistrationPreparation`, which locks the pools' lifecycle mutex. That mutex is a function-local static, constructed on its first use, after the driver, so it is destroyed before the driver's destructor runs. glibc locks a destroyed mutex without complaint; macOS fails the lock with `EINVAL`, and `std::mutex::lock` throws. The mutex is now allocated once and never destroyed, as the pools it guards already are. ### Tested macOS 26.5.1, Apple M5 Max, Apple clang 21, x86-64 build under Rosetta 2, no Vulkan loader. The libraries do not link on macOS without the rest of the macOS series, so the run is on `main` 7e2ba73e4 with the series (`ci/macos-series-2` on my fork) and the libSceSsl change of #7332. - lldb, with a breakpoint on `__cxa_throw`, shows the throw at exit: `exit` → `__cxa_finalize_ranges` → `Driver::~Driver` → `Driver::stop` → `CloseRegistrationPreparation` → `std::mutex::lock`. - `agc_driver_registration_prepare_stack` fails without this PR and passes with it, 3 runs of 3. - Linux and Windows: not run. The mutex is the same object on every host and is no longer destroyed at exit, which no code after the driver's destructor relies on. ### 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 (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: none; the change is in the shutdown of the shader preparation pools, which the test runs without a GPU - [x] AI-assisted: yes
MarcZX10 commented 2026-10-22 11:54:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T11:54:07Z.


I compared this exact change with open #8658. Its current head already changes RegistrationPreparePool::Lifecycle() to the same never-destroyed new std::mutex on Apple (static auto* lifecycle = new std::mutex). The pairwise merge also conflicts in ShaderRegistry.cpp, and #8658 includes substantially broader macOS support.

Please clarify the intended merge order for this duplicate hunk. If this standalone fix should land first, record the relationship/order and make sure #8658 removes or rebases over the duplicate; if #8658 is intended to supply it, this PR should not land the same change independently. The current Depends on: none leaves the overlap unresolved.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6340095629 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9738#issuecomment-6340095629). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T11:54:07Z. --- I compared this exact change with open #8658. Its current head already changes `RegistrationPreparePool::Lifecycle()` to the same never-destroyed `new std::mutex` on Apple (`static auto* lifecycle = new std::mutex`). The pairwise merge also conflicts in `ShaderRegistry.cpp`, and #8658 includes substantially broader macOS support. Please clarify the intended merge order for this duplicate hunk. If this standalone fix should land first, record the relationship/order and make sure #8658 removes or rebases over the duplicate; if #8658 is intended to supply it, this PR should not land the same change independently. The current `Depends on: none` leaves the overlap unresolved.
MarcZX10 commented 2026-10-22 11:59:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T11:59:38Z.

Review: CHANGES_REQUESTED


There are two merge blockers: 1. Open #8658 already contains the identical RegistrationPreparePool::Lifecycle() change on Apple (static auto* lifecycle = new std::mutex), and the current pairwise merge conflicts in ShaderRegistry.cpp. Please agree with #8658’s author which change lands first and record the ordering/dependency; make sure the final branch contains the fix once. 2. This is a new generic PR with Unblocks: none. The focused macOS reproduction is useful evidence. This branch is also 4 commits behind current main; if it proceeds later, refresh and rerun the focused exit test and overlap check. Current product CI jobs are skipped.

I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5613779954 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9738#pullrequestreview-5613779954). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T11:59:38Z. **Review:** CHANGES_REQUESTED --- There are two merge blockers: 1. Open #8658 already contains the identical `RegistrationPreparePool::Lifecycle()` change on Apple (`static auto* lifecycle = new std::mutex`), and the current pairwise merge conflicts in `ShaderRegistry.cpp`. Please agree with #8658’s author which change lands first and record the ordering/dependency; make sure the final branch contains the fix once. 2. This is a new generic PR with `Unblocks: none`. The focused macOS reproduction is useful evidence. This branch is also 4 commits behind current `main`; if it proceeds later, refresh and rerun the focused exit test and overlap check. Current product CI jobs are skipped. I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.
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!9738
No description provided.