fix(libkernel): redirect a raise only once the target leaves host code #7652

Merged
billythemenace2 merged 2 commits from upstream-pr/7652 into main 2026-10-20 11:16:49 +00:00
billythemenace2 commented 2026-10-18 20:25:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by billythemenace2 on 2026-10-18T20:25:19Z.


Requested by billy

What

On Windows, sceKernelRaiseException suspends the target thread and redirects it to the guest handler wherever it stopped. The only exceptions are NtContinue and winpthreads. A target stopped inside host code, such as a C runtime call or a host library, can be holding a host lock there. If the handler then waits, as a collector's thread-stop handler does, the lock stays held, and every host thread that needs it stops too.

SONIC SUPERSTARS (PPSA06888) froze at frame 65 on current main because of this. Its IL2CPP "UDS" thread was redirected in the middle of libSceNpTrophy2's [trophy] ... stderr write (the log line breaks off mid-word) and parked in sceKernelWaitEventFlag. gdb showed it as the OwningThread of the ucrt stderr critical section, with recursion 2. The driver's ShaderDiskCache report and AsyncLog threads were blocked in RtlEnterCriticalSection on that lock.

  • InHostModule: the address is in a loaded module other than the executable or a *.guest.prx.
  • BlockedInWait: the address is in an ntdll wait stub (NtWaitForSingleObject, NtWaitForMultipleObjects, NtWaitForAlertByThreadId, NtDelayExecution, the I/O completion and keyed-event waits) or in win32u's NtUserMsgWaitForMultipleObjectsEx. A thread blocked there is still redirected, as before, which guest_raise_exception's host-blocked and lock-blocked cases rely on.
  • A target running other host code is resumed and suspended again until it is back in guest code, for up to the existing 1 s retry limit. After that it is redirected where it is, as before. A target in NtContinue or winpthreads still throws at the limit.

Tested

  • Windows 11, WinLibs GCC 15.2.0. The new guest_raise_in_host_code test has a scePthread writing 1 MiB blocks to a tmpfile() through ucrt while the main thread raises 20 times. The handler waits until the main thread has written to the same file. It times out on main (the main thread blocks on the stream lock the redirected writer still holds) and passes with this change. guest_raise_exception, guest_raise_nested_sleep and guest_mutex_raise_exception pass. guest_raise_exception takes 18.8 s here, against 15.8 s on main.
  • SONIC SUPERSTARS on main f03a2da5e, RX 5700 XT: without the change it stops presenting at frame 65 with no error. With it, it runs past frame 4000 at 60 FPS and then reaches the known native multisample depth metadata fill transition is not implemented stop (#6297, #6300).

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: PPSA06888 #5382 (startup freeze)
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4912524906 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7652). Originally posted by [billythemenace2](https://github.com/billythemenace2) on 2026-10-18T20:25:19Z. --- <!-- claude-projects-attribution --> _Requested by **billy**_ ### What On Windows, `sceKernelRaiseException` suspends the target thread and redirects it to the guest handler wherever it stopped. The only exceptions are NtContinue and winpthreads. A target stopped inside host code, such as a C runtime call or a host library, can be holding a host lock there. If the handler then waits, as a collector's thread-stop handler does, the lock stays held, and every host thread that needs it stops too. SONIC SUPERSTARS (PPSA06888) froze at frame 65 on current `main` because of this. Its IL2CPP "UDS" thread was redirected in the middle of libSceNpTrophy2's `[trophy] ...` stderr write (the log line breaks off mid-word) and parked in `sceKernelWaitEventFlag`. gdb showed it as the `OwningThread` of the ucrt stderr critical section, with recursion 2. The driver's `ShaderDiskCache` report and `AsyncLog` threads were blocked in `RtlEnterCriticalSection` on that lock. - `InHostModule`: the address is in a loaded module other than the executable or a `*.guest.prx`. - `BlockedInWait`: the address is in an ntdll wait stub (`NtWaitForSingleObject`, `NtWaitForMultipleObjects`, `NtWaitForAlertByThreadId`, `NtDelayExecution`, the I/O completion and keyed-event waits) or in win32u's `NtUserMsgWaitForMultipleObjectsEx`. A thread blocked there is still redirected, as before, which `guest_raise_exception`'s host-blocked and lock-blocked cases rely on. - A target running other host code is resumed and suspended again until it is back in guest code, for up to the existing 1 s retry limit. After that it is redirected where it is, as before. A target in NtContinue or winpthreads still throws at the limit. ### Tested - Windows 11, WinLibs GCC 15.2.0. The new `guest_raise_in_host_code` test has a scePthread writing 1 MiB blocks to a `tmpfile()` through ucrt while the main thread raises 20 times. The handler waits until the main thread has written to the same file. It times out on `main` (the main thread blocks on the stream lock the redirected writer still holds) and passes with this change. `guest_raise_exception`, `guest_raise_nested_sleep` and `guest_mutex_raise_exception` pass. `guest_raise_exception` takes 18.8 s here, against 15.8 s on `main`. - SONIC SUPERSTARS on `main` `f03a2da5e`, RX 5700 XT: without the change it stops presenting at frame 65 with no error. With it, it runs past frame 4000 at 60 FPS and then reaches the known `native multisample depth metadata fill transition is not implemented` stop (#6297, #6300). ### 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: PPSA06888 #5382 (startup freeze) - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-18 20:48:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T20:48:11Z.


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
PR 4630 docs/dev/debt/libkernel.md L148

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 4972 core/libs/prx/libkernel/CMakeLists.txt
PR 5021 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/System/src/Exception.cpp
docs/dev/debt/libkernel.md
PR 6044 core/libs/prx/libkernel/CMakeLists.txt
PR 6149 docs/dev/debt/libkernel.md
PR 6226 core/libs/prx/libkernel/CMakeLists.txt
PR 6469 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 6519 docs/dev/debt/libkernel.md
PR 6658 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 6740 docs/dev/debt/libkernel.md
PR 6879 core/libs/prx/libkernel/System/src/Exception.cpp
PR 6942 core/libs/prx/libkernel/CMakeLists.txt
PR 6977 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 7049 docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7201 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/System/src/Exception.cpp
PR 7332 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/System/src/Exception.cpp
docs/dev/debt/libkernel.md
PR 7437 docs/dev/debt/libkernel.md
PR 7464 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 7509 docs/dev/debt/libkernel.md
PR 7530 docs/dev/debt/libkernel.md
PR 7541 docs/dev/debt/libkernel.md
PR 7605 docs/dev/debt/libkernel.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6254965903 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7652#issuecomment-6254965903). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-18T20:48:11Z. --- <!-- 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 | |---|---|---|---| | [PR 4630](https://www.github.com/boykopovar/AnyPS5/pull/4630) | `docs/dev/debt/libkernel.md` [L148](https://github.com/boykopovar/AnyPS5/blob/1d41645527f52139738b9a31a77daa88e1892db8/docs/dev/debt/libkernel.md#L148) | | | Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 4972](https://www.github.com/boykopovar/AnyPS5/pull/4972) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`core/libs/prx/libkernel/System/src/Exception.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 6149](https://www.github.com/boykopovar/AnyPS5/pull/6149) | `docs/dev/debt/libkernel.md` | | [PR 6226](https://www.github.com/boykopovar/AnyPS5/pull/6226) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 6469](https://www.github.com/boykopovar/AnyPS5/pull/6469) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`docs/dev/debt/libkernel.md` | | [PR 6519](https://www.github.com/boykopovar/AnyPS5/pull/6519) | `docs/dev/debt/libkernel.md` | | [PR 6658](https://www.github.com/boykopovar/AnyPS5/pull/6658) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`docs/dev/debt/libkernel.md` | | [PR 6740](https://www.github.com/boykopovar/AnyPS5/pull/6740) | `docs/dev/debt/libkernel.md` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/libs/prx/libkernel/System/src/Exception.cpp` | | [PR 6942](https://www.github.com/boykopovar/AnyPS5/pull/6942) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`docs/dev/debt/libkernel.md` | | [PR 7049](https://www.github.com/boykopovar/AnyPS5/pull/7049) | `docs/dev/debt/libkernel.md` | | [PR 7105](https://www.github.com/boykopovar/AnyPS5/pull/7105) | `docs/dev/debt/libkernel.md` | | [PR 7149](https://www.github.com/boykopovar/AnyPS5/pull/7149) | `docs/dev/debt/libkernel.md` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`core/libs/prx/libkernel/System/src/Exception.cpp` | | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`core/libs/prx/libkernel/System/src/Exception.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 7437](https://www.github.com/boykopovar/AnyPS5/pull/7437) | `docs/dev/debt/libkernel.md` | | [PR 7464](https://www.github.com/boykopovar/AnyPS5/pull/7464) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`docs/dev/debt/libkernel.md` | | [PR 7509](https://www.github.com/boykopovar/AnyPS5/pull/7509) | `docs/dev/debt/libkernel.md` | | [PR 7530](https://www.github.com/boykopovar/AnyPS5/pull/7530) | `docs/dev/debt/libkernel.md` | | [PR 7541](https://www.github.com/boykopovar/AnyPS5/pull/7541) | `docs/dev/debt/libkernel.md` | | [PR 7605](https://www.github.com/boykopovar/AnyPS5/pull/7605) | `docs/dev/debt/libkernel.md` |
wachi35676 commented 2026-10-19 00:12:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-19T00:12:54Z.


Tested at 2e363d375 on Windows 11 (GCC 15.2), main 8a456334f. The four raise tests pass. As a negative control, guest_raise_in_host_code with main's Exception.cpp failed 7 of 10 runs (timeout), and 0 of 10 with this branch. So it catches the bug, but not on every run.

I measured the cost of waiting for guest code with a target thread looping LoadLibraryW(L"dbghelp.dll")/FreeLibrary plus GetModuleFileNameW:

  • main delivers 300 raises in 0.01 s (median 0.04 ms).
  • This branch took 6.66 s and 3.03 s for 20 raises in two runs (median 185 / 76 ms, max 1010 / 949 ms).
  • A target spinning on scePthreadYield, sleeping in sceKernelUsleep or calling ucrt snprintf shows no change (under 1 ms).

InHostModule counts libkernel.prx, libc.prx and the other HLE modules as host modules. So a thread inside a long HLE or driver call now waits up to 1 s per raise, and after that it is still redirected inside host code. Could docs/dev/debt/libkernel.md get a Windows entry covering both points?

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6256915873 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7652#issuecomment-6256915873). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-19T00:12:54Z. --- Tested at `2e363d375` on Windows 11 (GCC 15.2), `main` `8a456334f`. The four raise tests pass. As a negative control, `guest_raise_in_host_code` with `main`'s `Exception.cpp` failed 7 of 10 runs (timeout), and 0 of 10 with this branch. So it catches the bug, but not on every run. I measured the cost of waiting for guest code with a target thread looping `LoadLibraryW(L"dbghelp.dll")`/`FreeLibrary` plus `GetModuleFileNameW`: - `main` delivers 300 raises in 0.01 s (median 0.04 ms). - This branch took 6.66 s and 3.03 s for 20 raises in two runs (median 185 / 76 ms, max 1010 / 949 ms). - A target spinning on `scePthreadYield`, sleeping in `sceKernelUsleep` or calling ucrt `snprintf` shows no change (under 1 ms). `InHostModule` counts libkernel.prx, libc.prx and the other HLE modules as host modules. So a thread inside a long HLE or driver call now waits up to 1 s per raise, and after that it is still redirected inside host code. Could `docs/dev/debt/libkernel.md` get a Windows entry covering both points? AI-assisted: yes (Claude Code); results above were run on this machine.
whamre commented 2026-10-19 11:54:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by whamre on 2026-10-19T11:54:11Z.


This also unblocks LEGO Builder's Journey (PPSA05496). We hit the same deadlock independently: IL2CPP's GC raises SIGUSR1 to stop the world, the main thread was redirected into the suspend handler inside host code, and the title stopped presenting after two frames. A local change with the same idea (redirect only once the target is back in title code) gets it past the logo into the first scene. Tested on Windows 11 (26300), GCC 15.2, RTX 2080 SUPER.

One problem we ran into while writing ours, which this version has too: InHostModule calls GetModuleHandleExW and GetModuleFileNameW while the target is suspended, and both take loader locks. If the target was suspended while holding one (inside LoadLibrary, FreeLibrary or a DLL notification, as in the dbghelp.dll loop in the measurement above), the raising thread blocks on that lock while it keeps the target suspended, and neither thread continues. The static initializer of BlockedInWait (GetModuleHandleW, GetProcAddress) has the same issue on the first raise, as do the existing ones in RestoringContext and InWinpthread.

What we did instead:

  • While the target is suspended, only VirtualQuery(rip) is called, and its AllocationBase is looked up in a small lock-free cache of image bases → guest/host. A MEM_IMAGE base that isn't cached yet is classified with GetModuleFileNameW after ResumeThread, and the target is sampled again.
  • The lookups behind the statics are run once before the first SuspendThread.
  • Nothing is allocated while the target is suspended (it might hold the heap lock).

For the cost raised in the review, we also queue an APC as soon as the target is found in host code and keep sampling. Whichever happens first runs the handler: the APC at the thread's next alertable TimedWait wait (which already drains APCs), or the redirect once the thread is sampled in title code. An atomic claim makes sure it runs once. This helps a thread that leaves an HLE call into a wait. It doesn't help one that stays in host code without waiting, like the LoadLibraryW loop, which still waits up to 1 s.

If it's useful, I can send the cache, the warm-up and the APC part as a follow-up PR once this one is merged, together with the Windows entry for docs/dev/debt/libkernel.md requested above.

AI-assisted: yes (Claude). The LEGO results are from my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6266591106 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7652#issuecomment-6266591106). Originally posted by [whamre](https://github.com/whamre) on 2026-10-19T11:54:11Z. --- This also unblocks **LEGO Builder's Journey (PPSA05496)**. We hit the same deadlock independently: IL2CPP's GC raises SIGUSR1 to stop the world, the main thread was redirected into the suspend handler inside host code, and the title stopped presenting after two frames. A local change with the same idea (redirect only once the target is back in title code) gets it past the logo into the first scene. Tested on Windows 11 (26300), GCC 15.2, RTX 2080 SUPER. One problem we ran into while writing ours, which this version has too: `InHostModule` calls `GetModuleHandleExW` and `GetModuleFileNameW` while the target is suspended, and both take loader locks. If the target was suspended while holding one (inside `LoadLibrary`, `FreeLibrary` or a DLL notification, as in the `dbghelp.dll` loop in the measurement above), the raising thread blocks on that lock while it keeps the target suspended, and neither thread continues. The `static` initializer of `BlockedInWait` (`GetModuleHandleW`, `GetProcAddress`) has the same issue on the first raise, as do the existing ones in `RestoringContext` and `InWinpthread`. What we did instead: - While the target is suspended, only `VirtualQuery(rip)` is called, and its `AllocationBase` is looked up in a small lock-free cache of image bases → guest/host. A `MEM_IMAGE` base that isn't cached yet is classified with `GetModuleFileNameW` *after* `ResumeThread`, and the target is sampled again. - The lookups behind the `static`s are run once before the first `SuspendThread`. - Nothing is allocated while the target is suspended (it might hold the heap lock). For the cost raised in the review, we also queue an APC as soon as the target is found in host code and keep sampling. Whichever happens first runs the handler: the APC at the thread's next alertable `TimedWait` wait (which already drains APCs), or the redirect once the thread is sampled in title code. An atomic claim makes sure it runs once. This helps a thread that leaves an HLE call into a wait. It doesn't help one that stays in host code without waiting, like the `LoadLibraryW` loop, which still waits up to 1 s. If it's useful, I can send the cache, the warm-up and the APC part as a follow-up PR once this one is merged, together with the Windows entry for `docs/dev/debt/libkernel.md` requested above. AI-assisted: yes (Claude). The LEGO results are from my machine.
billythemenace2 commented 2026-10-19 16:23:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by billythemenace2 on 2026-10-19T16:23:21Z.


@whamre thank you, the loader-lock deadlock is a real bug in this version, and good to hear it unblocks LEGO Builder's Journey too. @wachi35676 thank you for the negative control and the cost measurements.

Updated at d2657b975:

  • No loader calls while the target is suspended. Only VirtualQuery(rip) runs then, and its AllocationBase is looked up in a lock-free table of image bases marked guest or host (the executable and *.guest.prx are guest). An image not yet in the table is classified with GetModuleFileNameW after ResumeThread, and the target is sampled again. A full table counts unknown images as host. Nothing is allocated while the target is suspended.
  • Warm-up: the statics behind RestoringContext, InWinpthread, BlockedInWait, the executable base and the table are initialized before the first SuspendThread.
  • Debt entry: docs/dev/debt/libkernel.md has a Windows entry for sceKernelRaiseException. It says the HLE modules count as host code, that a target staying in host code costs the raiser up to 1 s per raise and is then redirected there anyway, and that the APC fast path is not implemented.
  • Test: guest_raise_in_host_code now raises 100 times. With main's Exception.cpp it timed out on 6 of 6 runs, and it passes on every run with this branch.

On the RX 5700 XT the four raise tests pass, guest_raise_exception 11 of 12 runs at 18.6-22.4 s. The first run after the rebuild timed out at 62 s once and I couldn't reproduce it in 11 reruns or a direct run, so I'm noting it in case it shows up for anyone else.

@whamre yes please, the APC part as a follow-up would be welcome. It addresses exactly the cost @wachi35676 measured for a thread that leaves an HLE call into a wait. I left it out here so this stays the minimal fix.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6271918957 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7652#issuecomment-6271918957). Originally posted by [billythemenace2](https://github.com/billythemenace2) on 2026-10-19T16:23:21Z. --- @whamre thank you, the loader-lock deadlock is a real bug in this version, and good to hear it unblocks LEGO Builder's Journey too. @wachi35676 thank you for the negative control and the cost measurements. Updated at [`d2657b975`](https://github.com/billythemenace2/AnyPS5/commit/d2657b975): - **No loader calls while the target is suspended.** Only `VirtualQuery(rip)` runs then, and its `AllocationBase` is looked up in a lock-free table of image bases marked guest or host (the executable and `*.guest.prx` are guest). An image not yet in the table is classified with `GetModuleFileNameW` after `ResumeThread`, and the target is sampled again. A full table counts unknown images as host. Nothing is allocated while the target is suspended. - **Warm-up:** the statics behind `RestoringContext`, `InWinpthread`, `BlockedInWait`, the executable base and the table are initialized before the first `SuspendThread`. - **Debt entry:** `docs/dev/debt/libkernel.md` has a Windows entry for `sceKernelRaiseException`. It says the HLE modules count as host code, that a target staying in host code costs the raiser up to 1 s per raise and is then redirected there anyway, and that the APC fast path is not implemented. - **Test:** `guest_raise_in_host_code` now raises 100 times. With `main`'s `Exception.cpp` it timed out on 6 of 6 runs, and it passes on every run with this branch. On the RX 5700 XT the four raise tests pass, `guest_raise_exception` 11 of 12 runs at 18.6-22.4 s. The first run after the rebuild timed out at 62 s once and I couldn't reproduce it in 11 reruns or a direct run, so I'm noting it in case it shows up for anyone else. @whamre yes please, the APC part as a follow-up would be welcome. It addresses exactly the cost @wachi35676 measured for a thread that leaves an HLE call into a wait. I left it out here so this stays the minimal fix. AI-assisted (Claude Code).
adriadam10 commented 2026-10-20 11:17:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-20T11:17:59Z.


Checked the diff against the review thread:

  • Only VirtualQuery and the lock-free image table run while the target is suspended; unknown images are classified after ResumeThread, and a full table counts as host.
  • Statics are warmed before the first SuspendThread; the NtContinue/winpthreads 1 s throw is unchanged.
  • guest_raise_in_host_code is registered under WIN32, and the debt entry covers the up-to-1 s cost per raise and the missing APC path.

Merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6289653878 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7652#issuecomment-6289653878). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-20T11:17:59Z. --- Checked the diff against the review thread: - Only `VirtualQuery` and the lock-free image table run while the target is suspended; unknown images are classified after `ResumeThread`, and a full table counts as host. - Statics are warmed before the first `SuspendThread`; the NtContinue/winpthreads 1 s throw is unchanged. - `guest_raise_in_host_code` is registered under `WIN32`, and the debt entry covers the up-to-1 s cost per raise and the missing APC path. Merged.
oneandonlydean commented 2026-10-20 12:06:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T12:06:58Z.


Since this merged, guest_raise_exception on the Windows CI runners has gone from 14.7-14.9 s (4 runs on the previous main) to 17-29 s, with 6 of 10 runs hitting the 30 s timeout. The runs were on unrelated pull requests, all on main d50ec76da0. This PR is the only merge in that range that touches the raise path, and it matches the 15.8 → 18.8 s you measured locally.

The cause is the new retry: a target running host code is resumed and re-suspended until it is sampled in guest code, for up to the 1 s RetryLimit per raise. Several cases in guest_raise_exception raise against threads that loop outside guest code: RaiseException (kernelbase/ntdll dispatch), scePthreadSelf and sceKernelSyncOnAddressWake (our libraries are built as .prx, so they count as host code), and std::this_thread::yield. That's about 220 raises, and each can now wait up to 1 s, more often on a loaded 4-core runner. The same cap applies at run time: a collector that stops every thread with a raise can wait up to 1 s for each thread in host code or in a syscall not on the wait list.

Could you give host code its own short limit, separate from the 1 s limit that still guards NtContinue and winpthreads? For example, a host deadline of around 50 ms after which the target is redirected where it is, as before. Your guest_raise_in_host_code writer spins in guest code between every 1 MiB write, so it should still pass, and so should the original stderr case. Logging the longest wait per raise in both tests would show what value is safe. Treating NtYieldExecution like the listed waits might also be worth considering.

Before it merges we'll check: guest_raise_exception back near 15 s on Windows CI (three runs, no timeout), guest_raise_in_host_code, guest_raise_nested_sleep and guest_mutex_raise_exception passing, and your title run still getting past the freeze.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6290463780 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7652#issuecomment-6290463780). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T12:06:58Z. --- Since this merged, `guest_raise_exception` on the Windows CI runners has gone from 14.7-14.9 s (4 runs on the previous main) to 17-29 s, with 6 of 10 runs hitting the 30 s timeout. The runs were on unrelated pull requests, all on main `d50ec76da0`. This PR is the only merge in that range that touches the raise path, and it matches the 15.8 → 18.8 s you measured locally. The cause is the new retry: a target running host code is resumed and re-suspended until it is sampled in guest code, for up to the 1 s `RetryLimit` per raise. Several cases in `guest_raise_exception` raise against threads that loop outside guest code: `RaiseException` (kernelbase/ntdll dispatch), `scePthreadSelf` and `sceKernelSyncOnAddressWake` (our libraries are built as `.prx`, so they count as host code), and `std::this_thread::yield`. That's about 220 raises, and each can now wait up to 1 s, more often on a loaded 4-core runner. The same cap applies at run time: a collector that stops every thread with a raise can wait up to 1 s for each thread in host code or in a syscall not on the wait list. Could you give host code its own short limit, separate from the 1 s limit that still guards NtContinue and winpthreads? For example, a host deadline of around 50 ms after which the target is redirected where it is, as before. Your `guest_raise_in_host_code` writer spins in guest code between every 1 MiB write, so it should still pass, and so should the original stderr case. Logging the longest wait per raise in both tests would show what value is safe. Treating `NtYieldExecution` like the listed waits might also be worth considering. Before it merges we'll check: `guest_raise_exception` back near 15 s on Windows CI (three runs, no timeout), `guest_raise_in_host_code`, `guest_raise_nested_sleep` and `guest_mutex_raise_exception` passing, and your title run still getting past the freeze.
whamre commented 2026-10-20 14:43:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by whamre on 2026-10-20T14:43:58Z.


Numbers for the host-code limit, from a local build of main 30fda0a03 with the limit read from an environment variable (not a PR). Windows 11 (26300), i5-10500 (6 cores, 12 threads), WinLibs GCC 15.2.0, Release:

Host-code limit guest_raise_in_host_code guest_raise_exception
1000 ms (as merged) 3 of 3 pass 11.3 s ×3
50 ms 3 of 3 pass 11.2 s ×3
10 ms 1 of 3 fails (30 s timeout) 11.2 s ×3
0 ms (redirect at once) 3 of 3 fail n/a (not timed)

guest_raise_nested_sleep and guest_mutex_raise_exception pass at every limit. The longest wait for one raise in guest_raise_exception, at the 1000 ms limit, was 21.5 ms. So on this 6-core machine the host-code freeze needs somewhere between 10 and 50 ms. 50 ms passes here but is about 2.3 times the longest wait measured, which may be tight on a loaded 4-core runner. Logging the longest wait per raise on CI, as suggested above, would show how much margin it has there. I can run LEGO Builder's Journey (PPSA05496), the title that froze without this, against a candidate once its shader cache is warm.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6293570557 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7652#issuecomment-6293570557). Originally posted by [whamre](https://github.com/whamre) on 2026-10-20T14:43:58Z. --- Numbers for the host-code limit, from a local build of `main` `30fda0a03` with the limit read from an environment variable (not a PR). Windows 11 (26300), i5-10500 (6 cores, 12 threads), WinLibs GCC 15.2.0, Release: | Host-code limit | `guest_raise_in_host_code` | `guest_raise_exception` | |---|---|---| | 1000 ms (as merged) | 3 of 3 pass | 11.3 s ×3 | | 50 ms | 3 of 3 pass | 11.2 s ×3 | | 10 ms | 1 of 3 fails (30 s timeout) | 11.2 s ×3 | | 0 ms (redirect at once) | 3 of 3 fail | n/a (not timed) | `guest_raise_nested_sleep` and `guest_mutex_raise_exception` pass at every limit. The longest wait for one raise in `guest_raise_exception`, at the 1000 ms limit, was 21.5 ms. So on this 6-core machine the host-code freeze needs somewhere between 10 and 50 ms. 50 ms passes here but is about 2.3 times the longest wait measured, which may be tight on a loaded 4-core runner. Logging the longest wait per raise on CI, as suggested above, would show how much margin it has there. I can run LEGO Builder's Journey (PPSA05496), the title that froze without this, against a candidate once its shader cache is warm. AI-assisted: yes.
whamre commented 2026-10-20 15:31:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by whamre on 2026-10-20T15:31:59Z.


Follow-up with LEGO Builder's Journey (PPSA05496), the title the host-code retry was first needed for: on main 30fda0a03, 2 runs each with the host-code limit at 0, 50 and 1000 ms all get past the point where it froze, to the next unrelated stop. None of the 77 GC raises per run met a target in host code, so this title doesn't constrain the limit; the guest_raise_in_host_code numbers above are what bound it.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6294606233 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7652#issuecomment-6294606233). Originally posted by [whamre](https://github.com/whamre) on 2026-10-20T15:31:59Z. --- Follow-up with LEGO Builder's Journey (PPSA05496), the title the host-code retry was first needed for: on `main` `30fda0a03`, 2 runs each with the host-code limit at 0, 50 and 1000 ms all get past the point where it froze, to the next unrelated stop. None of the 77 GC raises per run met a target in host code, so this title doesn't constrain the limit; the `guest_raise_in_host_code` numbers above are what bound it. AI-assisted: yes.
billythemenace2 commented 2026-10-20 15:40:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by billythemenace2 on 2026-10-20T15:40:53Z.


@oneandonlydean @whamre thank you both, and for the numbers. The follow-up is https://github.com/boykopovar/AnyPS5/pull/8539. Host code now has its own 100 ms limit, NtContinue and winpthreads keep 1 s, and NtYieldExecution counts as a wait. A failed GetThreadContext is retried instead of thrown, which Sonic hit once. guest_raise_in_host_code prints the longest raise. I picked 100 ms over 50 ms because the longest raise in that test reached 45 ms here. Here guest_raise_exception is back to 12.7-14.0 s.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6294805592 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7652#issuecomment-6294805592). Originally posted by [billythemenace2](https://github.com/billythemenace2) on 2026-10-20T15:40:53Z. --- @oneandonlydean @whamre thank you both, and for the numbers. The follow-up is https://github.com/boykopovar/AnyPS5/pull/8539. Host code now has its own 100 ms limit, NtContinue and winpthreads keep 1 s, and `NtYieldExecution` counts as a wait. A failed `GetThreadContext` is retried instead of thrown, which Sonic hit once. `guest_raise_in_host_code` prints the longest raise. I picked 100 ms over 50 ms because the longest raise in that test reached 45 ms here. Here `guest_raise_exception` is back to 12.7-14.0 s. AI-assisted (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!7652
No description provided.