fix(libSceNgs2): keep a rack lock and a voice callback from blocking other threads #8238
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8238"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-20T05:27:31Z.
What
sceNgs2RackLocktakes the library's internal recursive mutex (Ngs2Mutex) and keeps it untilsceNgs2RackUnlock. 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/sceNgs2RackUnlockand froze a few minutes into a run on Linux (RTX 5090 Laptop):futex_wait;pthread_mutex_lockundersceNgs2VoiceGetStateFlags;Ngs2Mutex, was inscePthreadMutexLockon one of the title's own locks.The existing
sceNgs2SystemLocktest already models the console: another thread'ssceNgs2VoiceGetStateFlagsandsceNgs2SystemGetInforeturn while the system is locked, and only rendering is held back.How
sceNgs2RackLockrecords an owner thread and a depth on the rack (nested, like the system lock), under the internal lock but without keeping it.sceNgs2RackUnlockdrops one level and wakes waiters at zero. A thread that doesn't hold the rack's lock getsSCE_NGS2_ERROR_INVALID_RACK_HANDLEinstead of unlocking a mutex it doesn't own.sceNgs2SystemRenderwaits, 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.std::lock_guardonNgs2Mutexis now anNgs2Lock, which counts the depth per thread, soNgs2Unlockedcan release the lock fully and take it back at the same depth.sceNgs2RackDestroyandsceNgs2SystemDestroywait while another thread renders the system (Ngs2System::renderer, which the render saves and restores so a nested render on the same thread is unaffected).Tested
Linux (Ubuntu 24.04, GCC 13.3), on
maina98590dec2.ngs2_render/ngs2_core_rendercases, both of which time out after 2 s instead of hanging:TestRackLockLeavesQueries: while the test holds a rack lock twice, another thread'ssceNgs2VoiceGetStateFlagsmust return. A render on a third thread must not finish until the second unlock, and a third unlock must fail. Withmain's NGS2 sources it fails atRequire(answeredWhileLocked).TestVoiceCallbackLeavesQueries: a block-end callback starts a thread that callssceNgs2VoiceGetStateFlags, and that call must return while the callback runs. Withmain's sources (and the rack test left out, so this case runs) it fails atRequire(callback.answered && callback.answeredInCallback).TestRackLockWaitsForRender(added in0fdc0e22b3after @wachi35676's report below): a block-end callback starts a thread that callssceNgs2RackLockon 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 theRack.cppline from0fdc0e22b3,ngs2_renderandngs2_core_renderfail at that check 3 of 3, because the other thread takes the rack mid-render. With it, they pass.TestCallbacksLockEachOthersSystemandTestRackLocksAcrossSystemsAndRender(added in850b807cf2after @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 of0fdc0e22b3,ngs2_renderandngs2_core_renderdeadlock on each case (run separately) 3 of 3. With850b807cf2, they pass.TestRackDestroyWaitsForLock(741e706b19, after @wachi35676's third report): while one thread holds a rack lock, another thread'ssceNgs2RackDestroyof that rack must not return until the lock is released. With the sources of850b807cf2,ngs2_renderandngs2_core_renderfail at that check 3 of 3, because the destroy returns at once. With741e706b19, they pass.^ngs2tests pass 3 of 3 on741e706b19.741e706b1merged ontomain(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 onmain. A thread that destroys a rack it holds itself no longer leaves other threads stuck, which it does onmain.850b807cfmerged ontomain44e3389c8(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 onmain, and other calls still go ahead.a5b068543merged ontomain02fb1ce53: the 13^ngs2_tests pass 3 of 3, and withmain's sourcesngs2_renderandngs2_core_renderfail atansweredWhileLocked. On0fdc0e22b(Windows 11 and Ubuntu 24.04 WSL2): 13 of 13 pass 3 of 3 on each, the new rack-lock test fails withRack.cppset back, and a callback's ownRackLock,VoiceGetStateFlags,RackGetInfoand second-rack lock return on its render thread.0fdc0e22b3,850b807cf2and741e706b19, 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, butmaindidn'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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
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.
main)core/libs/prx/libSceNgs2.native/src/Rack.cppL175-176, L183-184Some 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.
core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hppcore/libs/prx/libSceNgs2.native/src/Rack.cppcore/libs/prx/libSceNgs2.native/src/Render.cppcore/libs/prx/libSceNgs2.native/src/System.cppcore/libs/prx/libSceNgs2.native/src/Voice.cppcore/libs/prx/libSceNgs2.native/src/Ngs2Internal.hppcore/libs/prx/libSceNgs2.native/src/Render.cppcore/libs/prx/libSceNgs2.native/src/Voice.cppdocs/dev/debt/libSceNgs2.native.mdImported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T11:29:29Z.
Windows run of
a5b068543merged ontomain02fb1ce53(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.libSceNgs2.native/srcfiles reverted tomain,ngs2_renderandngs2_core_renderfail withNGS2 check failed at line 944(answeredWhileLocked). The other 11 still pass.One gap in the rule "
sceNgs2SystemRenderwaits 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 takesceNgs2RackLockon 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 onmain:Grain / 2frames, with a BLOCK_END callback.sceNgs2RackLock(R), records whether the render has returned yet, and keeps the lock. The callback waits up to 500 ms for B, then returns.sceNgs2SystemRenderreturns, the main thread lets B unlock.mainSo 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
RackLockwait for a render in progress, asRackDestroydoes withNgs2RenderingElsewhere, would close the gap. But it could bring back the deadlock this PR removes if the callback waits on a lock that theRackLockcaller 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.
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,sceNgs2RackLockalso waits while another thread renders the rack's system. Before this PR, the rack lock was the internal mutex, so aRackLockfrom another thread always waited for the whole render, callbacks included. So the wait adds no deadlock thatmaindidn't already have, andVoiceGetStateFlagsand the other calls still go through during a rack lock, which is what PPSA36591 needed.Your case is now
TestRackLockWaitsForRender. Without theRack.cppline,ngs2_renderandngs2_core_renderfail at it 3 of 3. With it, all 13^ngs2tests 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)
Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T12:11:05Z.
Re-ran
0fdc0e22bmerged ontomain02fb1ce53(clean merge; also merges cleanly ontod50ec76da, 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.srcfiles reverted tomain,ngs2_renderandngs2_core_renderfail on both OSes. With onlyRack.cppset back toa5b068543, the newTestRackLockWaitsForRenderfails atRequire(lockedAfterRender && !callback.lockedDuringCallback). So the new test covers the fix.sceNgs2RackLockfrom inside the callback, now waits for the render to finish.Self-deadlock check: a BLOCK_END callback calls these on its own render thread:
sceNgs2RackLockon its own rack,sceNgs2VoiceGetStateFlags,sceNgs2RackGetInfo, andsceNgs2RackLockplus 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 onmain, where the recursive internal mutex allowed it.Ngs2RenderingElsewhereskips 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.
maina5b0685430fdc0e22bRackLock(R2)(S2), then callsRackLock(R1)(S1). Meanwhile T1 renders S1 and its callback callsRackLock(R2).In case 2, T2 waits for T1's render of S1 to end, and T1's callback waits for T2 to release R2. On
mainthe 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.
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
850b807cf2instead 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.mainthe renders also ran one after the other.Both of your probes are now tests,
TestCallbacksLockEachOthersSystemandTestRackLocksAcrossSystemsAndRender. 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 of0fdc0e22b3,ngs2_renderandngs2_core_renderdeadlock on each case 3 of 3; I ran them separately, since the first one aborts the test. With850b807cf2, all 13^ngs2tests 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)
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
maindoesn't also have.I re-ran
850b807cfmerged ontomain44e3389c8(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.srcfiles set back tomain,ngs2_renderandngs2_core_renderfail at line 946 (answeredWhileLocked). With them set back to0fdc0e22b, 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.
main0fdc0e22b850b807cfVoiceGetStateFlagsandRackGetInfoRackLockon a rack of another systemVoiceGetStateFlagsRackLockon the rack being rendered (my first case)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 that0fdc0e22ballowed, which neither the description normainhad.mainhas the same deadlock (the bounded wait times out on both). Nothing new, but the debt entry could name it.main:sceNgs2RackDestroyfrom thread B on a rack that thread A holds (locked twice) returns 0 right away and releases A's exclusion. A's nextRackUnlockreturns0x804A8202, and later locks and renders go through. Onmain, 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.
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,sceNgs2RackDestroyandsceNgs2SystemDestroywait like renders and rack locks do. So a destroy from another thread now waits for whoever holds a rack lock, as onmain, 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 on741e706b19. All 13^ngs2tests 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)
Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T14:07:48Z.
Verdict: confirm.
741e706b1brings backmain's destroy wait, and a thread that destroys a rack it holds itself no longer leaves other threads stuck, as it does onmain.741e706b1merged cleanly ontomainon 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.srcfiles set back to850b807cf,ngs2_renderandngs2_core_renderfail at line 1124 (TestRackDestroyWaitsForLock). With them set back tomain, 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:
main850b807cf741e706b1850b807cf(render and rack-lock exclusion kept, 2×200 ms callbacks take 400–435 ms)RackDestroy(R1)while A holds R1 twice and releases it after 300 + 100 msSystemDestroy(S1)RackDestroy(R1)itselfRackLockon a rack of another system then never returns (3 of 3)RackUnlock(R1)then returns0x804A8202; another thread'sRackLockand a render of S1 return in 1–31 msA 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 listorSIGSEGVon Linux, or exit 127/139 on Windows, 3 runs out of 3. It does the same onmainand on850b807cf, 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.
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
maintoo. 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)
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)