fix(libSceNgs2): let render skip a grain while another thread holds the system lock #7072
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7072"
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 mrfelek on 2026-10-17T10:17:05Z.
What
sceNgs2SystemLocktook the library's process-wideNgs2Mutex()and kept it untilsceNgs2SystemUnlock. So while a title held the system lock,sceNgs2SystemRenderand every other NGS2 call on any other thread waited for it.On the console, render does not wait. prosper's notes, re-derived from the PS5
libSceNgs2.nativemodule (found by @SandstromPL), say:Peace Walker (PPSA18216) deadlocks on this during its
dlctexloads:sceNgs2SystemRenderand eightsceNgs2VoiceGetStateFlagscalls.A core of a frozen run with render made non-blocking alone shows the audio thread blocked in
sceNgs2VoiceGetStateFlagsinstead (details in #5268).Changes:
sceNgs2SystemLockandsceNgs2SystemUnlockkeep a per-system owner thread and depth behind their own mutex and condition variable, and no longer holdNgs2Mutex()after they return.sceNgs2SystemRendertries the system lock.sceNgs2SystemLockfrom another thread waits for the grain.Ngs2Mutex()only for its own work, so none of them waits for the system lock.sceNgs2RackLockis unchanged.Tested
Linux (Ubuntu 26.10), GCC 15.3, Release, Ryzen 9 5950X.
ngs2_*tests pass.TestLockis rewritten. It checks:GetInfoandVoiceGetStateFlagsnot blocking while another thread holds the lock;SystemLockfrom another thread waiting until both holds are released;main'sSystem.cpp,TestLockfails at the check that the other thread finished (line 907). With this change it passed 11 of 11 repeats.main8856f180c+ #5915, #5882, #6014 and #6196, started through the launcher hand-over, X pressed every 2 s. The change was applied to that tree, where a silent grain clears whole buffers as that tree's render does. OnlylibSceNgs2.prxdiffers between the runs:libSceNgs2dlctexloadsmain's locksceNgs2VoiceGetStateFlags)[present]lines in the 240 s after the last load each, then it opens its music file; no NGS2 errors)The same freeze happens on Windows 11 with
main's lock (3 of 4 runs, RTX A2000 Laptop). This PR was not run on Windows.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)AI-assisted: this PR was written with the help of Claude Code and checked against the runs by us.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-17T10:26:35Z.
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.
core/libs/prx/libSceNgs2.native/src/Ngs2Internal.hppcore/libs/prx/libSceNgs2.native/src/Render.cppcore/libs/prx/libSceNgs2.native/src/System.cppcore/libs/prx/libSceNgs2.native/src/Ngs2Internal.hppcore/libs/prx/libSceNgs2.native/src/Render.cppdocs/dev/debt/libSceNgs2.native.mdcore/libs/prx/libSceNgs2.native/src/Ngs2Internal.hppdocs/dev/debt/libSceNgs2.native.mddocs/dev/debt/libSceNgs2.native.mdcore/libs/prx/libSceNgs2.native/src/Ngs2Internal.hppcore/libs/prx/libSceNgs2.native/src/System.cppdocs/dev/debt/libSceNgs2.native.mdImported from boykopovar/AnyPS5. Originally posted by SandstromPL on 2026-10-17T11:25:50Z.
@mrfelek thanks for picking this up, and for the core dump that showed the
sceNgs2VoiceGetStateFlagsblock.I built and tested the head (
40f450665) on Ubuntu 24.04, GCC 13.3, Release:ngs2_*tests pass, includingngs2_core_render.System.cpp, both render tests abort at line 907, as you said.The Peace Walker path looks right to me. A few points:
A stray Unlock during a render can drop another thread's hold. Render adds its hold to the same
owner/depthas the game, and bothsceNgs2SystemUnlockand Render'sReleasedecrement without checking the owner. A small test program reproduces it 10 times out of 10:Releasethen drops L's hold, so other threads render and lock while L thinks it holds the lock.Main doesn't do this. A fix: a separate
rendererfield inSystemLockState. Render sets it,Releaseclears it and callsnotify_all, and Lock also waits until it is empty (or is the calling thread). YourTestLockpasses unchanged with that."Unlock releases from any thread, as on the console" was my overstatement in #5268, sorry. prosper's notes only say Unlock ignores the unlock result. Their "need not be the one that locked" line explains prosper's own approximation, not the native module. On a non-owner unlock, FreeBSD libthr returns EPERM for every mutex type, and so does our own libkernel. So the console most likely answers 0 and keeps the lock. Two options:
if (owner == self) ReleaseSystemLock(...). Lines 929-934 would then expect silence until the owner unlocks.Owner-only shouldn't affect Peace Walker. Your render-drop runs still used main's Lock/Unlock, where an Unlock from another thread would block forever, and 2 of 3 kept presenting. Can you confirm Peace Walker locks and unlocks on the same thread?
One new deadlock.
sceNgs2RackLockstill holdsNgs2Mutexafter it returns. Suppose a thread holds it, or is inside an NGS2 callback, and callssceNgs2SystemLockwhile another thread holds the system lock. Then the holder's next NGS2 call blocks, and both threads hang. Main completes this; the PR deadlocks 10 times out of 10. It's unlikely in games, so one sentence in the debt entry should be enough.Debt line. Besides point 2, "follow prosper's notes" also covers things the notes don't give:
Could the line be split into what the notes say and what this library assumes?
Optional tests (happy to share snippets):
blocked.join(), so a regression fails with a line number instead of hanging.Last thing: your Peace Walker runs were on a port to
8856f180c, where the silent grain cleared whole buffers. One run on this exact head, ideally with points 1-2 in, would be great.AI-assisted: yes (Claude Code)