fix(libkernel): treat a raise target that is already terminating as exited on Windows #12069

Merged
dantdmdarealguy merged 1 commit from upstream-pr/12069 into main 2026-10-28 09:45:46 +00:00
dantdmdarealguy commented 2026-10-27 18:38:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by dantdmdarealguy on 2026-10-27T18:38:16Z.


What

guest_raise_exception fails intermittently on Windows CI on current main. Every failure aborts at the same place, Exception.cpp:700:

[exception] signal 0x1e: sceKernelRaiseException: cannot suspend the target thread
FATAL: abort() (SIGABRT) on thread 8624

It failed in 9 of the 193 Windows jobs of build.yml runs on main on 2026-10-27, including two runs on 2ee8e9751a (jobs 122272870002 and 122272290778). It failed in none of the 114 Windows jobs on 2026-10-26. The jobs that passed ran the same commits as the failing ones.

Since #10666 (1b9c5feb30), a raise whose target is in host code is handed to a background deliverer. The deliverer sweeps the target with TryDeliver until the handler is taken or the thread exits. Each sweep starts with SuspendThread. When that fails, TryDeliver only counts the target as gone if its handle is already signaled; otherwise it throws, and the deliverer aborts the process.

A thread that has begun to exit sits in a window where SuspendThread already fails with ERROR_ACCESS_DENIED (NtSuspendThread returns STATUS_THREAD_IS_TERMINATING, 0xC000004B), but its handle is not signaled yet and GetExitCodeThread still returns STILL_ACTIVE. A standalone probe on Windows 11 measured this: 20,000 threads, each suspended in a loop while it returned. 9,977 SuspendThread failures (all GetLastError 5) and 9,973 NtSuspendThread failures (all 0xC000004B) came before the handle was signaled. Every handle was signaled shortly afterwards.

guest_raise_exception hits the window when a thread it raised at exits while the deliverer still has that raise in its sweep list. For example, the handler was delivered at the thread's return breakpoint, and the test stopped and joined the thread before the next sweep retired the raise.

TryDeliver now suspends through NtSuspendThread. It takes STATUS_THREAD_IS_TERMINATING as Attempt::Exited, which is what it already did for a signaled handle: the deliverer drops the raise, and a direct sceKernelRaiseException returns ESRCH. Any other suspend failure still throws. DisarmReturnBreak already tolerated a failed suspend and is unchanged.

guest_raise_exiting_thread (new, Windows) reproduces the abort directly. In each of 100 rounds it starts 32 guest threads that block in a host WaitForSingleObject, raises SIGUSR1 at each (so every raise goes to the deliverer), then releases them all and joins them. Every raise must return 0 and the process must not abort. It takes about 0.4 s.

Closed #1802 (October 9) handled the same Windows behaviour in the code from before the deliverer, as part of a larger change. It was closed because no title hit the race and its branch merged other pull requests. The race now fails main's Windows CI, through a path #10666 added.

This is separate from #11813 and #12005. #11813 is the address waking round N: the handler did not run within 5 s abort of the same test, and #12005 speeds up delivery to busy host-code targets. Neither touches the suspend failure. This change neither depends on #12005 nor overlaps its lines.

Tested

Windows 11 Pro, Intel Core i9-14900K, 128 GB RAM, NVIDIA GeForce RTX 5080 (driver 32.0.16.1742), WinLibs GCC 15.2.0, Ninja, Release, BUILD_TESTING=ON, SDL_AUDIODRIVER=dummy, on main 2ee8e9751a.

  • CI evidence on main, Windows job of build.yml:

    Day Jobs with cannot suspend the target thread
    2026-10-26 0 of 114
    2026-10-27 9 of 193

    The 2026-10-27 failures were jobs 122146244109, 122146653804, 122170202472, 122201223723, 122219271843, 122272290778, 122272870002, 122294206442 and 122320201238.

  • guest_raise_exiting_thread, the same test executable run 100 times with each libkernel.prx:

    • main: 100/100 aborted, all in the deliverer (libkernel.prx+0x12ce05). 93 printed cannot suspend the target thread; the other 7 lost that line to interleaved output.
    • This change: 0/100.
    • With the change, ctest -R '^guest_raise_exiting_thread$' --repeat until-fail:100 passed alone (33.8 s) and as two concurrent loops (100/100 each).
  • guest_raise_exception, four concurrent loops of 10 runs each: main 1/40, this change 0/40. The one main failure was #11813's address waking timeout. The CI suspend abort didn't occur locally in these 40 runs, nor in 9 full-suite runs on main. guest_raise_exiting_thread is the deterministic reproduction.

  • ctest --output-on-failure --timeout 120 --parallel 2 -R "^guest_raise|^guest_cond_timedwait$|patched_libraries" with this change: 16/17 passed, and patched_libraries printed 368 patched libraries loaded, 0 failed. The failure was guest_raise_exception with address waking round 28: the handler did not run within 5 s, which is #11813 and not this change. The test passed in all 40 loop runs above.

  • cmake --build and --target libs pass with no warnings in the touched files. check_test_registration.py: 1470 test executable(s), 0 built and not run as recorded, none new. check_conventions.py --base origin/main: passed.

  • pr_overlap.py: no overlap. Four open PRs that change Exception.cpp couldn't be compared because they conflict with main (#4630, #5021, #7192, #8592); none of them changes the suspend lines. The commit merges cleanly onto main 14332cd8d0.

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, some AI assistance was used
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5039224001 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12069). Originally posted by [dantdmdarealguy](https://github.com/dantdmdarealguy) on 2026-10-27T18:38:16Z. --- ### What `guest_raise_exception` fails intermittently on Windows CI on current `main`. Every failure aborts at the same place, `Exception.cpp:700`: ```text [exception] signal 0x1e: sceKernelRaiseException: cannot suspend the target thread FATAL: abort() (SIGABRT) on thread 8624 ``` It failed in 9 of the 193 Windows jobs of `build.yml` runs on `main` on 2026-10-27, including two runs on `2ee8e9751a` (jobs 122272870002 and 122272290778). It failed in none of the 114 Windows jobs on 2026-10-26. The jobs that passed ran the same commits as the failing ones. Since #10666 (`1b9c5feb30`), a raise whose target is in host code is handed to a background deliverer. The deliverer sweeps the target with `TryDeliver` until the handler is taken or the thread exits. Each sweep starts with `SuspendThread`. When that fails, `TryDeliver` only counts the target as gone if its handle is already signaled; otherwise it throws, and the deliverer aborts the process. A thread that has begun to exit sits in a window where `SuspendThread` already fails with `ERROR_ACCESS_DENIED` (`NtSuspendThread` returns `STATUS_THREAD_IS_TERMINATING`, `0xC000004B`), but its handle is not signaled yet and `GetExitCodeThread` still returns `STILL_ACTIVE`. A standalone probe on Windows 11 measured this: 20,000 threads, each suspended in a loop while it returned. 9,977 `SuspendThread` failures (all `GetLastError` 5) and 9,973 `NtSuspendThread` failures (all `0xC000004B`) came before the handle was signaled. Every handle was signaled shortly afterwards. `guest_raise_exception` hits the window when a thread it raised at exits while the deliverer still has that raise in its sweep list. For example, the handler was delivered at the thread's return breakpoint, and the test stopped and joined the thread before the next sweep retired the raise. `TryDeliver` now suspends through `NtSuspendThread`. It takes `STATUS_THREAD_IS_TERMINATING` as `Attempt::Exited`, which is what it already did for a signaled handle: the deliverer drops the raise, and a direct `sceKernelRaiseException` returns `ESRCH`. Any other suspend failure still throws. `DisarmReturnBreak` already tolerated a failed suspend and is unchanged. `guest_raise_exiting_thread` (new, Windows) reproduces the abort directly. In each of 100 rounds it starts 32 guest threads that block in a host `WaitForSingleObject`, raises SIGUSR1 at each (so every raise goes to the deliverer), then releases them all and joins them. Every raise must return 0 and the process must not abort. It takes about 0.4 s. Closed #1802 (October 9) handled the same Windows behaviour in the code from before the deliverer, as part of a larger change. It was closed because no title hit the race and its branch merged other pull requests. The race now fails `main`'s Windows CI, through a path #10666 added. This is separate from #11813 and #12005. #11813 is the `address waking round N: the handler did not run within 5 s` abort of the same test, and #12005 speeds up delivery to busy host-code targets. Neither touches the suspend failure. This change neither depends on #12005 nor overlaps its lines. ### Tested Windows 11 Pro, Intel Core i9-14900K, 128 GB RAM, NVIDIA GeForce RTX 5080 (driver 32.0.16.1742), WinLibs GCC 15.2.0, Ninja, Release, `BUILD_TESTING=ON`, `SDL_AUDIODRIVER=dummy`, on `main` `2ee8e9751a`. - **CI evidence on `main`, Windows job of `build.yml`:** | Day | Jobs with `cannot suspend the target thread` | |---|---| | 2026-10-26 | 0 of 114 | | 2026-10-27 | 9 of 193 | The 2026-10-27 failures were jobs 122146244109, 122146653804, 122170202472, 122201223723, 122219271843, 122272290778, 122272870002, 122294206442 and 122320201238. - **`guest_raise_exiting_thread`**, the same test executable run 100 times with each `libkernel.prx`: - `main`: **100/100** aborted, all in the deliverer (`libkernel.prx+0x12ce05`). 93 printed `cannot suspend the target thread`; the other 7 lost that line to interleaved output. - This change: **0/100**. - With the change, `ctest -R '^guest_raise_exiting_thread$' --repeat until-fail:100` passed alone (33.8 s) and as two concurrent loops (100/100 each). - **`guest_raise_exception`**, four concurrent loops of 10 runs each: `main` 1/40, this change 0/40. The one `main` failure was #11813's `address waking` timeout. The CI suspend abort didn't occur locally in these 40 runs, nor in 9 full-suite runs on `main`. `guest_raise_exiting_thread` is the deterministic reproduction. - `ctest --output-on-failure --timeout 120 --parallel 2 -R "^guest_raise|^guest_cond_timedwait$|patched_libraries"` with this change: 16/17 passed, and `patched_libraries` printed `368 patched libraries loaded, 0 failed`. The failure was `guest_raise_exception` with `address waking round 28: the handler did not run within 5 s`, which is #11813 and not this change. The test passed in all 40 loop runs above. - `cmake --build` and `--target libs` pass with no warnings in the touched files. `check_test_registration.py`: `1470 test executable(s), 0 built and not run as recorded, none new`. `check_conventions.py --base origin/main`: passed. - `pr_overlap.py`: no overlap. Four open PRs that change `Exception.cpp` couldn't be compared because they conflict with `main` (#4630, #5021, #7192, #8592); none of them changes the suspend lines. The commit merges cleanly onto `main` `14332cd8d0`. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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, some AI assistance was used
MarcZX10 commented 2026-10-27 19:04:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T19:04:09Z.

Review: APPROVED


The status handling is consistent with NTSTATUS semantics: STATUS_THREAD_IS_TERMINATING is classified as an exited target, while other failed suspends still require a signaled handle or remain errors. The Windows stress test raises at 32 blocked guest threads over 100 release rounds; the reported 100/100 baseline aborts and 0/100 changed runs directly exercise the race, with additional repeated/concurrent runs reported. I checked the exact-head diff, current-main merge preview, check_conventions.py, and git diff --check.

The hosted Windows job is green but did not run this test: its changed-test selection, libs build, and CTest steps were skipped. This approval relies on the detailed Windows 11 build and repeated test results recorded by the author; I could not independently execute this Windows-only test in the Linux environment.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5672768763 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12069#pullrequestreview-5672768763). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T19:04:09Z. **Review:** APPROVED --- The status handling is consistent with NTSTATUS semantics: `STATUS_THREAD_IS_TERMINATING` is classified as an exited target, while other failed suspends still require a signaled handle or remain errors. The Windows stress test raises at 32 blocked guest threads over 100 release rounds; the reported 100/100 baseline aborts and 0/100 changed runs directly exercise the race, with additional repeated/concurrent runs reported. I checked the exact-head diff, current-main merge preview, `check_conventions.py`, and `git diff --check`. The hosted Windows job is green but did not run this test: its changed-test selection, `libs` build, and CTest steps were skipped. This approval relies on the detailed Windows 11 build and repeated test results recorded by the author; I could not independently execute this Windows-only test in the Linux environment.
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!12069
No description provided.