fix(libSceHmdSetupDialog): keep a terminated dialog terminated during Open and Close #12931
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12931"
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 XairRrR on 2026-10-29T15:10:29Z.
What
sceHmdSetupDialogOpenandsceHmdSetupDialogCloseread the dialog status, checked it, and then storedFINISHEDin a separate step. If another thread calledsceHmdSetupDialogTerminatebetween the read and the store, Terminate returned 0 and set the status toNONE, and then Open or Close wroteFINISHEDover it. The library was left looking initialized after it had been terminated:sceHmdSetupDialogGetStatusreturnedFINISHED(3) instead ofNONE(0), and the nextsceHmdSetupDialogInitializefailed withALREADY_INITIALIZED(0x80B80004), so the dialog could not be used again.Open and Close now move the status to
FINISHEDwith a compare-exchange loop that repeats the checks against the value it replaces. A concurrent Terminate either happens first, so the call returnsNOT_INITIALIZED, or happens after, so the status stays atNONE. The single-threaded results are unchanged.guest_hmd_setup_dialoggains a race check: for 200000 rounds each of Open and Close, it initializes the dialog, runs the call while a second thread terminates it, and requires the status to beNONEafterwards. Both threads yield while they wait for each other, so the check also finishes quickly on a single CPU.Tested
Arch Linux 7.2.11-zen, GCC 16.2.1, Ninja, Release, Intel Core i9-14900KF.
guest_hmd_setup_dialog_testsandlibs.ctest --test-dir build -R '^guest_hmd_setup_dialog$': 1/1 passed (0.14 s). The test binary also passed 20 of 20 direct runs (about 0.1 s each) and 5 of 5 runs pinned to one CPU withtaskset -c 0(about 0.27 s each).core/libs/prx/libSceHmdSetupDialog/Export.cpprestored frommain, the test failed with1/1 Test #1985: guest_hmd_setup_dialog ...........Subprocess aborted***Exception: 0.08 sec(Require(sceHmdSetupDialogGetStatus() == kStatusNone)aborts in the race check). It failed 3 of 3 runs under ctest and 20 of 20 direct runs.python3 tools/check_conventions.py --base origin/main: all conventions checks passed.git diff --check origin/main: clean.python3 tools/pr_overlap.py --branch auto/fix-libscehmdsetupdialog-terminate-race: no overlap with open pull requests.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)Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T17:27:39Z.
Review: COMMENTED
Review of
dcf847e73e(macOS arm64, Apple M5, 10 cores, Apple clang)The fix is correct. Open and Close now re-check NONE/RUNNING/null against the value the compare-exchange replaces, so a concurrent Terminate either goes first (the call returns NOT_INITIALIZED) or goes after (the status stays NONE). Single-threaded results are unchanged: the first iteration does the same checks in the same order on the same loaded value.
Gates:
git diff --checkclean;git merge-treeagainstmainbb04a4f1eaclean; no other open PR touches libSceHmdSetupDialog.Standalone build: the PR's verbatim
Export.cppplus the verbatimGuestHmdSetupDialog.cpp.-O2: pass, 0.08 s.Negative control is weak on arm64 (non-blocking, but worth fixing). With
main'sExport.cpp, the race check fails only 1/40 runs at-O2and 2/20 under ASan. Restoring only Open (open_old) fails 1/40, and restoring only Close (close_old) fails 0/40. So on this machine the Close half of the fix is effectively untested.The cause: the main thread calls
finish()once, right afterround.store(i), and Open/Close usually complete before the terminator thread wakes fromyield(). The read→store window is rarely hit.Suggested change: keep calling
finish()until Terminate has run. This is deterministic, because at least one call is guaranteed to overlap or follow the Terminate.The PR head still takes 0.10 s at
-O2and 0.12 s under ASan. The loop still yields, so it stays safe on a single CPU. Even withkRaceRounds = 20000, all three mutants fail 40/40.Other mutants (with the PR test):
compare_exchange_weak→strong: equivalent.