fix(libkernel): retry a failed thread context read during a raise #8539
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8539"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by billythemenace2 on 2026-10-20T15:40:51Z.
Requested by billy
What
GetThreadContextcan fail on a target that was just suspended.RaiseOnused to throwsceKernelRaiseException: cannot read the target thread contextat the first failure. Now it resumes the target, yields, and samples again within the existing 1 sRetryLimit, 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_codealso 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
NtYieldExecutionredirect this PR first carried are gone. The limit is onmainthrough #8512. The yield redirect would run the guest handler withGuestMemory::GpuMutex()held, becauseWaitForLeasesAndImportsand the recorder teardown loops yield under it.Tested
Windows 11, Ryzen 7 7700, WinLibs GCC 15.2.0, Release,
main434e965d4, three runs.guest_raise_exceptionpasses in 12.4-13.2 s,guest_raise_in_host_codepasses with the longest raise at 5.4-10.6 ms, andguest_raise_nested_sleepandguest_mutex_raise_exceptionpass. The failure path itself isn't covered by a test:GetThreadContextfailing on a suspended thread can't be triggered on demand.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T15:47:07Z.
Thanks. The 100 ms host limit and the
GetThreadContextretry 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
NtYieldExecutionfromBlockedInWait. 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:WaitForLeasesAndImportsinlibSceAgcDriver/Graphics/src/GuestBufferMemory.cppcallsstd::this_thread::yield()insidestd::lock_guard lock(GuestMemory::GpuMutex()). That's the wait a guestmunmapormprotectof GPU-leased memory runs, andGuestAllocations.cppcalls 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 theNtYieldExecutionclause out of the debt entry.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.
core/libs/prx/libkernel/System/src/Exception.cppcore/libs/prx/libkernel/System/src/Exception.cppcore/libs/prx/libkernel/System/src/Exception.cppcore/libs/prx/libkernel/System/src/Exception.cppcore/libs/prx/libkernel/System/src/Exception.cppImported 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_exceptiontimeouts, 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 onmain, and this branch conflicts with it inException.cpp.The
GetThreadContextretry 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 currentmainand shrink this PR to just that retry, plus its test inGuestRaiseInHostCode.cpp? That would make it a small, easy review.One thing to leave out: the
NtYieldExecutionredirect. Our host code yields while holding the GPU mutex (for example the lease wait inGuestBufferMemory.cpp, which a guestmunmap/mprotectof 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.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
main434e965d4and shrunk this PR to theGetThreadContextretry plus the longest-raise output inguest_raise_in_host_code(e5b66f2dd, 2 files). TheNtYieldExecutionredirect is gone: you're right that the lease wait and the recorder teardown yield underGpuMutex(), 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, withguest_raise_exceptionat 12.4-13.2 s.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-22T04:22:31Z.
Thanks, and sorry this sat for a day.
e5b66f2ddis 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.