Repository navigation
fix(libkernel): keep sceKernelRaiseException pending on a suspended thread on Windows - #12963
Conversation
…hread on Windows On Windows, sceKernelRaiseException threw for a thread created suspended and not resumed yet. FreeBSD libthr starts such a thread with signals blocked and restores its mask on resume, so the signal stays pending and is delivered on that thread once it runs. The raise is now recorded in the thread's pendingException with a compare-exchange, and the thread exchanges it for -1 when it leaves the creation wait and delivers it to itself before its start routine. Both sides use one atomic, so a raise racing a resume is either taken by the thread or, after it started, delivered as for a running thread. Raises while pending are merged, as for a standard signal. guest_pthread_create_suspended now runs the deferred-delivery case on every host, with a second raise while the thread is suspended. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
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.
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.
|
rooklz
left a comment
There was a problem hiding this comment.
I checked this on a macOS host and did not build or run the Windows target. The PR's windows check 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 suspended load, then compare_exchange_strong(0 → signum) || pending > 0, against a thread that waits and then calls exchange(-1). To widen the race window I added a spin between the suspended load and the CAS. Results on macOS arm64:
| variant | result |
|---|---|
| this PR, -O2 | 50,000 raise-vs-resume races, 0 lost |
| this PR, ASan+UBSan | 20,000 races, 0 lost |
| this PR, TSan | 5,000 races, no reports |
thread exchanges 0 instead of -1 |
471–540 / 50,000 raises lost |
|| pending > 0 dropped |
1000 / 1000 sequential double raises run the handler on the suspended thread |
- The first mutant confirms the PR body's argument for the
-1sentinel. The test can't catch it because its raises are sequential. That seems acceptable to me, since catching it needs a timing window. - Your new second-raise row kills the second mutant.
- If two raises race a resume, they sometimes give two deliveries: the first one merges, and the second one sees
-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.- Merge order: #12130 and #12642 replace the
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.
|
Linux run at this PR's exact head Arch Linux, kernel 7.2.9, Ryzen 9 7950X, g++ (GCC) 16.2.1 20260810, glibc 2.44, x86-64. Release,
The change is aimed at Windows; this run shows the Linux build and |
|
Recorded the review’s integration constraint: future park-loop replacements from #12130 or #12642 must retain AI-assisted: yes (Codex). |
|
Checked the pending/resume protocol (CAS 0→signal while suspended, exchange to -1 after the creation wait, self-delivery through |
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)