fix(libkernel): keep sceKernelRaiseException pending on a suspended thread on Windows #12963

Merged
JJenkx merged 1 commit from upstream-pr/12963 into main 2026-10-30 05:32:06 +00:00
JJenkx commented 2026-10-29 17:40:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-29T17:40:45Z.


What

On Windows, sceKernelRaiseException throws (the target thread is still suspended at creation) for a thread created suspended and not resumed yet. FreeBSD 11.4 libthr starts such a thread with every signal but SIGTRAP blocked and restores its mask only in thread_start once it is resumed, so the signal stays pending and is delivered on that thread before its start routine. That is what Linux and macOS already do here, through the blocked host mask, and what DefersRaiseUntilResume in guest_pthread_create_suspended checks on those hosts.

This makes Windows do the same:

  • sceKernelRaiseException: when the target is still suspended, it compare-exchanges the thread's pendingException (declared in PthreadPrivate but unused so far) from 0 to the signal and returns 0. If a raise is already pending, the new one merges into it, as for a standard signal. If the value is -1, the thread has already left its creation wait, and the raise is delivered as for any running thread.
  • RunThread: right after suspended.wait, the thread exchanges pendingException for -1. If a signal was pending, it delivers it to itself through the existing same-thread path of RaiseOn (RtlCaptureContext + Deliver). The handler is looked up at delivery, and a handler removed in between throws, as the POSIX Deliver aborts in that case.
  • scePthreadCreate resets pendingException to 0 with suspended.

A raise racing a resume is safe because both sides do a read-modify-write on the same atomic, so they are totally ordered. Either the thread takes the pending raise, or the raiser sees -1 and redirects the now running thread. A plain flag plus a re-check of suspended would not be safe: suspended is stored with release only, so that version could lose a raise.

The test now runs DefersRaiseUntilResume on every host instead of the Windows-only RefusesRaiseWhileSuspended. It also raises twice while the thread is suspended and still expects exactly one handler call, on that thread, after resume. The sceKernelRaiseException entry in docs/dev/debt/libkernel.md now describes the pending behaviour, and the console behaviour stays marked unverified.

Tested

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, no Vulkan driver visible, on main bb04a4f1ea.

  • guest_pthread_create_suspended passes 10 of 10 (ctest --repeat until-fail:10), and 10 of 10 again after the final rebuild.
  • Negative control: with Exception.cpp, Thread.cpp and Pthread.hpp from main and this test, it fails with sceKernelRaiseException: the target thread is still suspended at creation.
  • Related tests: ctest -R 'raise|exception|pthread|thread|signal|suspend' -j8 --repeat until-fail:3 covers 149 tests. 148 pass. guest_raise_exception failed once under that parallel load, so I ran it alone 8 times on each side:
build passes failure
main bb04a4f1ea 7 of 8 run 3: address waking round 2: the handler did not run within 5 s
this PR 7 of 8 run 6: address waking round 5: the handler did not run within 5 s

Both sides show the same pre-existing flake (#11813), at the same rate.

  • python tools/check_conventions.py --base origin/main: all checks passed.
  • Not run here: Linux and macOS. Their code is unchanged. Only the test changed for them: the second raise while suspended, where a blocked standard signal is merged and delivered once.

Future integration constraint — 2026-10-30

Recorded the review’s integration constraint: future park-loop replacements from #12130 or #12642 must retain DeliverPendingException_nid_no_patch(self) after the wait. Their new waiting mechanism must not bypass pending callback delivery. This is a preservation requirement for future composition, not a claim that those branches were merged or tested together, and it does not create an implementation prerequisite for this standalone change. The current source is unchanged.

AI-assisted: yes (Codex).

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 (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5072801662 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12963). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-29T17:40:45Z. --- ### What On Windows, `sceKernelRaiseException` throws (`the target thread is still suspended at creation`) for a thread created suspended and not resumed yet. FreeBSD 11.4 libthr starts such a thread with every signal but `SIGTRAP` blocked and restores its mask only in `thread_start` once it is resumed, so the signal stays pending and is delivered on that thread before its start routine. That is what Linux and macOS already do here, through the blocked host mask, and what `DefersRaiseUntilResume` in `guest_pthread_create_suspended` checks on those hosts. This makes Windows do the same: - `sceKernelRaiseException`: when the target is still `suspended`, it compare-exchanges the thread's `pendingException` (declared in `PthreadPrivate` but unused so far) from `0` to the signal and returns 0. If a raise is already pending, the new one merges into it, as for a standard signal. If the value is `-1`, the thread has already left its creation wait, and the raise is delivered as for any running thread. - `RunThread`: right after `suspended.wait`, the thread exchanges `pendingException` for `-1`. If a signal was pending, it delivers it to itself through the existing same-thread path of `RaiseOn` (`RtlCaptureContext` + `Deliver`). The handler is looked up at delivery, and a handler removed in between throws, as the POSIX `Deliver` aborts in that case. - `scePthreadCreate` resets `pendingException` to 0 with `suspended`. A raise racing a resume is safe because both sides do a read-modify-write on the same atomic, so they are totally ordered. Either the thread takes the pending raise, or the raiser sees `-1` and redirects the now running thread. A plain flag plus a re-check of `suspended` would not be safe: `suspended` is stored with release only, so that version could lose a raise. The test now runs `DefersRaiseUntilResume` on every host instead of the Windows-only `RefusesRaiseWhileSuspended`. It also raises twice while the thread is suspended and still expects exactly one handler call, on that thread, after resume. The `sceKernelRaiseException` entry in `docs/dev/debt/libkernel.md` now describes the pending behaviour, and the console behaviour stays marked unverified. ### Tested Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, no Vulkan driver visible, on `main` `bb04a4f1ea`. - `guest_pthread_create_suspended` passes 10 of 10 (`ctest --repeat until-fail:10`), and 10 of 10 again after the final rebuild. - Negative control: with `Exception.cpp`, `Thread.cpp` and `Pthread.hpp` from `main` and this test, it fails with `sceKernelRaiseException: the target thread is still suspended at creation`. - Related tests: `ctest -R 'raise|exception|pthread|thread|signal|suspend' -j8 --repeat until-fail:3` covers 149 tests. 148 pass. `guest_raise_exception` failed once under that parallel load, so I ran it alone 8 times on each side: | build | passes | failure | |---|---|---| | `main` `bb04a4f1ea` | 7 of 8 | run 3: `address waking round 2: the handler did not run within 5 s` | | this PR | 7 of 8 | run 6: `address waking round 5: the handler did not run within 5 s` | Both sides show the same pre-existing flake (#11813), at the same rate. - `python tools/check_conventions.py --base origin/main`: all checks passed. - Not run here: Linux and macOS. Their code is unchanged. Only the test changed for them: the second raise while suspended, where a blocked standard signal is merged and delivered once. <!-- codex-maintenance-20261030-repeat-scope-12963 --> **Future integration constraint — 2026-10-30** Recorded the [review’s integration constraint](https://github.com/boykopovar/AnyPS5/pull/12963#pullrequestreview-5703321630): future park-loop replacements from #12130 or #12642 must retain `DeliverPendingException_nid_no_patch(self)` after the wait. Their new waiting mechanism must not bypass pending callback delivery. This is a preservation requirement for future composition, not a claim that those branches were merged or tested together, and it does not create an implementation prerequisite for this standalone change. The current source is unchanged. AI-assisted: yes (Codex). ### 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 (Claude Code)
rooklz commented 2026-10-29 18:55:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T18:55:06Z.

Review: APPROVED


I checked this on a macOS host and did not build or run the Windows target. The PR's windows check passes.

Gates

  • git merge-tree against bb04a4f1ea is clean.
  • git diff --check is clean.
  • check_conventions --base origin/main passes.
  • x86_64-w64-mingw32-g++ 16.2 -std=c++20 -fsyntax-only reports 0 errors for Exception.cpp, Thread.cpp and GuestPthreadCreateSuspended.cpp.

Ordering. I copied the raise/resume protocol into a standalone harness: the suspended load, then compare_exchange_strong(0 → signum) || pending > 0, against a thread that waits and then calls exchange(-1). To widen the race window I added a spin between the suspended load and the CAS. Results on macOS arm64:

variant result
this PR, -O2 50,000 raise-vs-resume races, 0 lost
this PR, ASan+UBSan 20,000 races, 0 lost
this PR, TSan 5,000 races, no reports
thread exchanges 0 instead of -1 471–540 / 50,000 raises lost
|| pending > 0 dropped 1000 / 1000 sequential double raises run the handler on the suspended thread
  • The first mutant confirms the PR body's argument for the -1 sentinel. The test can't catch it because its raises are sequential. That seems acceptable to me, since catching it needs a timing window.
  • Your new second-raise row kills the second mutant.
  • If two raises race a resume, they sometimes give two deliveries: the first one merges, and the second one sees -1 and redirects. That matches POSIX, where a signal that arrives after the mask is restored is a new delivery.

Notes (non-blocking)

  • p->pendingException.store(0, ...) in scePthreadCreate is redundant. p is a fresh make_unique<PthreadPrivate>, and the member defaults to {0}. It's harmless, so drop it only if you want.
  • Merge order: #12130 and #12642 replace the self->suspended.wait(true, ...) line in RunThread with their own park loop. pr_overlap reports Thread.cpp L803-805. Whichever of those lands second needs to keep DeliverPendingException_nid_no_patch(self) after the wait, or Windows goes back to losing the pending raise. #12135 touches the adjacent debt lines L207-208.

Approving.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5703321630 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12963#pullrequestreview-5703321630). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-29T18:55:06Z. **Review:** APPROVED --- I checked this on a macOS host and did not build or run the Windows target. The PR's `windows` check passes. **Gates** - `git merge-tree` against `bb04a4f1ea` is clean. - `git diff --check` is clean. - `check_conventions --base origin/main` passes. - `x86_64-w64-mingw32-g++` 16.2 `-std=c++20 -fsyntax-only` reports 0 errors for `Exception.cpp`, `Thread.cpp` and `GuestPthreadCreateSuspended.cpp`. **Ordering.** I copied the raise/resume protocol into a standalone harness: the `suspended` load, then `compare_exchange_strong(0 → signum) || pending > 0`, against a thread that waits and then calls `exchange(-1)`. To widen the race window I added a spin between the `suspended` load and the CAS. Results on macOS arm64: | variant | result | |---|---| | this PR, -O2 | 50,000 raise-vs-resume races, 0 lost | | this PR, ASan+UBSan | 20,000 races, 0 lost | | this PR, TSan | 5,000 races, no reports | | thread exchanges `0` instead of `-1` | 471–540 / 50,000 raises lost | | `\|\| pending > 0` dropped | 1000 / 1000 sequential double raises run the handler on the suspended thread | - The first mutant confirms the PR body's argument for the `-1` sentinel. The test can't catch it because its raises are sequential. That seems acceptable to me, since catching it needs a timing window. - Your new second-raise row kills the second mutant. - If two raises race a resume, they sometimes give two deliveries: the first one merges, and the second one sees `-1` and redirects. That matches POSIX, where a signal that arrives after the mask is restored is a new delivery. **Notes (non-blocking)** - `p->pendingException.store(0, ...)` in `scePthreadCreate` is redundant. `p` is a fresh `make_unique<PthreadPrivate>`, and the member defaults to `{0}`. It's harmless, so drop it only if you want. - Merge order: #12130 and #12642 replace the `self->suspended.wait(true, ...)` line in `RunThread` with their own park loop. `pr_overlap` reports Thread.cpp L803-805. Whichever of those lands second needs to keep `DeliverPendingException_nid_no_patch(self)` after the wait, or Windows goes back to losing the pending raise. #12135 touches the adjacent debt lines L207-208. Approving.
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!12963
No description provided.