fix(libSceNgs2.native): keep the system list and its mutex alive through static teardown #1197

Merged
gookie-dev merged 1 commit from upstream-pr/1197 into main 2026-10-07 20:23:18 +00:00
gookie-dev commented 2026-10-07 17:16:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gookie-dev on 2026-10-07T17:16:10Z.


What

NGS2 looks up every system, rack and voice handle in a function-local std::vector guarded by a function-local std::recursive_mutex (System.cpp). Both were destroyed with the other statics when the process exits. std::exit runs the destructors on the exiting thread while the title's other threads keep running, so an audio thread that renders during exit finds the freed vector:

  • sceNgs2SystemRender returns SCE_NGS2_ERROR_INVALID_SYSTEM_HANDLE (the freed vector's first element now holds the allocator's free-list pointer), and
  • a lookup that walks the racks (Ngs2FindVoice from sceNgs2VoiceGetState) dereferences freed memory and segfaults.

Both objects are now allocated once and never destroyed, as other driver state that must outlive static teardown already is (Draw.cpp, Pipeline.cpp, Recorder.cpp).

Tested

Pop!_OS 24.04 (kernel 7.1.5), GCC 13.3.0, CMake 3.28.3, Ninja, Release, BUILD_TESTING=ON, SPIRV-Tools validation off, NVIDIA GeForce RTX 4050 Laptop GPU.

  • Default build and libs packaging pass. ctest: 390 of 390 pass.
  • ngs2_render now registers an atexit handler before its first NGS2 call. The handler runs after the library's statics are destroyed, renders a system created at the end of main and destroys it. Without this change it fails on that render (sceNgs2SystemRender does not return SCE_NGS2_OK).
  • PPSA21402 (Balatro 01.015.000), relinked for Linux on main + #1020, #861, #1038 and #1100, which it needs to reach its menu here. The title was closed through SDL_QUIT (SIGTERM) after 20 to 45 s. Without this change 4 of 6 runs printed Error: sceNgs2SystemRender() 804A0230 and 4 of 6 ended with a segfault (the core dump examined: Ngs2FindVoice called from sceNgs2VoiceGetState). With it, 6 of 6 exited with status 0 and no NGS2 error.
  • Windows not built or tested.

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 in TechnicalDebt
  • 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
  • Generic behaviour, not specific to one title
  • Depends on: none (merges cleanly with #959, #964, #1020 and #1038, which also touch Ngs2Render.cpp or NGS2)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4775999548 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1197). Originally posted by [gookie-dev](https://github.com/gookie-dev) on 2026-10-07T17:16:10Z. --- ### What NGS2 looks up every system, rack and voice handle in a function-local `std::vector` guarded by a function-local `std::recursive_mutex` (`System.cpp`). Both were destroyed with the other statics when the process exits. `std::exit` runs the destructors on the exiting thread while the title's other threads keep running, so an audio thread that renders during exit finds the freed vector: - `sceNgs2SystemRender` returns `SCE_NGS2_ERROR_INVALID_SYSTEM_HANDLE` (the freed vector's first element now holds the allocator's free-list pointer), and - a lookup that walks the racks (`Ngs2FindVoice` from `sceNgs2VoiceGetState`) dereferences freed memory and segfaults. Both objects are now allocated once and never destroyed, as other driver state that must outlive static teardown already is (`Draw.cpp`, `Pipeline.cpp`, `Recorder.cpp`). ### Tested Pop!_OS 24.04 (kernel 7.1.5), GCC 13.3.0, CMake 3.28.3, Ninja, Release, `BUILD_TESTING=ON`, SPIRV-Tools validation off, NVIDIA GeForce RTX 4050 Laptop GPU. - Default build and `libs` packaging pass. `ctest`: 390 of 390 pass. - `ngs2_render` now registers an `atexit` handler before its first NGS2 call. The handler runs after the library's statics are destroyed, renders a system created at the end of `main` and destroys it. Without this change it fails on that render (`sceNgs2SystemRender` does not return `SCE_NGS2_OK`). - PPSA21402 (Balatro 01.015.000), relinked for Linux on `main` + #1020, #861, #1038 and #1100, which it needs to reach its menu here. The title was closed through `SDL_QUIT` (SIGTERM) after 20 to 45 s. Without this change 4 of 6 runs printed `Error: sceNgs2SystemRender() 804A0230` and 4 of 6 ended with a segfault (the core dump examined: `Ngs2FindVoice` called from `sceNgs2VoiceGetState`). With it, 6 of 6 exited with status 0 and no NGS2 error. - Windows not built or tested. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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 - [x] Generic behaviour, not specific to one title - [x] Depends on: none (merges cleanly with #959, #964, #1020 and #1038, which also touch `Ngs2Render.cpp` or NGS2) - [x] AI-assisted: yes
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!1197
No description provided.