fix(libSceJson2): take the recovery mutex before the state mutex in terminate #16595

Merged
StepFPV merged 1 commit from upstream-pr/16595 into main 2026-11-09 08:35:34 +00:00
StepFPV commented 2026-11-07 08:19:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by StepFPV on 2026-11-07T08:19:24Z.


What

sce::Json::Initializer::terminate held the library's state mutex while Destroy(*state.fallback) reached EraseLegacyHandler, which locks the kernel recovery mutex, so its order was state → recovery. Every other path takes the recovery mutex first: CallbackScope holds it across a title callback that may enter any API, and RegisterLegacyHandler (reached through Value::setTypeMismatchHandler below application SDK 6) holds it across Allocate, 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. ReleaseLegacyHandlers also freed the handler map without the recovery mutex while CopyLegacyHandler on another thread could still read it.

terminate now locks the recovery mutex before the state mutex, the order CallbackScope uses, releases the handler map and the fallback value under it, unlocks it, and only then destroys the kernel mutexes. ReleaseInitialization is split into ReleaseRecoveryState and ReleaseKernelResources; its initialization-failure callers are unchanged and never hold the recovery mutex there, since it stays null until the resources are admitted. KernelMutexLock gains Unlock. Termination at SDK 6 and above doesn't erase legacy handlers, so there the change only adds the lock around ReleaseRecoveryState. Because terminate now takes the recovery mutex, ReleaseKernelResources clears state.recoveryMutex before it hands the destroyed mutex to the allocator's free callback: guest_json_internal_initializer re-enters terminate from 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 as lock-order-inversion (potential deadlock) … libSceJson2/Export.cpp:306 in ActiveAllocation.

New guest_json_terminate_lock_order reproduces 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 calls terminate, and the registration resumes once terminate asks for the recovery mutex. On main cb44bbbdf0 with 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_threads and guest_json_internal_initializer also pass 5 of 5 under ThreadSanitizer with no output.

ctest -R json passes on both: Windows 11, WinLibs GCC 15.2.0 UCRT, Release, 224 of 224; Linux ThreadSanitizer build, 225 of 225. tools/check_conventions.py passes.

Not tested on a title. The reference's locking isn't observable, so the change follows the order the file already uses.

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: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5199826704 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16595). Originally posted by [StepFPV](https://github.com/StepFPV) on 2026-11-07T08:19:24Z. --- ### What `sce::Json::Initializer::terminate` held the library's state mutex while `Destroy(*state.fallback)` reached `EraseLegacyHandler`, which locks the kernel recovery mutex, so its order was state → recovery. Every other path takes the recovery mutex first: `CallbackScope` holds it across a title callback that may enter any API, and `RegisterLegacyHandler` (reached through `Value::setTypeMismatchHandler` below application SDK 6) holds it across `Allocate`, 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. `ReleaseLegacyHandlers` also freed the handler map without the recovery mutex while `CopyLegacyHandler` on another thread could still read it. `terminate` now locks the recovery mutex before the state mutex, the order `CallbackScope` uses, releases the handler map and the fallback value under it, unlocks it, and only then destroys the kernel mutexes. `ReleaseInitialization` is split into `ReleaseRecoveryState` and `ReleaseKernelResources`; its initialization-failure callers are unchanged and never hold the recovery mutex there, since it stays null until the resources are admitted. `KernelMutexLock` gains `Unlock`. Termination at SDK 6 and above doesn't erase legacy handlers, so there the change only adds the lock around `ReleaseRecoveryState`. Because `terminate` now takes the recovery mutex, `ReleaseKernelResources` clears `state.recoveryMutex` before it hands the destroyed mutex to the allocator's free callback: `guest_json_internal_initializer` re-enters `terminate` from 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 as `lock-order-inversion (potential deadlock) … libSceJson2/Export.cpp:306 in ActiveAllocation`. New `guest_json_terminate_lock_order` reproduces 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 calls `terminate`, and the registration resumes once terminate asks for the recovery mutex. On `main` `cb44bbbdf0` with 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_threads` and `guest_json_internal_initializer` also pass 5 of 5 under ThreadSanitizer with no output. `ctest -R json` passes on both: Windows 11, WinLibs GCC 15.2.0 UCRT, Release, 224 of 224; Linux ThreadSanitizer build, 225 of 225. `tools/check_conventions.py` passes. Not tested on a title. The reference's locking isn't observable, so the change follows the order the file already uses. ### Checklist - [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: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/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!16595
No description provided.