fix(libSceNgs2): keep a rack lock and a voice callback from blocking other threads #8238

Merged
milescoviello merged 4 commits from upstream-pr/8238 into main 2026-10-21 02:02:32 +00:00
milescoviello commented 2026-10-20 05:27:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T05:27:31Z.


What

sceNgs2RackLock takes the library's internal recursive mutex (Ngs2Mutex) and keeps it until sceNgs2RackUnlock. A render also holds that mutex while it calls a voice's block-end or repeat callback. So while a game thread holds a rack lock, or a callback is running, every NGS2 call from another thread waits.

A title deadlocks when it takes one of its own locks while it holds a rack lock (or inside the callback), and another of its threads holds that lock and calls any NGS2 function. PPSA36591 does this. It imports sceNgs2RackLock/sceNgs2RackUnlock and froze a few minutes into a run on Linux (RTX 5090 Laptop):

  • every thread was in futex_wait;
  • one thread was in pthread_mutex_lock under sceNgs2VoiceGetStateFlags;
  • the main thread, which owned Ngs2Mutex, was in scePthreadMutexLock on one of the title's own locks.

The existing sceNgs2SystemLock test already models the console: another thread's sceNgs2VoiceGetStateFlags and sceNgs2SystemGetInfo return while the system is locked, and only rendering is held back.

How

  • sceNgs2RackLock records an owner thread and a depth on the rack (nested, like the system lock), under the internal lock but without keeping it. sceNgs2RackUnlock drops one level and wakes waiters at zero. A thread that doesn't hold the rack's lock gets SCE_NGS2_ERROR_INVALID_RACK_HANDLE instead of unlocking a mutex it doesn't own.
  • sceNgs2SystemRender waits, with the internal lock released, while another thread holds a lock on one of the system's racks, so a rack still isn't rendered mid-update. Other NGS2 calls from other threads go ahead.
  • The voice callback runs with the internal lock released. Every std::lock_guard on Ngs2Mutex is now an Ngs2Lock, which counts the depth per thread, so Ngs2Unlocked can release the lock fully and take it back at the same depth.
  • While the callback runs, another thread could now destroy what the render still walks. So sceNgs2RackDestroy and sceNgs2SystemDestroy wait while another thread renders the system (Ngs2System::renderer, which the render saves and restores so a nested render on the same thread is unaffected).
  • UserFx process handlers still run with the lock held: they get pointers into voice buffers that a concurrent setup could reallocate.
  • A debt entry records what isn't measured on a console: other threads' calls while a rack is locked, and the unlock error code.

Tested

Linux (Ubuntu 24.04, GCC 13.3), on main a98590dec2.

  • New ngs2_render/ngs2_core_render cases, both of which time out after 2 s instead of hanging:
    • TestRackLockLeavesQueries: while the test holds a rack lock twice, another thread's sceNgs2VoiceGetStateFlags must return. A render on a third thread must not finish until the second unlock, and a third unlock must fail. With main's NGS2 sources it fails at Require(answeredWhileLocked).
    • TestVoiceCallbackLeavesQueries: a block-end callback starts a thread that calls sceNgs2VoiceGetStateFlags, and that call must return while the callback runs. With main's sources (and the rack test left out, so this case runs) it fails at Require(callback.answered && callback.answeredInCallback).
    • TestRackLockWaitsForRender (added in 0fdc0e22b3 after @wachi35676's report below): a block-end callback starts a thread that calls sceNgs2RackLock on the rack being rendered, then waits up to 200 ms for it. The lock must not be granted until the render has left the callback. Without the Rack.cpp line from 0fdc0e22b3, ngs2_render and ngs2_core_render fail at that check 3 of 3, because the other thread takes the rack mid-render. With it, they pass.
    • TestCallbacksLockEachOthersSystem and TestRackLocksAcrossSystemsAndRender (added in 850b807cf2 after @wachi35676's second report): each case either makes two systems' callbacks lock a rack of the other system, or makes a thread holding a rack of one system lock a rack of the system another thread renders. Every render and lock must return within 2 s. With the sources of 0fdc0e22b3, ngs2_render and ngs2_core_render deadlock on each case (run separately) 3 of 3. With 850b807cf2, they pass.
    • TestRackDestroyWaitsForLock (741e706b19, after @wachi35676's third report): while one thread holds a rack lock, another thread's sceNgs2RackDestroy of that rack must not return until the lock is released. With the sources of 850b807cf2, ngs2_render and ngs2_core_render fail at that check 3 of 3, because the destroy returns at once. With 741e706b19, they pass.
  • The 13 ^ngs2 tests pass 3 of 3 on 741e706b19.
  • @wachi35676 (comment), on 741e706b1 merged onto main (Windows 11 and Ubuntu 24.04 WSL2): 13 of 13 pass 3 of 3 on each, with the negative controls failing at the new checks. A destroy from another thread waits for a rack lock or a render as on main. A thread that destroys a rack it holds itself no longer leaves other threads stuck, which it does on main.
  • @wachi35676, on 850b807cf merged onto main 44e3389c8 (Windows 11 and Ubuntu 24.04 WSL2): 13 of 13 pass 3 of 3 on each. Both deadlock probes return. Renders and rack locks run one at a time again, as on main, and other calls still go ahead.
  • @wachi35676, Windows 11, WinLibs GCC 15.2.0 UCRT, on a5b068543 merged onto main 02fb1ce53: the 13 ^ngs2_ tests pass 3 of 3, and with main's sources ngs2_render and ngs2_core_render fail at answeredWhileLocked. On 0fdc0e22b (Windows 11 and Ubuntu 24.04 WSL2): 13 of 13 pass 3 of 3 on each, the new rack-lock test fails with Rack.cpp set back, and a callback's own RackLock, VoiceGetStateFlags, RackGetInfo and second-rack lock return on its render thread.
  • PPSA36591 (01.002.000) on Linux, RTX 4070 Ti, release libs from this branch, on the display. It played 14 minutes in one run and 10 in another, through the prologue and the first chapter's scenes, with no freeze. Quick save and quick load work. On 0fdc0e22b3, 850b807cf2 and 741e706b19, 400 scripted presses over 715 s through the prologue into the conference scene at 60 FPS, no freeze. The freeze took a few minutes of play on the laptop above, but main didn't freeze on this host within 8 minutes either. So the in-game runs show nothing regresses, and the two cases above are what reproduce the deadlock.

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
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: PPSA36591
  • GPUs tested: n/a (audio library); title runs on NVIDIA GeForce RTX 4070 Ti, driver 595.99.02, Ubuntu 24.04
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4931435382 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8238). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-20T05:27:31Z. --- ### What `sceNgs2RackLock` takes the library's internal recursive mutex (`Ngs2Mutex`) and keeps it until `sceNgs2RackUnlock`. A render also holds that mutex while it calls a voice's block-end or repeat callback. So while a game thread holds a rack lock, or a callback is running, every NGS2 call from another thread waits. A title deadlocks when it takes one of its own locks while it holds a rack lock (or inside the callback), and another of its threads holds that lock and calls any NGS2 function. PPSA36591 does this. It imports `sceNgs2RackLock`/`sceNgs2RackUnlock` and froze a few minutes into a run on Linux (RTX 5090 Laptop): - every thread was in `futex_wait`; - one thread was in `pthread_mutex_lock` under `sceNgs2VoiceGetStateFlags`; - the main thread, which owned `Ngs2Mutex`, was in `scePthreadMutexLock` on one of the title's own locks. The existing `sceNgs2SystemLock` test already models the console: another thread's `sceNgs2VoiceGetStateFlags` and `sceNgs2SystemGetInfo` return while the system is locked, and only rendering is held back. ### How - `sceNgs2RackLock` records an owner thread and a depth on the rack (nested, like the system lock), under the internal lock but without keeping it. `sceNgs2RackUnlock` drops one level and wakes waiters at zero. A thread that doesn't hold the rack's lock gets `SCE_NGS2_ERROR_INVALID_RACK_HANDLE` instead of unlocking a mutex it doesn't own. - `sceNgs2SystemRender` waits, with the internal lock released, while another thread holds a lock on one of the system's racks, so a rack still isn't rendered mid-update. Other NGS2 calls from other threads go ahead. - The voice callback runs with the internal lock released. Every `std::lock_guard` on `Ngs2Mutex` is now an `Ngs2Lock`, which counts the depth per thread, so `Ngs2Unlocked` can release the lock fully and take it back at the same depth. - While the callback runs, another thread could now destroy what the render still walks. So `sceNgs2RackDestroy` and `sceNgs2SystemDestroy` wait while another thread renders the system (`Ngs2System::renderer`, which the render saves and restores so a nested render on the same thread is unaffected). - UserFx process handlers still run with the lock held: they get pointers into voice buffers that a concurrent setup could reallocate. - A debt entry records what isn't measured on a console: other threads' calls while a rack is locked, and the unlock error code. ### Tested Linux (Ubuntu 24.04, GCC 13.3), on `main` a98590dec2. - New `ngs2_render`/`ngs2_core_render` cases, both of which time out after 2 s instead of hanging: - `TestRackLockLeavesQueries`: while the test holds a rack lock twice, another thread's `sceNgs2VoiceGetStateFlags` must return. A render on a third thread must not finish until the second unlock, and a third unlock must fail. With `main`'s NGS2 sources it fails at `Require(answeredWhileLocked)`. - `TestVoiceCallbackLeavesQueries`: a block-end callback starts a thread that calls `sceNgs2VoiceGetStateFlags`, and that call must return while the callback runs. With `main`'s sources (and the rack test left out, so this case runs) it fails at `Require(callback.answered && callback.answeredInCallback)`. - `TestRackLockWaitsForRender` (added in 0fdc0e22b3 after @wachi35676's report below): a block-end callback starts a thread that calls `sceNgs2RackLock` on the rack being rendered, then waits up to 200 ms for it. The lock must not be granted until the render has left the callback. Without the `Rack.cpp` line from 0fdc0e22b3, `ngs2_render` and `ngs2_core_render` fail at that check 3 of 3, because the other thread takes the rack mid-render. With it, they pass. - `TestCallbacksLockEachOthersSystem` and `TestRackLocksAcrossSystemsAndRender` (added in 850b807cf2 after @wachi35676's second report): each case either makes two systems' callbacks lock a rack of the other system, or makes a thread holding a rack of one system lock a rack of the system another thread renders. Every render and lock must return within 2 s. With the sources of 0fdc0e22b3, `ngs2_render` and `ngs2_core_render` deadlock on each case (run separately) 3 of 3. With 850b807cf2, they pass. - `TestRackDestroyWaitsForLock` (741e706b19, after @wachi35676's third report): while one thread holds a rack lock, another thread's `sceNgs2RackDestroy` of that rack must not return until the lock is released. With the sources of 850b807cf2, `ngs2_render` and `ngs2_core_render` fail at that check 3 of 3, because the destroy returns at once. With 741e706b19, they pass. - The 13 `^ngs2` tests pass 3 of 3 on 741e706b19. - @wachi35676 ([comment](https://github.com/boykopovar/AnyPS5/pull/8238#issuecomment-6292795752)), on `741e706b1` merged onto `main` (Windows 11 and Ubuntu 24.04 WSL2): 13 of 13 pass 3 of 3 on each, with the negative controls failing at the new checks. A destroy from another thread waits for a rack lock or a render as on `main`. A thread that destroys a rack it holds itself no longer leaves other threads stuck, which it does on `main`. - @wachi35676, on `850b807cf` merged onto `main` `44e3389c8` (Windows 11 and Ubuntu 24.04 WSL2): 13 of 13 pass 3 of 3 on each. Both deadlock probes return. Renders and rack locks run one at a time again, as on `main`, and other calls still go ahead. - @wachi35676, Windows 11, WinLibs GCC 15.2.0 UCRT, on `a5b068543` merged onto `main` `02fb1ce53`: the 13 `^ngs2_` tests pass 3 of 3, and with `main`'s sources `ngs2_render` and `ngs2_core_render` fail at `answeredWhileLocked`. On `0fdc0e22b` (Windows 11 and Ubuntu 24.04 WSL2): 13 of 13 pass 3 of 3 on each, the new rack-lock test fails with `Rack.cpp` set back, and a callback's own `RackLock`, `VoiceGetStateFlags`, `RackGetInfo` and second-rack lock return on its render thread. - PPSA36591 (01.002.000) on Linux, RTX 4070 Ti, release libs from this branch, on the display. It played 14 minutes in one run and 10 in another, through the prologue and the first chapter's scenes, with no freeze. Quick save and quick load work. On 0fdc0e22b3, 850b807cf2 and 741e706b19, 400 scripted presses over 715 s through the prologue into the conference scene at 60 FPS, no freeze. The freeze took a few minutes of play on the laptop above, but `main` didn't freeze on this host within 8 minutes either. So the in-game runs show nothing regresses, and the two cases above are what reproduce the deadlock. ### 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 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 - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: PPSA36591 - [x] GPUs tested: n/a (audio library); title runs on NVIDIA GeForce RTX 4070 Ti, driver 595.99.02, Ubuntu 24.04 - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-20 05:30:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T05:30:04Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 8609 core/libs/prx/libSceNgs2.native/src/Rack.cpp L175-176, L183-184

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 1384 core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp
core/libs/prx/libSceNgs2.native/src/Rack.cpp
core/libs/prx/libSceNgs2.native/src/Render.cpp
core/libs/prx/libSceNgs2.native/src/System.cpp
core/libs/prx/libSceNgs2.native/src/Voice.cpp
PR 5797 core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp
core/libs/prx/libSceNgs2.native/src/Render.cpp
core/libs/prx/libSceNgs2.native/src/Voice.cpp
docs/dev/debt/libSceNgs2.native.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6284125973 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8238#issuecomment-6284125973). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T05:30:04Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 8609](https://www.github.com/boykopovar/AnyPS5/pull/8609) | `core/libs/prx/libSceNgs2.native/src/Rack.cpp` [L175-176](https://github.com/boykopovar/AnyPS5/blob/b85d972f4208d4821a994c8d94eb3d7b78561ecd/core/libs/prx/libSceNgs2.native/src/Rack.cpp#L175-L176), [L183-184](https://github.com/boykopovar/AnyPS5/blob/b85d972f4208d4821a994c8d94eb3d7b78561ecd/core/libs/prx/libSceNgs2.native/src/Rack.cpp#L183-L184) | | | 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 1384](https://www.github.com/boykopovar/AnyPS5/pull/1384) | `core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp`<br>`core/libs/prx/libSceNgs2.native/src/Rack.cpp`<br>`core/libs/prx/libSceNgs2.native/src/Render.cpp`<br>`core/libs/prx/libSceNgs2.native/src/System.cpp`<br>`core/libs/prx/libSceNgs2.native/src/Voice.cpp` | | [PR 5797](https://www.github.com/boykopovar/AnyPS5/pull/5797) | `core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hpp`<br>`core/libs/prx/libSceNgs2.native/src/Render.cpp`<br>`core/libs/prx/libSceNgs2.native/src/Voice.cpp`<br>`docs/dev/debt/libSceNgs2.native.md` |
wachi35676 commented 2026-10-20 11:29:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T11:29:29Z.


Windows run of a5b068543 merged onto main 02fb1ce53 (clean merge). Windows 11 (10.0.26300), Ryzen 5 9600X, WinLibs GCC 15.2.0 UCRT, Release, BUILD_TESTING=ON.

  • ctest -R "^ngs2_": 13 of 13 pass, 3 runs out of 3. The build log has no warnings.
  • With the five libSceNgs2.native/src files reverted to main, ngs2_render and ngs2_core_render fail with NGS2 check failed at line 944 (answeredWhileLocked). The other 11 still pass.

One gap in the rule "sceNgs2SystemRender waits while another thread holds a lock on one of the system's racks, so a rack still isn't rendered mid-update". The check runs only when a render starts. Because the voice callback now runs with the internal lock released, another thread can take sceNgs2RackLock on the rack that is being rendered, and the render then finishes that rack while the other thread holds the lock.

I added a temporary case to Ngs2Render.cpp, run before the PR's new cases so that it also runs on main:

  • A sampler voice in rack R is patched to the mastering voice and plays a block of Grain / 2 frames, with a BLOCK_END callback.
  • In the callback, thread B calls sceNgs2RackLock(R), records whether the render has returned yet, and keeps the lock. The callback waits up to 500 ms for B, then returns.
  • After sceNgs2SystemRender returns, the main thread lets B unlock.
NGS2 sources B got the rack lock before the render returned
this PR (3 runs) yes yes, and the render returned while B still held R's lock
main yes no (B waited for the render to end)

So with this change the rest of a render can run while another thread holds the lock of the rack it is rendering. The two new cases don't cover that. Making RackLock wait for a render in progress, as RackDestroy does with Ngs2RenderingElsewhere, would close the gap. But it could bring back the deadlock this PR removes if the callback waits on a lock that the RackLock caller holds. So adding this case to the new debt entry may be the better fix.

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6289840463 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8238#issuecomment-6289840463). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-20T11:29:29Z. --- Windows run of `a5b068543` merged onto `main` `02fb1ce53` (clean merge). Windows 11 (10.0.26300), Ryzen 5 9600X, WinLibs GCC 15.2.0 UCRT, Release, `BUILD_TESTING=ON`. - `ctest -R "^ngs2_"`: 13 of 13 pass, 3 runs out of 3. The build log has no warnings. - With the five `libSceNgs2.native/src` files reverted to `main`, `ngs2_render` and `ngs2_core_render` fail with `NGS2 check failed at line 944` (`answeredWhileLocked`). The other 11 still pass. One gap in the rule "`sceNgs2SystemRender` waits while another thread holds a lock on one of the system's racks, so a rack still isn't rendered mid-update". The check runs only when a render starts. Because the voice callback now runs with the internal lock released, another thread can take `sceNgs2RackLock` on the rack that is being rendered, and the render then finishes that rack while the other thread holds the lock. I added a temporary case to `Ngs2Render.cpp`, run before the PR's new cases so that it also runs on `main`: - A sampler voice in rack R is patched to the mastering voice and plays a block of `Grain / 2` frames, with a BLOCK_END callback. - In the callback, thread B calls `sceNgs2RackLock(R)`, records whether the render has returned yet, and keeps the lock. The callback waits up to 500 ms for B, then returns. - After `sceNgs2SystemRender` returns, the main thread lets B unlock. | NGS2 sources | B got the rack lock | before the render returned | |---|---|---| | this PR (3 runs) | yes | **yes**, and the render returned while B still held R's lock | | `main` | yes | no (B waited for the render to end) | So with this change the rest of a render can run while another thread holds the lock of the rack it is rendering. The two new cases don't cover that. Making `RackLock` wait for a render in progress, as `RackDestroy` does with `Ngs2RenderingElsewhere`, would close the gap. But it could bring back the deadlock this PR removes if the callback waits on a lock that the `RackLock` caller holds. So adding this case to the new debt entry may be the better fix. AI-assisted: yes (Claude Code); results above were run on this machine.
milescoviello commented 2026-10-20 11:47:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T11:47:31Z.


Thanks @wachi35676, that gap is real, and the Windows run is useful. I fixed it instead of only noting it: in 0fdc0e22b3, sceNgs2RackLock also waits while another thread renders the rack's system. Before this PR, the rack lock was the internal mutex, so a RackLock from another thread always waited for the whole render, callbacks included. So the wait adds no deadlock that main didn't already have, and VoiceGetStateFlags and the other calls still go through during a rack lock, which is what PPSA36591 needed.

Your case is now TestRackLockWaitsForRender. Without the Rack.cpp line, ngs2_render and ngs2_core_render fail at it 3 of 3. With it, all 13 ^ngs2 tests pass 3 of 3. PPSA36591 then played 715 s (400 scripted presses, prologue to the conference scene) at 60 FPS without freezing. The debt entry now describes the wait. I added your Windows result to the description.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6290138773 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8238#issuecomment-6290138773). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-20T11:47:31Z. --- Thanks @wachi35676, that gap is real, and the Windows run is useful. I fixed it instead of only noting it: in 0fdc0e22b3, `sceNgs2RackLock` also waits while another thread renders the rack's system. Before this PR, the rack lock was the internal mutex, so a `RackLock` from another thread always waited for the whole render, callbacks included. So the wait adds no deadlock that `main` didn't already have, and `VoiceGetStateFlags` and the other calls still go through during a rack lock, which is what PPSA36591 needed. Your case is now `TestRackLockWaitsForRender`. Without the `Rack.cpp` line, `ngs2_render` and `ngs2_core_render` fail at it 3 of 3. With it, all 13 `^ngs2` tests pass 3 of 3. PPSA36591 then played 715 s (400 scripted presses, prologue to the conference scene) at 60 FPS without freezing. The debt entry now describes the wait. I added your Windows result to the description. AI-assisted: yes (Claude Code)
wachi35676 commented 2026-10-20 12:11:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T12:11:05Z.


Re-ran 0fdc0e22b merged onto main 02fb1ce53 (clean merge; also merges cleanly onto d50ec76da, which doesn't touch NGS2). Windows 11, WinLibs GCC 15.2.0, and Ubuntu 24.04 (WSL2) GCC 13.3, both Release.

  • ctest -R "^ngs2_": 13 of 13 pass, 3 runs out of 3 on each OS. Neither build log has warnings.
  • Negative control: with the five src files reverted to main, ngs2_render and ngs2_core_render fail on both OSes. With only Rack.cpp set back to a5b068543, the new TestRackLockWaitsForRender fails at Require(lockedAfterRender && !callback.lockedDuringCallback). So the new test covers the fix.
  • My earlier case, where another thread takes sceNgs2RackLock from inside the callback, now waits for the render to finish.

Self-deadlock check: a BLOCK_END callback calls these on its own render thread: sceNgs2RackLock on its own rack, sceNgs2VoiceGetStateFlags, sceNgs2RackGetInfo, and sceNgs2RackLock plus unlock on a second rack of the same system. All of them return 0 in 2 to 34 ms, 3 runs out of 3 on each OS, as on main, where the recursive internal mutex allowed it. Ngs2RenderingElsewhere skips the render thread itself, so there is no self-deadlock.

It does deadlock when two systems are involved. Probes run 3 times on each OS. "Deadlock" means no return within 3 s, and one run on Windows with a 15 s limit gave the same result.

Case main a5b068543 0fdc0e22b
1. T1 renders S1 and its callback locks a rack of S2. T2 renders S2 and its callback locks a rack of S1. Both callbacks are running. returns (~1 s; the renders run one after the other) returns deadlock
2. T2 holds RackLock(R2) (S2), then calls RackLock(R1) (S1). Meanwhile T1 renders S1 and its callback calls RackLock(R2). returns returns deadlock

In case 2, T2 waits for T1's render of S1 to end, and T1's callback waits for T2 to release R2. On main the rack lock was the internal mutex, so T1's render could not start while T2 held R2. Case 2 doesn't need a second renderer: a game thread that holds rack locks across two systems is enough, if a callback on another system's render thread locks one of them. Neither case is in the tests or in the updated debt entry. If the deadlock is accepted, a sentence in the entry would cover it: "a callback that takes a rack lock of another system can deadlock with that system's render or with a thread that holds that lock".

The probes are temporary cases in Ngs2Render.cpp, each with one sampler voice and a BLOCK_END callback. I can share them as test cases.

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6290536721 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8238#issuecomment-6290536721). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-20T12:11:05Z. --- Re-ran `0fdc0e22b` merged onto `main` `02fb1ce53` (clean merge; also merges cleanly onto `d50ec76da`, which doesn't touch NGS2). Windows 11, WinLibs GCC 15.2.0, and Ubuntu 24.04 (WSL2) GCC 13.3, both Release. - `ctest -R "^ngs2_"`: 13 of 13 pass, 3 runs out of 3 on each OS. Neither build log has warnings. - Negative control: with the five `src` files reverted to `main`, `ngs2_render` and `ngs2_core_render` fail on both OSes. With only `Rack.cpp` set back to `a5b068543`, the new `TestRackLockWaitsForRender` fails at `Require(lockedAfterRender && !callback.lockedDuringCallback)`. So the new test covers the fix. - My earlier case, where another thread takes `sceNgs2RackLock` from inside the callback, now waits for the render to finish. Self-deadlock check: a BLOCK_END callback calls these on its own render thread: `sceNgs2RackLock` on its own rack, `sceNgs2VoiceGetStateFlags`, `sceNgs2RackGetInfo`, and `sceNgs2RackLock` plus unlock on a second rack of the same system. All of them return 0 in 2 to 34 ms, 3 runs out of 3 on each OS, as on `main`, where the recursive internal mutex allowed it. `Ngs2RenderingElsewhere` skips the render thread itself, so there is no self-deadlock. It does deadlock when two systems are involved. Probes run 3 times on each OS. "Deadlock" means no return within 3 s, and one run on Windows with a 15 s limit gave the same result. | Case | `main` | `a5b068543` | `0fdc0e22b` | |---|---|---|---| | 1. T1 renders S1 and its callback locks a rack of S2. T2 renders S2 and its callback locks a rack of S1. Both callbacks are running. | returns (~1 s; the renders run one after the other) | returns | **deadlock** | | 2. T2 holds `RackLock(R2)` (S2), then calls `RackLock(R1)` (S1). Meanwhile T1 renders S1 and its callback calls `RackLock(R2)`. | returns | returns | **deadlock** | In case 2, T2 waits for T1's render of S1 to end, and T1's callback waits for T2 to release R2. On `main` the rack lock was the internal mutex, so T1's render could not start while T2 held R2. Case 2 doesn't need a second renderer: a game thread that holds rack locks across two systems is enough, if a callback on another system's render thread locks one of them. Neither case is in the tests or in the updated debt entry. If the deadlock is accepted, a sentence in the entry would cover it: "a callback that takes a rack lock of another system can deadlock with that system's render or with a thread that holds that lock". The probes are temporary cases in `Ngs2Render.cpp`, each with one sampler voice and a BLOCK_END callback. I can share them as test cases. AI-assisted: yes (Claude Code); results above were run on this machine.
milescoviello commented 2026-10-20 12:46:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T12:46:19Z.


Thanks @wachi35676, both cases are real, and they are worse than leaving the per-rack gap. I fixed them in 850b807cf2 instead of documenting them.

Rack locks and renders now exclude each other across all threads and systems, as the single internal mutex did on main. A render waits while another thread holds any rack lock, and a rack lock waits while another thread holds one or is rendering, including while that render is in a callback. A thread nests both, also from its own callbacks, so your self-deadlock list still returns. The other NGS2 calls (VoiceGetStateFlags, controls, queries) still don't wait for any of that, which is what PPSA36591 needed.

  • Case 1: the second render can't start until the first has finished, callbacks included, so the two callbacks never wait on each other.
  • Case 2: T2 holding R2 keeps T1's render of S1 from starting, so T2's lock of R1 goes through and T1's callback runs after T2 unlocks. On main the renders also ran one after the other.

Both of your probes are now tests, TestCallbacksLockEachOthersSystem and TestRackLocksAcrossSystemsAndRender. Each has a 200 ms handshake, so the threads meet where they deadlocked, and a 2 s limit on every render and lock. With the sources of 0fdc0e22b3, ngs2_render and ngs2_core_render deadlock on each case 3 of 3; I ran them separately, since the first one aborts the test. With 850b807cf2, all 13 ^ngs2 tests pass 3 of 3. PPSA36591 played the same 715 s (400 presses) at 60 FPS without freezing. The debt entry now describes this behavior.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6291165907 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8238#issuecomment-6291165907). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-20T12:46:19Z. --- Thanks @wachi35676, both cases are real, and they are worse than leaving the per-rack gap. I fixed them in 850b807cf2 instead of documenting them. Rack locks and renders now exclude each other across all threads and systems, as the single internal mutex did on `main`. A render waits while another thread holds any rack lock, and a rack lock waits while another thread holds one or is rendering, including while that render is in a callback. A thread nests both, also from its own callbacks, so your self-deadlock list still returns. The other NGS2 calls (`VoiceGetStateFlags`, controls, queries) still don't wait for any of that, which is what PPSA36591 needed. - Case 1: the second render can't start until the first has finished, callbacks included, so the two callbacks never wait on each other. - Case 2: T2 holding R2 keeps T1's render of S1 from starting, so T2's lock of R1 goes through and T1's callback runs after T2 unlocks. On `main` the renders also ran one after the other. Both of your probes are now tests, `TestCallbacksLockEachOthersSystem` and `TestRackLocksAcrossSystemsAndRender`. Each has a 200 ms handshake, so the threads meet where they deadlocked, and a 2 s limit on every render and lock. With the sources of 0fdc0e22b3, `ngs2_render` and `ngs2_core_render` deadlock on each case 3 of 3; I ran them separately, since the first one aborts the test. With 850b807cf2, all 13 `^ngs2` tests pass 3 of 3. PPSA36591 played the same 715 s (400 presses) at 60 FPS without freezing. The debt entry now describes this behavior. AI-assisted: yes (Claude Code)
wachi35676 commented 2026-10-20 13:13:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T13:13:38Z.


Verdict: confirm. Both deadlocks are fixed, and I found no new deadlock that main doesn't also have.

I re-ran 850b807cf merged onto main 44e3389c8 (clean merge) on Windows 11 with WinLibs GCC 15.2.0 and on Ubuntu 24.04 (WSL2) with GCC 13.3, both Release.

  • ctest -R "^ngs2_": 13 of 13 pass, 3 runs out of 3 on each OS. Neither build log has warnings.
  • Negative control on both OSes: with the five src files set back to main, ngs2_render and ngs2_core_render fail at line 946 (answeredWhileLocked). With them set back to 0fdc0e22b, both fail at line 1072 (TestCallbacksLockEachOthersSystem, the 2 s limit) instead of hanging.

My probes from the earlier rounds, plus three new ones. Each case ran 3 times on each OS, with a 3 s limit per wait and a 20 s process timeout. Both OSes gave the same results.

Case main 0fdc0e22b 850b807cf
1. A callback locks its own rack and a second rack of the same system, and calls VoiceGetStateFlags and RackGetInfo returns 0 returns 0 returns 0
2. Two systems' callbacks lock each other's racks returns deadlock returns
3. T2 holds R2 (S2) and locks R1 (S1); T1's callback for S1 locks R2 returns deadlock returns
4. A callback waits (up to 500 ms) for another thread's RackLock on a rack of another system waits granted waits
5. A callback waits for another thread's VoiceGetStateFlags waits returns returns
6. Another thread's RackLock on the rack being rendered (my first case) waits waits waits
7. Two threads render two systems; each callback sleeps 200 ms (total wall time) 400–424 ms 200–228 ms 401–429 ms
  • Rows 4 and 7 show that renders and rack locks are serialized again, as on main. The description only claims that other NGS2 calls go ahead, and row 5 confirms they still do. The only thing given up is the overlap that 0fdc0e22b allowed, which neither the description nor main had.
  • Row 4 is the one way left for a callback to deadlock: it waits for another thread that needs any rack lock or a render. main has the same deadlock (the bounded wait times out on both). Nothing new, but the debt entry could name it.
  • Smaller difference from main: sceNgs2RackDestroy from thread B on a rack that thread A holds (locked twice) returns 0 right away and releases A's exclusion. A's next RackUnlock returns 0x804A8202, and later locks and renders go through. On main, B's destroy waited for the lock holder. Nothing hangs or crashes; I'm noting it only because the debt entry says destroy waits for a render, not for a lock.

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6291690298 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8238#issuecomment-6291690298). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-20T13:13:38Z. --- Verdict: confirm. Both deadlocks are fixed, and I found no new deadlock that `main` doesn't also have. I re-ran `850b807cf` merged onto `main` `44e3389c8` (clean merge) on Windows 11 with WinLibs GCC 15.2.0 and on Ubuntu 24.04 (WSL2) with GCC 13.3, both Release. - `ctest -R "^ngs2_"`: 13 of 13 pass, 3 runs out of 3 on each OS. Neither build log has warnings. - Negative control on both OSes: with the five `src` files set back to `main`, `ngs2_render` and `ngs2_core_render` fail at line 946 (`answeredWhileLocked`). With them set back to `0fdc0e22b`, both fail at line 1072 (`TestCallbacksLockEachOthersSystem`, the 2 s limit) instead of hanging. My probes from the earlier rounds, plus three new ones. Each case ran 3 times on each OS, with a 3 s limit per wait and a 20 s process timeout. Both OSes gave the same results. | Case | `main` | `0fdc0e22b` | `850b807cf` | |---|---|---|---| | 1. A callback locks its own rack and a second rack of the same system, and calls `VoiceGetStateFlags` and `RackGetInfo` | returns 0 | returns 0 | returns 0 | | 2. Two systems' callbacks lock each other's racks | returns | **deadlock** | returns | | 3. T2 holds R2 (S2) and locks R1 (S1); T1's callback for S1 locks R2 | returns | **deadlock** | returns | | 4. A callback waits (up to 500 ms) for another thread's `RackLock` on a rack of another system | waits | granted | waits | | 5. A callback waits for another thread's `VoiceGetStateFlags` | waits | returns | **returns** | | 6. Another thread's `RackLock` on the rack being rendered (my first case) | waits | waits | waits | | 7. Two threads render two systems; each callback sleeps 200 ms (total wall time) | 400–424 ms | 200–228 ms | 401–429 ms | - Rows 4 and 7 show that renders and rack locks are serialized again, as on `main`. The description only claims that other NGS2 calls go ahead, and row 5 confirms they still do. The only thing given up is the overlap that `0fdc0e22b` allowed, which neither the description nor `main` had. - Row 4 is the one way left for a callback to deadlock: it waits for another thread that needs any rack lock or a render. `main` has the same deadlock (the bounded wait times out on both). Nothing new, but the debt entry could name it. - Smaller difference from `main`: `sceNgs2RackDestroy` from thread B on a rack that thread A holds (locked twice) returns 0 right away and releases A's exclusion. A's next `RackUnlock` returns `0x804A8202`, and later locks and renders go through. On `main`, B's destroy waited for the lock holder. Nothing hangs or crashes; I'm noting it only because the debt entry says destroy waits for a render, not for a lock. AI-assisted: yes (Claude Code); results above were run on this machine.
milescoviello commented 2026-10-20 13:29:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T13:29:57Z.


Thanks @wachi35676, especially for the larger probe set. I fixed the destroy difference instead of documenting it. In 741e706b19, sceNgs2RackDestroy and sceNgs2SystemDestroy wait like renders and rack locks do. So a destroy from another thread now waits for whoever holds a rack lock, as on main, and a thread can still destroy a rack it holds itself. The render owner field that only the old destroy wait read is removed.

The new test, TestRackDestroyWaitsForLock, fails 3 of 3 on 850b807cf2's sources (the destroy returns at once) and passes on 741e706b19. All 13 ^ngs2 tests pass 3 of 3. PPSA36591 played 716 s (400 presses) at 60 FPS without freezing.

The debt entry now names your row 4: a callback that waits for another thread's rack lock or render never returns, the same as on main.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6292017231 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8238#issuecomment-6292017231). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-20T13:29:57Z. --- Thanks @wachi35676, especially for the larger probe set. I fixed the destroy difference instead of documenting it. In 741e706b19, `sceNgs2RackDestroy` and `sceNgs2SystemDestroy` wait like renders and rack locks do. So a destroy from another thread now waits for whoever holds a rack lock, as on `main`, and a thread can still destroy a rack it holds itself. The render owner field that only the old destroy wait read is removed. The new test, `TestRackDestroyWaitsForLock`, fails 3 of 3 on 850b807cf2's sources (the destroy returns at once) and passes on 741e706b19. All 13 `^ngs2` tests pass 3 of 3. PPSA36591 played 716 s (400 presses) at 60 FPS without freezing. The debt entry now names your row 4: a callback that waits for another thread's rack lock or render never returns, the same as on `main`. AI-assisted: yes (Claude Code)
wachi35676 commented 2026-10-20 14:07:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T14:07:48Z.


Verdict: confirm. 741e706b1 brings back main's destroy wait, and a thread that destroys a rack it holds itself no longer leaves other threads stuck, as it does on main.

741e706b1 merged cleanly onto main on Windows 11 with WinLibs GCC 15.2.0 (19fcab22e) and on Ubuntu 24.04 (WSL2) with GCC 13.3 (d28cb2a66). Both builds are Release, with no warnings.

  • ctest -R "^ngs2_": 13 of 13 pass, 3 runs out of 3 on each OS.
  • Negative control on both OSes: with the src files set back to 850b807cf, ngs2_render and ngs2_core_render fail at line 1124 (TestRackDestroyWaitsForLock). With them set back to main, they fail at line 946.

Probes, 3 runs per case on each OS, each with a 20 s process timeout. Both OSes gave the same results:

Case main 850b807cf 741e706b1
Rows 1–7 from my last comment as before as before same as 850b807cf (render and rack-lock exclusion kept, 2×200 ms callbacks take 400–435 ms)
B calls RackDestroy(R1) while A holds R1 twice and releases it after 300 + 100 ms waits 399–421 ms; A's unlocks return 0 returns at once waits 399–421 ms; A's unlocks return 0
Same with SystemDestroy(S1) waits returns at once waits
B destroys a rack or system while A renders it (callback sleeps 200 ms) waits for the render (123–149 ms) same same
T locks R1 twice, then calls RackDestroy(R1) itself returns 0, but another thread's RackLock on a rack of another system then never returns (3 of 3) returns 0; other threads go ahead returns 0; RackUnlock(R1) then returns 0x804A8202; another thread's RackLock and a render of S1 return in 1–31 ms

A voice callback that destroys its own rack, its own system, or another rack of its system does not deadlock. The destroy returns 0 at once, because the render belongs to the same thread. The process then crashes with corrupted double-linked list or SIGSEGV on Linux, or exit 127/139 on Windows, 3 runs out of 3. It does the same on main and on 850b807cf, because the render keeps using the destroyed rack. So this PR doesn't cause it. It might be worth a debt line or an unsupported-state throw at some point.

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6292795752 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8238#issuecomment-6292795752). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-20T14:07:48Z. --- Verdict: confirm. `741e706b1` brings back `main`'s destroy wait, and a thread that destroys a rack it holds itself no longer leaves other threads stuck, as it does on `main`. `741e706b1` merged cleanly onto `main` on Windows 11 with WinLibs GCC 15.2.0 (`19fcab22e`) and on Ubuntu 24.04 (WSL2) with GCC 13.3 (`d28cb2a66`). Both builds are Release, with no warnings. - `ctest -R "^ngs2_"`: 13 of 13 pass, 3 runs out of 3 on each OS. - Negative control on both OSes: with the `src` files set back to `850b807cf`, `ngs2_render` and `ngs2_core_render` fail at line 1124 (`TestRackDestroyWaitsForLock`). With them set back to `main`, they fail at line 946. Probes, 3 runs per case on each OS, each with a 20 s process timeout. Both OSes gave the same results: | Case | `main` | `850b807cf` | `741e706b1` | |---|---|---|---| | Rows 1–7 from my last comment | as before | as before | same as `850b807cf` (render and rack-lock exclusion kept, 2×200 ms callbacks take 400–435 ms) | | B calls `RackDestroy(R1)` while A holds R1 twice and releases it after 300 + 100 ms | waits 399–421 ms; A's unlocks return 0 | returns at once | waits 399–421 ms; A's unlocks return 0 | | Same with `SystemDestroy(S1)` | waits | returns at once | waits | | B destroys a rack or system while A renders it (callback sleeps 200 ms) | waits for the render (123–149 ms) | same | same | | T locks R1 twice, then calls `RackDestroy(R1)` itself | returns 0, but another thread's `RackLock` on a rack of another system then never returns (3 of 3) | returns 0; other threads go ahead | returns 0; `RackUnlock(R1)` then returns `0x804A8202`; another thread's `RackLock` and a render of S1 return in 1–31 ms | A voice callback that destroys its own rack, its own system, or another rack of its system does not deadlock. The destroy returns 0 at once, because the render belongs to the same thread. The process then crashes with `corrupted double-linked list` or `SIGSEGV` on Linux, or exit 127/139 on Windows, 3 runs out of 3. It does the same on `main` and on `850b807cf`, because the render keeps using the destroyed rack. So this PR doesn't cause it. It might be worth a debt line or an unsupported-state throw at some point. AI-assisted: yes (Claude Code); results above were run on this machine.
milescoviello commented 2026-10-20 14:08:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T14:08:25Z.


Thanks @wachi35676, that's a thorough check. I added it to the description.

You're right that the callback that destroys its own rack or system is a separate, older bug. The render goes on using the freed rack, on main too. I'll open a small follow-up PR that makes a destroy from inside its own system's render throw, with your case as the test, rather than mixing it into this one.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6292808693 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8238#issuecomment-6292808693). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-20T14:08:25Z. --- Thanks @wachi35676, that's a thorough check. I added it to the description. You're right that the callback that destroys its own rack or system is a separate, older bug. The render goes on using the freed rack, on `main` too. I'll open a small follow-up PR that makes a destroy from inside its own system's render throw, with your case as the test, rather than mixing it into this one. AI-assisted: yes (Claude Code)
milescoviello commented 2026-10-20 14:26:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T14:26:04Z.


@wachi35676 the follow-up for your callback-destroy case is #8513 (stacked on this PR).

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6293178697 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8238#issuecomment-6293178697). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-20T14:26:04Z. --- @wachi35676 the follow-up for your callback-destroy case is #8513 (stacked on this PR). AI-assisted: yes (Claude Code)
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!8238
No description provided.