fix(libkernel): keep waiting while a raise target is inside a system call #8637

Merged
billythemenace2 merged 1 commit from upstream-pr/8637 into main 2026-10-21 00:11:09 +00:00
billythemenace2 commented 2026-10-20 19:36:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by billythemenace2 on 2026-10-20T19:36:10Z.


Requested by billy

What

Follow-up to #7652 and #8512. Since #8512, a raise redirects a target running host code once 50 ms have passed since the raise began. A target inside a system call that isn't a wait can stay there longer than that. One case is the NtWriteFile of an fwrite that holds its stream lock. The redirect then runs the handler with the lock still held, which is the #7652 deadlock again.

SONIC SUPERSTARS (PPSA06888) freezes at frame 67 on main 434e965d4 this way. Its IL2CPP collector's raise was delivered at ntdll!ZwWriteFile+20 (twice in the log), and the stream lock stayed with the stopped thread.

  • InSystemCall: the target stopped right after a syscall instruction (0F 05) in ntdll. While the target is suspended, it reads only those two bytes, with the ntdll base warmed before the first suspend.
  • Window restart: such a sample restarts the 50 ms host-code window, so the raise keeps sampling until the call returns and the target is back in guest code. It stays bounded by the 1 s RetryLimit. The ntdll waits in BlockedInWait are still redirected in place, as before.
  • Debt entry: the libkernel entry now gives the 50 ms from #8512 (it still said 1 s) and the system-call case.

Tested

Windows 11, Ryzen 7 7700, WinLibs GCC 15.2.0, Release, main 434e965d4:

  • New guest_raise_in_system_call: a scePthread fwrites 64 KiB blocks into a 4 KiB pipe that a reader drains 4 KiB every 10 ms, so it sits in NtWriteFile holding the stream lock for about 160 ms per block. The handler waits until the main thread has fputc'd to the same stream, and the test raises 10 times. With main's Exception.cpp it times out in 4 of 4 runs. With this change it passes in 4 of 4, in 0.5 s.
  • Existing raise tests: guest_raise_exception (13.5-14.2 s), guest_raise_in_host_code, guest_raise_nested_sleep and guest_mutex_raise_exception pass, three runs each.
  • SONIC SUPERSTARS: without the change it stops presenting at frame 67. With it, two runs reach frames 1228 and 4359 at 60 FPS, then the known native multisample stencil or depth format import is not implemented stop (#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 on current main)
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4941361548 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8637). Originally posted by [billythemenace2](https://github.com/billythemenace2) on 2026-10-20T19:36:10Z. --- <!-- claude-projects-attribution --> _Requested by **billy**_ ### What Follow-up to #7652 and #8512. Since #8512, a raise redirects a target running host code once 50 ms have passed since the raise began. A target inside a system call that isn't a wait can stay there longer than that. One case is the `NtWriteFile` of an `fwrite` that holds its stream lock. The redirect then runs the handler with the lock still held, which is the #7652 deadlock again. SONIC SUPERSTARS (PPSA06888) freezes at frame 67 on `main` `434e965d4` this way. Its IL2CPP collector's raise was delivered at `ntdll!ZwWriteFile+20` (twice in the log), and the stream lock stayed with the stopped thread. - **`InSystemCall`:** the target stopped right after a `syscall` instruction (`0F 05`) in ntdll. While the target is suspended, it reads only those two bytes, with the ntdll base warmed before the first suspend. - **Window restart:** such a sample restarts the 50 ms host-code window, so the raise keeps sampling until the call returns and the target is back in guest code. It stays bounded by the 1 s `RetryLimit`. The ntdll waits in `BlockedInWait` are still redirected in place, as before. - **Debt entry:** the libkernel entry now gives the 50 ms from #8512 (it still said 1 s) and the system-call case. ### Tested Windows 11, Ryzen 7 7700, WinLibs GCC 15.2.0, Release, `main` `434e965d4`: - **New `guest_raise_in_system_call`:** a scePthread `fwrite`s 64 KiB blocks into a 4 KiB pipe that a reader drains 4 KiB every 10 ms, so it sits in `NtWriteFile` holding the stream lock for about 160 ms per block. The handler waits until the main thread has `fputc`'d to the same stream, and the test raises 10 times. With `main`'s `Exception.cpp` it times out in 4 of 4 runs. With this change it passes in 4 of 4, in 0.5 s. - **Existing raise tests:** `guest_raise_exception` (13.5-14.2 s), `guest_raise_in_host_code`, `guest_raise_nested_sleep` and `guest_mutex_raise_exception` pass, three runs each. - **SONIC SUPERSTARS:** without the change it stops presenting at frame 67. With it, two runs reach frames 1228 and 4359 at 60 FPS, then the known `native multisample stencil or depth format import is not implemented` stop (#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 on current `main`) - [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-20 19:51:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T19:51:55Z.


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 8592 core/libs/prx/libkernel/System/src/Exception.cpp L429
PR 8658 docs/dev/debt/libkernel.md L151-152

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 4630 core/libs/prx/libkernel/System/src/Exception.cpp
docs/dev/debt/libkernel.md
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 6740 docs/dev/debt/libkernel.md
PR 6879 core/libs/prx/libkernel/System/src/Exception.cpp
docs/dev/debt/libkernel.md
PR 6936 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 6977 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 7049 docs/dev/debt/libkernel.md
PR 7088 docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7121 docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7192 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/System/src/Exception.cpp
docs/dev/debt/libkernel.md
PR 7201 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/System/src/Exception.cpp
PR 7509 docs/dev/debt/libkernel.md
PR 7530 docs/dev/debt/libkernel.md
PR 7532 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 7653 docs/dev/debt/libkernel.md
PR 8520 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 8523 docs/dev/debt/libkernel.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6300045092 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8637#issuecomment-6300045092). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T19:51:55Z. --- <!-- 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 8592](https://www.github.com/boykopovar/AnyPS5/pull/8592) | `core/libs/prx/libkernel/System/src/Exception.cpp` [L429](https://github.com/boykopovar/AnyPS5/blob/5b59fbc5539b6c6db7a26972f6069f1c6cc179ac/core/libs/prx/libkernel/System/src/Exception.cpp#L429) | | | | [PR 8658](https://www.github.com/boykopovar/AnyPS5/pull/8658) | `docs/dev/debt/libkernel.md` [L151-152](https://github.com/boykopovar/AnyPS5/blob/5b59fbc5539b6c6db7a26972f6069f1c6cc179ac/docs/dev/debt/libkernel.md#L151-L152) | | | 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 4630](https://www.github.com/boykopovar/AnyPS5/pull/4630) | `core/libs/prx/libkernel/System/src/Exception.cpp`<br>`docs/dev/debt/libkernel.md` | | [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 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`<br>`docs/dev/debt/libkernel.md` | | [PR 6936](https://www.github.com/boykopovar/AnyPS5/pull/6936) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`docs/dev/debt/libkernel.md` | | [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 7088](https://www.github.com/boykopovar/AnyPS5/pull/7088) | `docs/dev/debt/libkernel.md` | | [PR 7105](https://www.github.com/boykopovar/AnyPS5/pull/7105) | `docs/dev/debt/libkernel.md` | | [PR 7121](https://www.github.com/boykopovar/AnyPS5/pull/7121) | `docs/dev/debt/libkernel.md` | | [PR 7149](https://www.github.com/boykopovar/AnyPS5/pull/7149) | `docs/dev/debt/libkernel.md` | | [PR 7192](https://www.github.com/boykopovar/AnyPS5/pull/7192) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`core/libs/prx/libkernel/System/src/Exception.cpp`<br>`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 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 7532](https://www.github.com/boykopovar/AnyPS5/pull/7532) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`docs/dev/debt/libkernel.md` | | [PR 7653](https://www.github.com/boykopovar/AnyPS5/pull/7653) | `docs/dev/debt/libkernel.md` | | [PR 8520](https://www.github.com/boykopovar/AnyPS5/pull/8520) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`docs/dev/debt/libkernel.md` | | [PR 8523](https://www.github.com/boykopovar/AnyPS5/pull/8523) | `docs/dev/debt/libkernel.md` |
xXPBSXx commented 2026-10-20 23:33:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T23:33:00Z.


Independent Windows run of this PR (data only).

Environment: Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against main 339217ebc9 is clean. I built only guest_raise_in_system_call_tests; ctest -N -R guest_raise_in_system_call lists exactly one test.

Results (3 runs each):

  • PR head: guest_raise_in_system_call passes 3/3.
  • Baseline: I kept the PR's test and replaced only core/libs/prx/libkernel/System/src/Exception.cpp with the version from the PR's merge base (434e965d46), then rebuilt. The test times out 3/3 (ctest TIMEOUT). I restored the PR's Exception.cpp afterwards.

Not run: other platforms, the other raise-exception tests, the full repository CTest, and any title or console run.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6304181499 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8637#issuecomment-6304181499). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-20T23:33:00Z. --- Independent Windows run of this PR (data only). **Environment:** Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against `main` 339217ebc9 is clean. I built only `guest_raise_in_system_call_tests`; `ctest -N -R guest_raise_in_system_call` lists exactly one test. **Results (3 runs each):** - PR head: `guest_raise_in_system_call` passes 3/3. - Baseline: I kept the PR's test and replaced only `core/libs/prx/libkernel/System/src/Exception.cpp` with the version from the PR's merge base (`434e965d46`), then rebuilt. The test times out 3/3 (ctest TIMEOUT). I restored the PR's `Exception.cpp` afterwards. **Not run:** other platforms, the other raise-exception tests, the full repository CTest, and any title or console run. 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!8637
No description provided.