fix(libkernel): defer raises while a thread holds the address-wait lock on Windows #5756

Merged
oneandonlydean merged 1 commit from upstream-pr/5756 into main 2026-10-14 18:01:28 +00:00
oneandonlydean commented 2026-10-14 09:35:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T09:35:29Z.


What

Fixes #1488. On Windows, sceKernelRaiseException redirects a thread wherever it was stopped. A thread stopped inside sceKernelSyncOnAddressWake, sceKernelSyncOnAddressWait or _umtx_op while it held the address-wait lock ran the handler with the lock held, so a handler that waits, as a stop-the-world GC handler does, blocked every other thread that waits on or wakes an address. After #1562, LeandroLP23's reproducer in #1488 still blocked 35 of 300 rounds, all inside sceKernelSyncOnAddressWake.

The address-wait lock is now taken under the DeliveryDeferral that the guest mutex park queues already use (#5429): while it is held the thread's wait count is raised, so a raise queues an APC instead of redirecting the thread. The APC runs in the condition wait, where the lock is released, or after the lock is released on the way out of the call, before it returns to the guest. Nothing is delivered from inside the unlock or between the condition's enqueue and its wait (the points raised in the #4888 review). DeliveryDeferral and DeliverDeferred move from TimedWait.cpp into TimedWait.hpp for this.

Linux is unchanged: delivery there runs in the target's SIGUSR1 handler and the debt entry for it stays. Other emulator locks can take the same deferral when a case shows up. The Windows sceKernelRaiseException entry in docs/dev/debt/libkernel.md now names the deferred locks.

Tested

  • New Windows phases in guest_raise_exception: a guest thread loops on sceKernelSyncOnAddressWake, or on a 20 µs sceKernelSyncOnAddressWait; for 100 rounds each the test raises at it, holds the handler, and requires a wake on another address from another thread to return within 500 ms.
  • Windows: CI run 38547032212 (green, guest_raise_exception passes on Windows). Test without the fix: guest_raise_exception fails on Windows in run 38543474956 (completed failure).
  • Linux (CachyOS, GCC 16, Release): builds; full ctest except agc_driver*: 724 of 724 (6 VideoOut tests skipped, no display). The new phases are Windows only.
  • tools/check_conventions.py and tools/check_test_registration.py pass.
  • Not tested: titles.

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
  • Unblocks: none known
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4855865779 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5756). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T09:35:29Z. --- ### What Fixes #1488. On Windows, `sceKernelRaiseException` redirects a thread wherever it was stopped. A thread stopped inside `sceKernelSyncOnAddressWake`, `sceKernelSyncOnAddressWait` or `_umtx_op` while it held the address-wait lock ran the handler with the lock held, so a handler that waits, as a stop-the-world GC handler does, blocked every other thread that waits on or wakes an address. After #1562, LeandroLP23's reproducer in #1488 still blocked 35 of 300 rounds, all inside `sceKernelSyncOnAddressWake`. The address-wait lock is now taken under the `DeliveryDeferral` that the guest mutex park queues already use (#5429): while it is held the thread's wait count is raised, so a raise queues an APC instead of redirecting the thread. The APC runs in the condition wait, where the lock is released, or after the lock is released on the way out of the call, before it returns to the guest. Nothing is delivered from inside the unlock or between the condition's enqueue and its wait (the points raised in the #4888 review). `DeliveryDeferral` and `DeliverDeferred` move from `TimedWait.cpp` into `TimedWait.hpp` for this. Linux is unchanged: delivery there runs in the target's SIGUSR1 handler and the debt entry for it stays. Other emulator locks can take the same deferral when a case shows up. The Windows `sceKernelRaiseException` entry in `docs/dev/debt/libkernel.md` now names the deferred locks. ### Tested - New Windows phases in `guest_raise_exception`: a guest thread loops on `sceKernelSyncOnAddressWake`, or on a 20 µs `sceKernelSyncOnAddressWait`; for 100 rounds each the test raises at it, holds the handler, and requires a wake on another address from another thread to return within 500 ms. - Windows: CI run [38547032212](https://github.com/boykopovar/AnyPS5/actions/runs/38547032212) (green, `guest_raise_exception` passes on Windows). Test without the fix: `guest_raise_exception` fails on Windows in [run 38543474956](https://github.com/boykopovar/AnyPS5/actions/runs/38543474956) (completed failure). - Linux (CachyOS, GCC 16, Release): builds; full `ctest` except `agc_driver*`: 724 of 724 (6 VideoOut tests skipped, no display). The new phases are Windows only. - `tools/check_conventions.py` and `tools/check_test_registration.py` pass. - Not tested: titles. ### 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] Unblocks: none known - [x] GPUs tested: n/a - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-14 09:40:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T09:40:58Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#4630 core/libs/tests/GuestRaiseException.cpp L662
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6165448080 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5756#issuecomment-6165448080). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-14T09:40:58Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #4630 | `core/libs/tests/GuestRaiseException.cpp` [L662](https://github.com/boykopovar/AnyPS5/blob/0c4e4302d4eada6a9c6dac4a7fb1d5f9535c34b6/core/libs/tests/GuestRaiseException.cpp#L662) | | |
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!5756
No description provided.