fix(libSceJson2): take the recovery mutex before the state mutex in terminate #16595
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/16595"
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 StepFPV on 2026-11-07T08:19:24Z.
What
sce::Json::Initializer::terminateheld the library's state mutex whileDestroy(*state.fallback)reachedEraseLegacyHandler, which locks the kernel recovery mutex, so its order was state → recovery. Every other path takes the recovery mutex first:CallbackScopeholds it across a title callback that may enter any API, andRegisterLegacyHandler(reached throughValue::setTypeMismatchHandlerbelow application SDK 6) holds it acrossAllocate, which locks the state mutex. A title that terminates the library on one thread while another thread registers a type-mismatch handler deadlocks: terminate waits for the recovery mutex under the state mutex, the registration waits for the state mutex under the recovery mutex.ReleaseLegacyHandlersalso freed the handler map without the recovery mutex whileCopyLegacyHandleron another thread could still read it.terminatenow locks the recovery mutex before the state mutex, the orderCallbackScopeuses, releases the handler map and the fallback value under it, unlocks it, and only then destroys the kernel mutexes.ReleaseInitializationis split intoReleaseRecoveryStateandReleaseKernelResources; its initialization-failure callers are unchanged and never hold the recovery mutex there, since it stays null until the resources are admitted.KernelMutexLockgainsUnlock. Termination at SDK 6 and above doesn't erase legacy handlers, so there the change only adds the lock aroundReleaseRecoveryState. Becauseterminatenow takes the recovery mutex,ReleaseKernelResourcesclearsstate.recoveryMutexbefore it hands the destroyed mutex to the allocator's free callback:guest_json_internal_initializerre-entersterminatefrom that callback, which would otherwise lock the destroyed mutex.Tested
Found by running the CPU tests under ThreadSanitizer on Linux (
-DANYPS5_ENABLE_TSAN=ON, Arch Linux in WSL2, GCC 16.1.1, RelWithDebInfo), which reports the cycle aslock-order-inversion (potential deadlock) … libSceJson2/Export.cpp:306 in ActiveAllocation.New
guest_json_terminate_lock_orderreproduces the deadlock deterministically with the mocked kernel mutex and allocator of the sibling test: a thread registers the first legacy handler and pauses in the allocator while it holds the recovery mutex; the main thread then callsterminate, and the registration resumes once terminate asks for the recovery mutex. Onmaincb44bbbdf0with this test added, the test deadlocks and its 5 s watchdog aborts, on Windows and under ThreadSanitizer on Linux, where it also prints the lock-order report above. With the change it passes 3 of 3 on Windows and 5 of 5 under ThreadSanitizer, both calls return 0, the handler map registered during termination is released, and there is no ThreadSanitizer output;guest_json_callback_threadsandguest_json_internal_initializeralso pass 5 of 5 under ThreadSanitizer with no output.ctest -R jsonpasses on both: Windows 11, WinLibs GCC 15.2.0 UCRT, Release, 224 of 224; Linux ThreadSanitizer build, 225 of 225.tools/check_conventions.pypasses.Not tested on a title. The reference's locking isn't observable, so the change follows the order the file already uses.
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