Skip to content

fix(libkernel): keep sceKernelRaiseException pending on a suspended thread on Windows - #12963

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
JJenkx:windows/libkernel-raise-pending-until-resume
Oct 30, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
JJenkx:windows/libkernel-raise-pending-until-resume

Conversation

@JJenkx

@JJenkx JJenkx commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

What

On Windows, sceKernelRaiseException throws (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 but SIGTRAP blocked and restores its mask only in thread_start once 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 what DefersRaiseUntilResume in guest_pthread_create_suspended checks on those hosts.

This makes Windows do the same:

  • sceKernelRaiseException: when the target is still suspended, it compare-exchanges the thread's pendingException (declared in PthreadPrivate but unused so far) from 0 to 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 after suspended.wait, the thread exchanges pendingException for -1. If a signal was pending, it delivers it to itself through the existing same-thread path of RaiseOn (RtlCaptureContext + Deliver). The handler is looked up at delivery, and a handler removed in between throws, as the POSIX Deliver aborts in that case.
  • scePthreadCreate resets pendingException to 0 with suspended.

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 -1 and redirects the now running thread. A plain flag plus a re-check of suspended would not be safe: suspended is stored with release only, so that version could lose a raise.

The test now runs DefersRaiseUntilResume on every host instead of the Windows-only RefusesRaiseWhileSuspended. It also raises twice while the thread is suspended and still expects exactly one handler call, on that thread, after resume. The sceKernelRaiseException entry in docs/dev/debt/libkernel.md now 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, on main bb04a4f1ea.

  • guest_pthread_create_suspended passes 10 of 10 (ctest --repeat until-fail:10), and 10 of 10 again after the final rebuild.
  • Negative control: with Exception.cpp, Thread.cpp and Pthread.hpp from main and this test, it fails with sceKernelRaiseException: the target thread is still suspended at creation.
  • Related tests: ctest -R 'raise|exception|pthread|thread|signal|suspend' -j8 --repeat until-fail:3 covers 149 tests. 148 pass. guest_raise_exception failed once under that parallel load, so I ran it alone 8 times on each side:
build passes failure
main bb04a4f1ea 7 of 8 run 3: address waking round 2: the handler did not run within 5 s
this PR 7 of 8 run 6: address waking round 5: the handler did not run within 5 s

Both sides show the same pre-existing flake (#11813), at the same rate.

  • python tools/check_conventions.py --base origin/main: all checks passed.
  • Not run here: Linux and macOS. Their code is unchanged. Only the test changed for them: the second raise while suspended, where a blocked standard signal is merged and delivered once.

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

  • 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)

…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>
@adriadam10 adriadam10 self-assigned this Oct 29, 2026
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

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.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 8477 core/libs/prx/libkernel/Pthread/src/Thread.cpp L804-805
PR 9938 core/libs/prx/libkernel/Pthread/src/Thread.cpp L983
PR 12130 core/libs/prx/libkernel/Pthread/src/Thread.cpp L803-804
PR 12135 docs/dev/debt/libkernel.md L212-213
PR 12642 core/libs/prx/libkernel/Pthread/src/Thread.cpp L803-804

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 3986 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 4563 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 4630 core/libs/prx/libkernel/System/src/Exception.cpp
docs/dev/debt/libkernel.md
PR 5021 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/System/src/Exception.cpp
docs/dev/debt/libkernel.md
PR 6936 docs/dev/debt/libkernel.md
PR 7088 docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7121 docs/dev/debt/libkernel.md
PR 7135 docs/dev/debt/libkernel.md
PR 7149 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 7192 core/libs/prx/libkernel/System/src/Exception.cpp
docs/dev/debt/libkernel.md
PR 7447 docs/dev/debt/libkernel.md
PR 7464 docs/dev/debt/libkernel.md
PR 7509 docs/dev/debt/libkernel.md
PR 7804 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 8592 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/System/src/Exception.cpp
PR 8605 docs/dev/debt/libkernel.md
PR 9156 docs/dev/debt/libkernel.md
PR 9242 docs/dev/debt/libkernel.md
PR 9293 docs/dev/debt/libkernel.md
PR 9297 docs/dev/debt/libkernel.md
PR 9302 docs/dev/debt/libkernel.md
PR 9326 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
docs/dev/debt/libkernel.md
PR 9389 docs/dev/debt/libkernel.md
PR 9557 docs/dev/debt/libkernel.md
PR 9566 docs/dev/debt/libkernel.md
PR 9843 docs/dev/debt/libkernel.md
PR 10442 docs/dev/debt/libkernel.md
PR 10552 docs/dev/debt/libkernel.md
PR 10645 docs/dev/debt/libkernel.md
PR 11002 docs/dev/debt/libkernel.md
PR 11215 docs/dev/debt/libkernel.md
PR 11661 core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/System/src/Exception.cpp
docs/dev/debt/libkernel.md
PR 11689 core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/System/src/Exception.cpp
docs/dev/debt/libkernel.md
PR 11698 core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/System/src/Exception.cpp
docs/dev/debt/libkernel.md
PR 11827 docs/dev/debt/libkernel.md
PR 11939 docs/dev/debt/libkernel.md
PR 12104 docs/dev/debt/libkernel.md
PR 12560 docs/dev/debt/libkernel.md
PR 12667 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
PR 12741 docs/dev/debt/libkernel.md
PR 12854 docs/dev/debt/libkernel.md
PR 12970 docs/dev/debt/libkernel.md

@rooklz rooklz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-tree against bb04a4f1ea is clean.
  • git diff --check is clean.
  • check_conventions --base origin/main passes.
  • x86_64-w64-mingw32-g++ 16.2 -std=c++20 -fsyntax-only reports 0 errors for Exception.cpp, Thread.cpp and GuestPthreadCreateSuspended.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 -1 sentinel. 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 -1 and 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, ...) in scePthreadCreate is redundant. p is a fresh make_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 in RunThread with their own park loop. pr_overlap reports Thread.cpp L803-805. Whichever of those lands second needs to keep DeliverPendingException_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.

@JJenkx

JJenkx commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

Linux run at this PR's exact head db14f7a49d (db14f7a49d5ad965e1bd5a076d2ef792782368d2), not merged onto anything else; its base on main is bb04a4f1ea.

Arch Linux, kernel 7.2.9, Ryzen 9 7950X, g++ (GCC) 16.2.1 20260810, glibc 2.44, x86-64. Release, BUILD_TESTING=ON, Ninja -j4, private worktree.

  • ctest -R '^guest_pthread_create_suspended$': passes in each of 3 runs (3 passes).

The change is aimed at Windows; this run shows the Linux build and guest_pthread_create_suspended still pass at this head. The Windows behavior is not exercised here.

@JJenkx

JJenkx commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

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).

@adriadam10
adriadam10 merged commit 5ba6b2b into boykopovar:main Oct 30, 2026
13 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the pending/resume protocol (CAS 0→signal while suspended, exchange to -1 after the creation wait, self-delivery through RaiseOn), the removed-handler throw, the debt entry and the test now running on every host with the double raise; Windows CI green; merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants