fix(libkernel): stop losing and deadlocking sceKernelRaiseException deliveries on Windows #1421
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1421"
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 popcoder565 on 2026-10-08T03:48:19Z.
What
On Windows,
sceKernelRaiseExceptioncould lose a delivery: it returned 0, but the handler never ran on the target thread.guest_raise_exceptionhits this in its busy-thread phase in about one run in five onmain(9619edff, after #1375) and then hangs until the 30 s ctest timeout. Three paths lost deliveries, and a fourth deadlocked them.The handler's own return went through
NtContinue. A raise suspends the target, points its context atAps5RedirectedEntryStub, and the stub returns throughRtlRestoreContext. On Windows 10 (19045),RtlRestoreContextrestores in user mode, withiretq, only whenContextFlagswithoutCONTEXT_XSTATEis exactlyCONTEXT_FULL | CONTEXT_SEGMENTS(0x10000F, read from the ntdll disassembly); otherwise it callsNtContinue. The context was read withCONTEXT_FULL | CONTEXT_FLOATING_POINT(0x10000B), so every handler returned throughNtContinue. When the next raise suspended the thread inside that system call,SetThreadContextchanged the saved user context, andNtContinuethen replaced it with the context it was restoring, so the thread went back to its loop without running the handler.CONTEXT_FULL | CONTEXT_SEGMENTS, so the handler returns through the user-mode restore, which builds itsiretqframe below the saved context and never enters the kernel.Other code returns through
NtContinuetoo. An APC or an exception handler that continues execution resumes the thread throughNtContinueorNtContinueEx.The APC path was chosen before the thread had stopped.
SuspendThreadreturns before the thread is suspended;GetThreadContextwaits until it is.waitCountwas read right afterSuspendThread, so a thread leaving its wait could still decrement it and drain its APCs withNtTestAlertbeforeQueueUserAPCqueued the delivery, which then waited for an alertable wait that never came.waitCountis read from a stopped thread.A raise could interrupt the target inside winpthreads. MinGW's
thread_localgoes through emutls and winpthreads'pthread_getspecific, which holds a per-thread spinlock. A handler that reads athread_local, such asscePthreadSelfor theTimedWaitwait state, on a thread interrupted while it held that lock spun on it forever.libwinpthread-1.dllresumes it and suspends it again, as one insideNtContinuedoes.Both retries give up after 1 s. A target that stays inside those
NtContinuestubs or winpthreads for that long makes the raise throw, instead of spinning forever. Inguest_raise_exception, the longest retry took 7.4 ms.TechnicalDebt: a new entry records two things: a handler can still reenter other host code the thread was interrupted in, such as the C runtime's heap, and the raise gives up after 1 s. The YMM entry is gone since #1478, which saves the upper halves; with
mainmerged in, its vectors phase passes with this PR'sCONTEXT_FULL | CONTEXT_SEGMENTSread.How this relates to #1562
Since
07610212, #1562 no longer changesException.cpp. It now moves libkernel's handler-visible state (the current thread, pthread keys and theTimedWaitstate) to fiber-local storage, and makes contended mutex and rwlock waits interruptible on Windows. The two PRs complement each other: with #1562, libkernel calls from a handler no longer take winpthreads' TLS lock, and this PR's retry also covers athread_localin libc or the other prx modules.In this probe (not part of this PR), a worker loops on libc's
strerror, whose buffer is a MinGWthread_local, and the handler callsstrerrortoo:mainde12706aTested
Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, Intel Core i7-12700H.
guest_raise_exceptionfailed 2 of 5 runs on its own (ctest timeout), and a loop over the test binary hung on runs 12, 3 and 3. gdb on a hung process showed the main thread still waiting for the 9th delivery to the busy thread and the busy thread back in its loop. A 1 ms sleep takes 15.6 ms there, soExpectDelivery's 5000 attempts outlast the 30 s timeout instead of aborting.guest_raise_exception: a thread raises SEH exceptions in a loop and a vectored handler continues each one, so the thread spends much of its time inNtContinue. With the first change only, it hung in 4 of 4 runs; with the retry it passes.SuspendThreadbeing asynchronous, not from a measured before and after.SuspendThreadis asynchronous.scePthreadSelfin a loop while it is raised at, and the handler checks it sees its own thread. Without the winpthreads retry it hung in every run (3 of 3 with the final 100 rounds, 10 of 10 in an earlier version); with it, it passes.ctest -R ^guest_raise_exception$ --repeat until-fail:10passed all 10 runs (at most 8.7 s each, the 30 s timeout unchanged), and 20 of 20 runs passed with a 200-round version of the new phase.agc_driver_dcc_keys_first_write. That test fails without this change too (#1434) and never raises an exception.NtContinueand checks that the raise throws after 1 s without running the handler. The thread continues a context whoseRipisNtContinueand whoseRcxpoints at that same context, so each call restores itself.guest_raise_exceptionpassed 10 of 10 runs on the branch, and 10 of 10 merged ontomain63c57d22.main63c57d22: 452 of 454 pass. The failures areagc_driver_dcc_keys_first_writeandagc_driver_srgb8_color_target, the NVIDIA import flakes in #1434.No title tested.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T13:13:47Z.
Review: CHANGES_REQUESTED
Checked the diff, the
guest_raise_exceptionregistration (Windows only) and CI on the current head (windows, linux, conventions, report green; Linux ctest 457/457 locally, the test itself doesn't run there). Two things before merging:RaiseOn(Exception.cpp:219-243) has no bound: a thread that stays insideNtContinue/NtContinueExor winpthreads makes the raise spin onSwitchToThreadforever, a hang instead of an error. Please bound it (a retry count or a deadline) and throw when it runs out, with the test covering that path if it can be forced.Also, the third change (reading the context before
waitCount) has no before/after measurement; if you have one, add it to Tested.Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-08T13:56:44Z.
@adriadam10 thanks for the review. I pushed
24dc98d7and updated the description.guest_raise_exceptionphase forces this path deterministically. A thread keeps continuing a context that callsNtContinueagain, so it never leaves the stub. The raise must throw without running the handler.RaiseOnbrings back the first three problems: it returns throughNtContinue(it reads the context withCONTEXT_ALL), doesn't retry inside it, and readswaitCountright afterSuspendThread.PthreadPrivatemembers its head reads but never adds.guest_raise_exceptionstill timed out in 4 of 10 runs, and in 5 of 10 with this PR's phases.🤖 Addressed by Claude Code
Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-08T14:16:53Z.
The
linuxfailure isn't from this PR. It stops at the test-registration step before building:guest_formatting_tests is run by ctest now; remove it from KNOWN_UNRUN. #1435 started runningguest_formattingon Linux without updatingtools/check_test_registration.py, somainfails the same way. #1603 fixes it, and I'll re-run CI here once that lands.🤖 Addressed by Claude Code
Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T09:09:45Z.
I updated the comparison with #1562 in the description. Its current head
de12706abuilds and passes this PR's phases, apart from the NtContinue loop phase, which checks this PR's bound. One case still hangs with it: a handler that uses another module'sthread_local(here libc'sstrerrorbuffer) while the target was stopped inside winpthreads. That case hangs in 5 of 5 runs with #1562 and onmain, and passes 5 of 5 with this PR. So this PR's own parts are now the winpthreads retry and the 1 sNtContinuebound. I can rebase them onto #1562 if you prefer its delivery.🤖 Addressed by Claude Code
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-09T23:23:15Z.
Review: APPROVED
Checked the new bound (
Exception.cpp:230-255, 1 s deadline that throws), theguest_raise_exceptionphases covering it (NtContinue loop,scePthreadSelfin winpthreads, SEH continue) and the TechnicalDebt line; merges cleanly with currentmainand the Linux build and ctest are green (the test itself is Windows-only, so the Windows behaviour rests on your runs). Merging.