fix(libkernel): retry a failed thread context read during a raise #8539

Merged
billythemenace2 merged 1 commit from upstream-pr/8539 into main 2026-10-22 04:59:56 +00:00
billythemenace2 commented 2026-10-20 15:40:51 +00:00 (Migrated from github.com)

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


Requested by billy

What

GetThreadContext can fail on a target that was just suspended. RaiseOn used to throw sceKernelRaiseException: cannot read the target thread context at the first failure. Now it resumes the target, yields, and samples again within the existing 1 s RetryLimit, and throws only once that limit has passed. Every failed attempt resumes the target before it loops, so suspends and resumes stay balanced. SONIC SUPERSTARS (PPSA06888) hit the throw once in three runs on a build with #7652.

guest_raise_in_host_code also prints the longest single raise, so CI logs show how much room the host-code limit from #8512 has.

The host-code limit and the NtYieldExecution redirect this PR first carried are gone. The limit is on main through #8512. The yield redirect would run the guest handler with GuestMemory::GpuMutex() held, because WaitForLeasesAndImports and the recorder teardown loops yield under it.

Tested

Windows 11, Ryzen 7 7700, WinLibs GCC 15.2.0, Release, main 434e965d4, three runs. guest_raise_exception passes in 12.4-13.2 s, guest_raise_in_host_code passes with the longest raise at 5.4-10.6 ms, and guest_raise_nested_sleep and guest_mutex_raise_exception pass. The failure path itself isn't covered by a test: GetThreadContext failing on a suspended thread can't be triggered on demand.

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)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4938308637 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8539). Originally posted by [billythemenace2](https://github.com/billythemenace2) on 2026-10-20T15:40:51Z. --- <!-- claude-projects-attribution --> _Requested by **billy**_ ### What `GetThreadContext` can fail on a target that was just suspended. `RaiseOn` used to throw `sceKernelRaiseException: cannot read the target thread context` at the first failure. Now it resumes the target, yields, and samples again within the existing 1 s `RetryLimit`, and throws only once that limit has passed. Every failed attempt resumes the target before it loops, so suspends and resumes stay balanced. SONIC SUPERSTARS (PPSA06888) hit the throw once in three runs on a build with #7652. `guest_raise_in_host_code` also prints the longest single raise, so CI logs show how much room the host-code limit from #8512 has. The host-code limit and the `NtYieldExecution` redirect this PR first carried are gone. The limit is on `main` through #8512. The yield redirect would run the guest handler with `GuestMemory::GpuMutex()` held, because `WaitForLeasesAndImports` and the recorder teardown loops yield under it. ### Tested Windows 11, Ryzen 7 7700, WinLibs GCC 15.2.0, Release, `main` `434e965d4`, three runs. `guest_raise_exception` passes in 12.4-13.2 s, `guest_raise_in_host_code` passes with the longest raise at 5.4-10.6 ms, and `guest_raise_nested_sleep` and `guest_mutex_raise_exception` pass. The failure path itself isn't covered by a test: `GetThreadContext` failing on a suspended thread can't be triggered on demand. ### 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) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
oneandonlydean commented 2026-10-20 15:47:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T15:47:07Z.


Thanks. The 100 ms host limit and the GetThreadContext retry both look right to us. The retry keeps suspend and resume balanced (each failed attempt resumes before it loops), and it stays bounded by the 1 s limit. One change before this can go in:

Please drop NtYieldExecution from BlockedInWait. Waits are redirected in place on the assumption that a thread sitting in one is safe to interrupt. Our own host code doesn't hold to that for yields: WaitForLeasesAndImports in libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp calls std::this_thread::yield() inside std::lock_guard lock(GuestMemory::GpuMutex()). That's the wait a guest munmap or mprotect of GPU-leased memory runs, and GuestAllocations.cpp calls it in a loop. A stop-the-world raise that catches a thread there would run the handler with the GPU mutex held, which is the same deadlock shape #7652 fixed for the stderr lock. Without the yield entry, that thread is sampled until it leaves the wait or the 100 ms limit expires, as with any other host code.

If you'd rather keep the yield redirect, moving that yield outside the GPU mutex in this PR would also work, but the recorder teardown loops (Recorder.cpp) yield under the same mutex too. Dropping the line is the smaller change. Please also take the NtYieldExecution clause out of the debt entry.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6294935248 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8539#issuecomment-6294935248). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T15:47:07Z. --- Thanks. The 100 ms host limit and the `GetThreadContext` retry both look right to us. The retry keeps suspend and resume balanced (each failed attempt resumes before it loops), and it stays bounded by the 1 s limit. One change before this can go in: **Please drop `NtYieldExecution` from `BlockedInWait`.** Waits are redirected in place on the assumption that a thread sitting in one is safe to interrupt. Our own host code doesn't hold to that for yields: `WaitForLeasesAndImports` in `libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp` calls `std::this_thread::yield()` inside `std::lock_guard lock(GuestMemory::GpuMutex())`. That's the wait a guest `munmap` or `mprotect` of GPU-leased memory runs, and `GuestAllocations.cpp` calls it in a loop. A stop-the-world raise that catches a thread there would run the handler with the GPU mutex held, which is the same deadlock shape #7652 fixed for the stderr lock. Without the yield entry, that thread is sampled until it leaves the wait or the 100 ms limit expires, as with any other host code. If you'd rather keep the yield redirect, moving that yield outside the GPU mutex in this PR would also work, but the recorder teardown loops (`Recorder.cpp`) yield under the same mutex too. Dropping the line is the smaller change. Please also take the `NtYieldExecution` clause out of the debt entry.
github-actions[bot] commented 2026-10-20 15:56:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T15:56:02Z.


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
PR 5021 core/libs/prx/libkernel/System/src/Exception.cpp
PR 6879 core/libs/prx/libkernel/System/src/Exception.cpp
PR 7201 core/libs/prx/libkernel/System/src/Exception.cpp
PR 8592 core/libs/prx/libkernel/System/src/Exception.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6295127962 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8539#issuecomment-6295127962). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T15:56:02Z. --- <!-- pr-overlap --> 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` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/System/src/Exception.cpp` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/libs/prx/libkernel/System/src/Exception.cpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/System/src/Exception.cpp` | | [PR 8592](https://www.github.com/boykopovar/AnyPS5/pull/8592) | `core/libs/prx/libkernel/System/src/Exception.cpp` |
oneandonlydean commented 2026-10-20 18:00:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T18:00:44Z.


@billythemenace2 thanks for this, and sorry it raced with ours. We had a deadline on the Windows guest_raise_exception timeouts, so #8512 has now merged (eae6e0162f). It's the same core fix: a separate host-code deadline in the raise loop, at 50 ms instead of 100 ms. That part of this PR is now on main, and this branch conflicts with it in Exception.cpp.

The GetThreadContext retry here is a real improvement that #8512 doesn't have. Each failed attempt resumes the thread before retrying, so suspend and resume stay balanced, and it's bounded by the existing deadline. Could you rebase onto current main and shrink this PR to just that retry, plus its test in GuestRaiseInHostCode.cpp? That would make it a small, easy review.

One thing to leave out: the NtYieldExecution redirect. Our host code yields while holding the GPU mutex (for example the lease wait in GuestBufferMemory.cpp, which a guest munmap/mprotect of GPU memory runs), so redirecting a thread that sits in that yield runs the guest handler with the mutex held. That's the same deadlock shape as #7652. If you need that part, it would need that yield moved outside the mutex in the same change.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6297618688 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8539#issuecomment-6297618688). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T18:00:44Z. --- @billythemenace2 thanks for this, and sorry it raced with ours. We had a deadline on the Windows `guest_raise_exception` timeouts, so #8512 has now merged (eae6e0162f). It's the same core fix: a separate host-code deadline in the raise loop, at 50 ms instead of 100 ms. That part of this PR is now on `main`, and this branch conflicts with it in `Exception.cpp`. The `GetThreadContext` retry here is a real improvement that #8512 doesn't have. Each failed attempt resumes the thread before retrying, so suspend and resume stay balanced, and it's bounded by the existing deadline. Could you rebase onto current `main` and shrink this PR to just that retry, plus its test in `GuestRaiseInHostCode.cpp`? That would make it a small, easy review. One thing to leave out: the `NtYieldExecution` redirect. Our host code yields while holding the GPU mutex (for example the lease wait in `GuestBufferMemory.cpp`, which a guest `munmap`/`mprotect` of GPU memory runs), so redirecting a thread that sits in that yield runs the guest handler with the mutex held. That's the same deadlock shape as #7652. If you need that part, it would need that yield moved outside the mutex in the same change.
billythemenace2 commented 2026-10-20 19:12:45 +00:00 (Migrated from github.com)

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


@oneandonlydean thank you, and no problem about the race. #8512 covers the limit. I've rebased onto main 434e965d4 and shrunk this PR to the GetThreadContext retry plus the longest-raise output in guest_raise_in_host_code (e5b66f2dd, 2 files). The NtYieldExecution redirect is gone: you're right that the lease wait and the recorder teardown yield under GpuMutex(), which is exactly the #7652 deadlock shape. There's no debt-entry change left either. Title and description are updated. The four raise tests pass three times here, with guest_raise_exception at 12.4-13.2 s.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6299092275 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8539#issuecomment-6299092275). Originally posted by [billythemenace2](https://github.com/billythemenace2) on 2026-10-20T19:12:45Z. --- @oneandonlydean thank you, and no problem about the race. #8512 covers the limit. I've rebased onto `main` `434e965d4` and shrunk this PR to the `GetThreadContext` retry plus the longest-raise output in `guest_raise_in_host_code` ([`e5b66f2dd`](https://github.com/billythemenace2/AnyPS5/commit/e5b66f2dd), 2 files). The `NtYieldExecution` redirect is gone: you're right that the lease wait and the recorder teardown yield under `GpuMutex()`, which is exactly the #7652 deadlock shape. There's no debt-entry change left either. Title and description are updated. The four raise tests pass three times here, with `guest_raise_exception` at 12.4-13.2 s. AI-assisted (Claude Code).
oneandonlydean commented 2026-10-22 04:22:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-22T04:22:31Z.


Thanks, and sorry this sat for a day. e5b66f2dd is what I hoped for: the context-read retry stays inside the existing 1 s limit and keeps suspend and resume balanced, and the NtYieldExecution redirect is gone. It merges cleanly with current main, including the new system-call wait in the same loop. I'm running the full workflow three times on this head. Run 1 is green: on Windows, guest_raise_exception passed on the first try in 13.5 s. If the raise tests pass on the first try in runs 2 and 3 as well, it goes in.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6332635494 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8539#issuecomment-6332635494). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-22T04:22:31Z. --- Thanks, and sorry this sat for a day. e5b66f2dd is what I hoped for: the context-read retry stays inside the existing 1 s limit and keeps suspend and resume balanced, and the NtYieldExecution redirect is gone. It merges cleanly with current main, including the new system-call wait in the same loop. I'm running the full workflow three times on this head. Run 1 is green: on Windows, guest_raise_exception passed on the first try in 13.5 s. If the raise tests pass on the first try in runs 2 and 3 as well, it goes in.
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!8539
No description provided.