fix(libkernel): keep sceKernelRaiseException pending on a suspended thread on Windows #12963
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12963"
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 JJenkx on 2026-10-29T17:40:45Z.
What
On Windows,
sceKernelRaiseExceptionthrows (the target thread is still suspended at creation) for a thread created suspended and not resumed yet. FreeBSD 11.4 libthr starts such a thread with every signal butSIGTRAPblocked and restores its mask only inthread_startonce it is resumed, so the signal stays pending and is delivered on that thread before its start routine. That is what Linux and macOS already do here, through the blocked host mask, and whatDefersRaiseUntilResumeinguest_pthread_create_suspendedchecks on those hosts.This makes Windows do the same:
sceKernelRaiseException: when the target is stillsuspended, it compare-exchanges the thread'spendingException(declared inPthreadPrivatebut unused so far) from0to the signal and returns 0. If a raise is already pending, the new one merges into it, as for a standard signal. If the value is-1, the thread has already left its creation wait, and the raise is delivered as for any running thread.RunThread: right aftersuspended.wait, the thread exchangespendingExceptionfor-1. If a signal was pending, it delivers it to itself through the existing same-thread path ofRaiseOn(RtlCaptureContext+Deliver). The handler is looked up at delivery, and a handler removed in between throws, as the POSIXDeliveraborts in that case.scePthreadCreateresetspendingExceptionto 0 withsuspended.A raise racing a resume is safe because both sides do a read-modify-write on the same atomic, so they are totally ordered. Either the thread takes the pending raise, or the raiser sees
-1and redirects the now running thread. A plain flag plus a re-check ofsuspendedwould not be safe:suspendedis stored with release only, so that version could lose a raise.The test now runs
DefersRaiseUntilResumeon every host instead of the Windows-onlyRefusesRaiseWhileSuspended. It also raises twice while the thread is suspended and still expects exactly one handler call, on that thread, after resume. ThesceKernelRaiseExceptionentry indocs/dev/debt/libkernel.mdnow describes the pending behaviour, and the console behaviour stays marked unverified.Tested
Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release,
BUILD_TESTING=ON, no Vulkan driver visible, onmainbb04a4f1ea.guest_pthread_create_suspendedpasses 10 of 10 (ctest --repeat until-fail:10), and 10 of 10 again after the final rebuild.Exception.cpp,Thread.cppandPthread.hppfrommainand this test, it fails withsceKernelRaiseException: the target thread is still suspended at creation.ctest -R 'raise|exception|pthread|thread|signal|suspend' -j8 --repeat until-fail:3covers 149 tests. 148 pass.guest_raise_exceptionfailed once under that parallel load, so I ran it alone 8 times on each side:mainbb04a4f1eaaddress waking round 2: the handler did not run within 5 saddress waking round 5: the handler did not run within 5 sBoth sides show the same pre-existing flake (#11813), at the same rate.
python tools/check_conventions.py --base origin/main: all checks passed.Future integration constraint — 2026-10-30
Recorded the review’s integration constraint: future park-loop replacements from #12130 or #12642 must retain
DeliverPendingException_nid_no_patch(self)after the wait. Their new waiting mechanism must not bypass pending callback delivery. This is a preservation requirement for future composition, not a claim that those branches were merged or tested together, and it does not create an implementation prerequisite for this standalone change. The current source is unchanged.AI-assisted: yes (Codex).
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)Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T18:55:06Z.
Review: APPROVED
I checked this on a macOS host and did not build or run the Windows target. The PR's
windowscheck passes.Gates
git merge-treeagainstbb04a4f1eais clean.git diff --checkis clean.check_conventions --base origin/mainpasses.x86_64-w64-mingw32-g++16.2-std=c++20 -fsyntax-onlyreports 0 errors forException.cpp,Thread.cppandGuestPthreadCreateSuspended.cpp.Ordering. I copied the raise/resume protocol into a standalone harness: the
suspendedload, thencompare_exchange_strong(0 → signum) || pending > 0, against a thread that waits and then callsexchange(-1). To widen the race window I added a spin between thesuspendedload and the CAS. Results on macOS arm64:0instead of-1|| pending > 0dropped-1sentinel. The test can't catch it because its raises are sequential. That seems acceptable to me, since catching it needs a timing window.-1and redirects. That matches POSIX, where a signal that arrives after the mask is restored is a new delivery.Notes (non-blocking)
p->pendingException.store(0, ...)inscePthreadCreateis redundant.pis a freshmake_unique<PthreadPrivate>, and the member defaults to{0}. It's harmless, so drop it only if you want.self->suspended.wait(true, ...)line inRunThreadwith their own park loop.pr_overlapreports Thread.cpp L803-805. Whichever of those lands second needs to keepDeliverPendingException_nid_no_patch(self)after the wait, or Windows goes back to losing the pending raise. #12135 touches the adjacent debt lines L207-208.Approving.