fix(libkernel): let exception delivery interrupt mutex and rwlock waits #2051

Merged
esatturan merged 1 commit from upstream-pr/2051 into main 2026-10-11 09:31:40 +00:00
esatturan commented 2026-10-09 05:59:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by esatturan on 2026-10-09T05:59:49Z.


What

scePthreadMutexLock, scePthreadMutexTimedlock and the rwlock read and write locks, plain and timed, wait in 1 ms slices inside a TimedWait::InterruptibleScope. sceKernelRaiseException now reaches a thread blocked on a guest mutex or rwlock: the handler runs during the wait, and the wait goes on afterwards, as a FreeBSD thread blocked in pthread_mutex_lock runs a signal handler and keeps waiting.

Unity's GC in PPSA29141 stops the world by raising an exception on every thread. A thread blocked on a mutex never ran the handler, and the title deadlocked during level loads.

_umtx_op was split out in review: #1482 implements it.

Tested

Windows 11, i7-13650HX, RTX 4070 Laptop, MinGW-w64 GCC 15.2.

  • guest_raise_exception: new rounds deliver an exception to a thread blocked in a mutex lock, a timed mutex lock and an rwlock write lock. On main the test blocks at the first round and times out; here it passes.
  • Full ctest on current main (c6e3aa76) merged with this branch, the other LEGO Party! PRs and #1482: 547/547 pass. On Windows, main's libs step currently fails with duplicate patched export name: LibwuIonIBw: #1714 added APS5_EXPORTs for sceVideoOutAddVrrActiveStatusEvent and sceVideoOutAddVrrStatusFlagsPrivilege, whose names the NID table already maps. Those two lines were removed locally for the run.
  • PPSA29141: no GC deadlock during level loads. Title runs were on the base these PRs had before review. PPSA29141 was converted with #846's --module-dir for its plugins, and #846 no longer merges with today's main, so the title was not converted and run again on it.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4796621474 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2051). Originally posted by [esatturan](https://github.com/esatturan) on 2026-10-09T05:59:49Z. --- ### What `scePthreadMutexLock`, `scePthreadMutexTimedlock` and the rwlock read and write locks, plain and timed, wait in 1 ms slices inside a `TimedWait::InterruptibleScope`. `sceKernelRaiseException` now reaches a thread blocked on a guest mutex or rwlock: the handler runs during the wait, and the wait goes on afterwards, as a FreeBSD thread blocked in `pthread_mutex_lock` runs a signal handler and keeps waiting. Unity's GC in PPSA29141 stops the world by raising an exception on every thread. A thread blocked on a mutex never ran the handler, and the title deadlocked during level loads. `_umtx_op` was split out in review: #1482 implements it. ### Tested Windows 11, i7-13650HX, RTX 4070 Laptop, MinGW-w64 GCC 15.2. - `guest_raise_exception`: new rounds deliver an exception to a thread blocked in a mutex lock, a timed mutex lock and an rwlock write lock. On `main` the test blocks at the first round and times out; here it passes. - Full `ctest` on current `main` (`c6e3aa76`) merged with this branch, the other LEGO Party! PRs and #1482: 547/547 pass. On Windows, `main`'s `libs` step currently fails with `duplicate patched export name: LibwuIonIBw`: #1714 added `APS5_EXPORT`s for `sceVideoOutAddVrrActiveStatusEvent` and `sceVideoOutAddVrrStatusFlagsPrivilege`, whose names the NID table already maps. Those two lines were removed locally for the run. - PPSA29141: no GC deadlock during level loads. Title runs were on the base these PRs had before review. PPSA29141 was converted with #846's `--module-dir` for its plugins, and #846 no longer merges with today's `main`, so the title was not converted and run again on it. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes (Claude Code)
oneandonlydean commented 2026-10-09 09:22:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T09:22:57Z.


Please split this in two: the interruptible mutex/rwlock waits (the PPSA29141 GC deadlock) in one PR, _umtx_op in another. #1482 already implements _umtx_op; if PPSA29141 stops at it on main, please give the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6078130665 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2051#issuecomment-6078130665). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T09:22:57Z. --- Please split this in two: the interruptible mutex/rwlock waits (the PPSA29141 GC deadlock) in one PR, `_umtx_op` in another. #1482 already implements `_umtx_op`; if PPSA29141 stops at it on main, please give the log line.
esatturan commented 2026-10-09 23:34:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by esatturan on 2026-10-09T23:34:51Z.


Split as asked. This PR now holds only the interruptible mutex and rwlock waits, rebased on current main. The new guest_raise_exception rounds block at the first round on main and time out after 30 s; they pass here.

_umtx_op: on current main, PPSA29141 stops at load with

FAIL: unresolved ELF import 04AjkP0jO9U from libkernel.prx

That is its only unresolved import. With #1482 merged into main locally, it loads, boots Unity and runs on to the stencil clear that #2504 handles. So #1482 covers what PPSA29141 needs, and I dropped my _umtx_op and its test. That also removes the __error_nid_postfix overlap with #2123.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6091075869 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2051#issuecomment-6091075869). Originally posted by [esatturan](https://github.com/esatturan) on 2026-10-09T23:34:51Z. --- Split as asked. This PR now holds only the interruptible mutex and rwlock waits, rebased on current `main`. The new `guest_raise_exception` rounds block at the first round on `main` and time out after 30 s; they pass here. `_umtx_op`: on current `main`, PPSA29141 stops at load with ``` FAIL: unresolved ELF import 04AjkP0jO9U from libkernel.prx ``` That is its only unresolved import. With #1482 merged into `main` locally, it loads, boots Unity and runs on to the stencil clear that #2504 handles. So #1482 covers what PPSA29141 needs, and I dropped my `_umtx_op` and its test. That also removes the `__error_nid_postfix` overlap with #2123.
oneandonlydean commented 2026-10-10 05:17:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-10T05:17:37Z.


Conflicts with main in Mutex.cpp and Rwlock.cpp (from #1562); please rebase.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094135675 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2051#issuecomment-6094135675). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-10T05:17:37Z. --- Conflicts with main in `Mutex.cpp` and `Rwlock.cpp` (from #1562); please rebase.
adriadam10 commented 2026-10-10 06:49:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:49:18Z.

Review: CHANGES_REQUESTED


Thanks for the split. #1562 is merged and already covers part of this, so the PR now conflicts in Mutex.cpp and Rwlock.cpp. Please rebase and keep only what main still lacks:

  • Mutex.cpp / Rwlock.cpp: main now polls with TimedWait::SleepNanos(1000000) under #ifdef _WIN32 in scePthreadMutexLock, scePthreadRwlockRdlock and scePthreadRwlockWrlock. Drop the duplicate; keep the polling Windows-only, since on Linux InterruptibleScope is a no-op and 1 ms polling instead of a blocking lock() only adds latency and CPU.
  • If the timed variants (scePthreadMutexTimedlock, scePthreadRwlockTimedrdlock/Timedwrlock) still can't be interrupted on main, make that the PR's only change and say so in the description.
  • Keep the new guest_raise_exception rounds (mutex, timed mutex, rwlock write lock). Please confirm they still block on current main and pass with the rebased change.
  • Update the PPSA29141 note in ### Tested to say whether the deadlock still reproduces on current main with #1562.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5478017761 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2051#pullrequestreview-5478017761). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T06:49:18Z. **Review:** CHANGES_REQUESTED --- Thanks for the split. #1562 is merged and already covers part of this, so the PR now conflicts in `Mutex.cpp` and `Rwlock.cpp`. Please rebase and keep only what main still lacks: - `Mutex.cpp` / `Rwlock.cpp`: main now polls with `TimedWait::SleepNanos(1000000)` under `#ifdef _WIN32` in `scePthreadMutexLock`, `scePthreadRwlockRdlock` and `scePthreadRwlockWrlock`. Drop the duplicate; keep the polling Windows-only, since on Linux `InterruptibleScope` is a no-op and 1 ms polling instead of a blocking `lock()` only adds latency and CPU. - If the timed variants (`scePthreadMutexTimedlock`, `scePthreadRwlockTimedrdlock`/`Timedwrlock`) still can't be interrupted on main, make that the PR's only change and say so in the description. - Keep the new `guest_raise_exception` rounds (mutex, timed mutex, rwlock write lock). Please confirm they still block on current `main` and pass with the rebased change. - Update the PPSA29141 note in `### Tested` to say whether the deadlock still reproduces on current `main` with #1562.
Mouhand-Kaddo commented 2026-10-10 10:09:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Mouhand-Kaddo on 2026-10-10T10:09:40Z.


Additional platform validation on standalone PR head e86107d3bb0637f93337035c7a699ffc68e4955a:

Linux: passed — Arch Linux in WSL2 (kernel 6.6.87.2), GCC 16.2.1 20260810; CMake 4.3.1 / Ninja.

Release / BUILD_TESTING=ON: built the selected test targets; 4 CTest cases, 0 failures, 0 CTest skips.

Tests: guest_kernel_sync, guest_cond_timedwait, guest_cond_recursive_mutex, guest_pthread_rwlock.

Reproduce after building the test targets: ctest --test-dir <build> --verbose --no-tests=error --timeout 30 -R "^guest_(raise_exception|pthread_rwlock|cond_timedwait|cond_recursive_mutex|kernel_sync|pthread_mutex)$".

guest_raise_exception is registered only on Windows at this head; Linux checks cover the portable mutex/rwlock/condition wait behavior, not the Windows exception-delivery rounds

Integration limitation: a local merge against main 948a526c2 has source conflicts; these results validate the standalone PR head, not a resolved merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6096427570 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2051#issuecomment-6096427570). Originally posted by [Mouhand-Kaddo](https://github.com/Mouhand-Kaddo) on 2026-10-10T10:09:40Z. --- <!-- anyps5-platform-validation:e86107d3bb0637f93337035c7a699ffc68e4955a --> Additional platform validation on standalone PR head `e86107d3bb0637f93337035c7a699ffc68e4955a`: **Linux: passed** — Arch Linux in WSL2 (kernel 6.6.87.2), GCC 16.2.1 20260810; CMake 4.3.1 / Ninja. Release / `BUILD_TESTING=ON`: built the selected test targets; 4 CTest cases, 0 failures, 0 CTest skips. Tests: `guest_kernel_sync`, `guest_cond_timedwait`, `guest_cond_recursive_mutex`, `guest_pthread_rwlock`. Reproduce after building the test targets: `ctest --test-dir <build> --verbose --no-tests=error --timeout 30 -R "^guest_(raise_exception|pthread_rwlock|cond_timedwait|cond_recursive_mutex|kernel_sync|pthread_mutex)$"`. guest_raise_exception is registered only on Windows at this head; Linux checks cover the portable mutex/rwlock/condition wait behavior, not the Windows exception-delivery rounds Integration limitation: a local merge against main `948a526c2` has source conflicts; these results validate the standalone PR head, not a resolved merge.
adriadam10 commented 2026-10-11 09:28:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T09:28:28Z.

Review: APPROVED


Rebased onto main and reduced to what #1562 does not cover, since the PR was conflicting and had no push after the review:

  • Dropped InterruptibleScope, the lock/rwlock polling helpers and the 1 ms polling in scePthreadMutexLock/Rdlock/Wrlock (already on main, _WIN32 only).
  • TimedWait::AcquireUntil (timed mutex, rwlock rdlock/wrlock): on Windows it now polls in 0.5 ms slices instead of one long blocking try_lock_for, so a pending exception is delivered during the wait. Linux is unchanged.
  • MutexOperations::Timedlock: Windows-only try_lock + SleepNanos loop up to the deadline.
  • Kept the new guest_raise_exception rounds (mutex, timed mutex, rwlock write lock).
    Checked: libkernel build and scoped ctest on Linux (guest_kernel_sync, guest_pthread_rwlock, guest_cond_* pass). guest_raise_exception is Windows-only, so it was not run here; @esatturan, if you can run it on Windows with this head it would confirm the rounds still block on main and pass now.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5482755985 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2051#pullrequestreview-5482755985). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T09:28:28Z. **Review:** APPROVED --- Rebased onto main and reduced to what #1562 does not cover, since the PR was conflicting and had no push after the review: - Dropped `InterruptibleScope`, the lock/rwlock polling helpers and the 1 ms polling in `scePthreadMutexLock`/`Rdlock`/`Wrlock` (already on main, `_WIN32` only). - `TimedWait::AcquireUntil` (timed mutex, rwlock rdlock/wrlock): on Windows it now polls in 0.5 ms slices instead of one long blocking `try_lock_for`, so a pending exception is delivered during the wait. Linux is unchanged. - `MutexOperations::Timedlock`: Windows-only try_lock + `SleepNanos` loop up to the deadline. - Kept the new `guest_raise_exception` rounds (mutex, timed mutex, rwlock write lock). Checked: libkernel build and scoped ctest on Linux (`guest_kernel_sync`, `guest_pthread_rwlock`, `guest_cond_*` pass). `guest_raise_exception` is Windows-only, so it was not run here; @esatturan, if you can run it on Windows with this head it would confirm the rounds still block on main and pass now.
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!2051
No description provided.