Skip to content

fix(libkernel): keep the signal mask per thread - #3986

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
Silverash-i8:fix/per-thread-signal-mask
Nov 6, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
Silverash-i8:fix/per-thread-signal-mask

Conversation

@Silverash-i8

@Silverash-i8 Silverash-i8 commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

What

Fixes #2879. sigprocmask, _sigprocmask, pthread_sigmask and sigsuspend share one process-wide blocked mask: a thread changing its mask changes every thread's, and scePthreadCreate does not hand the creating thread's mask to the new thread. Rebased onto current main and reduced to that, as asked in the reviews of 13 and 22 October. The 128-bit mask, SIGKILL and SIGSTOP never blocked, EINVAL for an unknown how and the process-wide pending set are already on main and are not touched.

  • Each guest thread has its own blocked mask and handler holds (ThreadSignals in Signals.cpp); sigprocmask, _sigprocmask, pthread_sigmask, sigsuspend, sigpending and the delivery checks (raise, Dispatch, pause, the unblock path) read the calling thread's.
  • scePthreadCreate starts the new thread with its creator's mask.
  • The pending set stays on main's structure (MarkPending, TakePending, PendingInSet). A signal that raise leaves pending is still seen by sigwait, sigtimedwait and sigwaitinfo on any thread, and is delivered by the first thread that unblocks it, as a process-directed signal is in FreeBSD.
  • Which mask is consulted where the caller is not the signalled thread: the SIGALRM expiry runs on the timer thread, which is not a guest thread, so it checks the mask of the thread that armed the timer (alarm, setitimer); a host signal from outside the guest (Ctrl+C, SIGTERM from another process) is checked against the host thread that receives it. FreeBSD would deliver either to any thread that does not block it. Both are recorded in docs/dev/debt/libkernel.md with the pthread_sigmask, sigpending, sigsuspend and setitimer entries.
  • A thread's mask state is not freed when the thread exits (a thread-local object with a destructor is avoided because of the MinGW TLS teardown problem in agc_driver_tests: rare heap-corruption exit (0xc0000374) on Windows remains after #2976 #3171). Only the arming thread's state is read from another thread, so an expiry for a timer armed by a thread that exited with SIGALRM blocked stays pending.

Tests: guest_signals now checks a child inheriting its creator's mask and changing its own without touching the creator's, a signal raised while blocked being taken by sigtimedwait on a child thread, and a child that unblocks a signal the creator still blocks running the handler. The three CheckOtherThread*Kept checks in guest_signal_handler_mask asserted the process-wide behaviour (another thread's change showing up in this thread's mask); they now assert that the other thread's change leaves this thread's mask alone.

Tested

On main 03cfa4c067:

  • Windows 11 Home 26300, WinLibs GCC 15.2.0 r7, Release: guest_signals, guest_signal_sets, guest_signal_handler_mask, guest_signal_interrupt, guest_signal_list, guest_sigtimedwait, guest_itimer (with _pending_default and _short_expiry), guest_getitimer, guest_legacy_signal_mask (and _forwarding), guest_user_context (and _exit), guest_sigaltstack, guest_kernel_sys_pselect and guest_sigvec pass 18 of 18 five times in a row (--repeat until-fail:5).
  • Full suite except agc_driver*: everything passes except test_registration and select_tests_MissingCommandTests (both need the Windows symlink privilege, WinError 1314) and guest_video_out_high_frame_rate, which skips when run alone and failed once under -j8 load; none touches signals.
  • Linux (Ubuntu 24.04 under WSL2, GCC 13.3.0, Release): guest_signals, guest_signal_sets, guest_signal_handler_mask, guest_signal_interrupt, guest_sigtimedwait, guest_itimer (with _pending_default and _short_expiry), guest_getitimer, guest_legacy_signal_mask (and _forwarding), guest_user_context (and _exit) and guest_kernel_sys_pselect pass 14 of 14. Not run on native Linux, and the control run against main's sources was done on Windows only.
  • Against main's Signals.cpp and Thread.cpp, with this PR's tests kept, guest_signals and guest_signal_handler_mask fail on this machine (exit 0xc0000409).
  • tools/check_conventions.py --base upstream/main: 0 errors (two debt-length warnings on existing entries).
  • Not tested: titles.

Overlaps: #4138 touches the same sigprocmask lines, and draft #11273 (signal calls EFAULT) the neighbouring ones; whichever lands first decides the order.

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: Fixes libkernel: blocked signal masks are shared between threads instead of inherited per thread #2879
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

https://claude.ai/code/session_012SLQbhAiexxW6TtjYToW8a

@Kikoarl

Kikoarl commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Tested on native Linux (CachyOS x86_64, GCC) with an NVIDIA GeForce RTX 5060 Ti:

  • guest_signals: ran 10 consecutive stress runs (--repeat until-fail:10) — 10/10 passed (15.05s total).
  • guest_signal_sets & guest_user_context_exit pass clean.
  • Verified in live title execution (Astro Bot PPSA21567): 70+ worker threads spawned via scePthreadCreate correctly inherit masks with zero deadlocks or regressions.
  • tools/check_conventions.py passes clean.

Validates the fix on native Linux and title execution as requested in the description. LGTM!

@oneandonlydean

Copy link
Copy Markdown
Collaborator

One thing before merging: please drop the comment at core/libs/prx/libkernel/System/src/Signals.cpp:47-48. Code comments aren't allowed apart from technical-debt markers; the TechnicalDebt entry already says it.

@Dextroid17 Dextroid17 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.

The control is what the record above does not have, and the overlap the bot lists is a text conflict rather than a same-file warning. Head 066b18da0, parent 0a8b1d32b, in a worktree:

head      guest_signal + guest_signal_sets → 100% tests passed out of 2, exit 0
control   parent's Pthread/src/Thread.cpp + System/src/Signals.cpp, the PR's tests kept
          → the test process exits 134 (SIGABRT, "Aborted (core dumped)"),
            ctest: 691 - guest_signals (Subprocess aborted), 50% passed, 1 failed out of 2 (ctest exit 8)

The abort is deterministic and comes from the parent's own bookkeeping: it wrote previousSet->bits[1..3] = 0, while the new test asserts the previous mask above bit 31 comes back set (previous.bits[1] == 0x1, with bit 32 reaching bits[2] and bits[3]). That is the half a green head cannot show.

Two checks on the change itself:

  • The mechanism fits this port. GuestSignalSet has held std::uint32_t bits[4] (Signals.cpp:24) since before this change; what was wrong is the mask bookkeeping — a single std::atomic<std::uint32_t> blockedMask, the operation applied to set->bits[0] only, and bits[1..3] returned as zero, so a mask above 32 was dropped and never read back. The change keeps the blocked and pending sets per thread and applies the operation to all four words. Per-thread storage is meaningful here because each guest thread runs on its own host thread: pthread_create(&p->hostThread, …, StartNativeThread, …) on the POSIX path (Thread.cpp:586), _beginthreadex on the Windows path (Thread.cpp:549), the entered thread adopted through pthread_self() (Thread.cpp:692).
  • On the overlap, the bot's table is a same-file, same-line-range warning computed against main and says nothing about whether the two branches can merge; the measured state is that they cannot be merged without a decision. git merge-tree --write-tree pr-3986 pr-4138 exits 1 with CONFLICT (content) in core/libs/prx/libkernel/System/src/Signals.cpp, in three regions — the blocked check in Dispatch, raise_nid_postfix, and _sigprocmask_nid_postfix — where both branches add the same three how values under differently capitalised names (GuestSigBlock/GuestSigUnblock/GuestSigSetmask against GuestSigblock/GuestSigunblock/GuestSigsetmask, the second side also adding GuestEintr, GuestEagain and GuestSiuser) and both add pending-set handling. pr-3986 with pr-4383 and pr-4138 with pr-4383 produce trees with no textual conflict (exit 0; neither result was built or run, so that is all it shows). #4138's body already says it will keep this PR's per-thread masks and drop its own duplicate pending handling, so the conflict makes that rebase required rather than optional, and #4383 can go either side of the pair without cost. A line recording that would spare the next reader the experiment.

The comment oneandonlydean asked to be dropped sits at Signals.cpp:46-47 on this head (his range says 47-48); nothing above depends on it — and if the overlap is worth recording in the repository, docs/dev/TechnicalDebt.md in the same pull request is the place for it.

Written with AI assistance.

Hermes Agent 🤖 at your service

@xXPBSXx

xXPBSXx commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

A small observation on the how handling in sigprocmask / _sigprocmask: the PR keeps throw std::invalid_argument for a how other than SIG_BLOCK, SIG_UNBLOCK and SIG_SETMASK. A throw out of a guest call ends the process, while FreeBSD 11.4's kern_sigprocmask writes the old mask first and then fails the unknown how with EINVAL (22). Returning EINVAL there would match it. On current main I checked this on Windows: sigprocmask with how 0, 4 and -1 and a non-null set aborts with the uncaught exception. I have a small test for it (guest_signals, how 0/4/-1) but did not open a PR, because this one rewrites the same lines; feel free to take it.

AI-assisted (Claude Code).

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for this, the per-thread mask is the right model. It no longer applies to main, and the conflict is not only textual:

  • main now keeps a process-wide pending set in Signals.cpp (MarkPending/TakePending/PendingInSet, added with sigtimedwait in #3430, used by sigwait, sigwaitinfo, pause and killpg). The per-thread pendingSignals here is a second store for the same thing: a signal that raise leaves in it is never seen by sigwait/sigtimedwait. Please rebase and decide how the two relate (for example blocked mask per thread, pending set kept on main's structure), then cover the interaction with a test.
  • ExpireAlarm (setitimer/alarm, #3800) reads blockedMask for SIGALRM and SignalBlocked only looks at the first 32 signals. Both need to use the new per-thread mask, and the alarm thread is not a guest thread, so say which mask it checks.
  • docs/dev/TechnicalDebt.md was split per library: the entry now lives in docs/dev/debt/libkernel.md, where the old pthread_sigmask line (line 123) and the sigtimedwait line (149, which describes the process-wide pending set) both need updating.
  • @oneandonlydean's request is still open: drop the comment at Signals.cpp:47-48.
  • @xXPBSXx's point about an unknown how (write the old mask, return EINVAL instead of throw, per FreeBSD 11.4 kern_sigprocmask) touches the same lines you rewrite, so it fits in this PR.

After the rebase, guest_signals, guest_signal_sets, the sigtimedwait tests and guest_itimer should pass together, with the new guest_signals still failing on main's Signals.cpp/Thread.cpp.

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

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 907 docs/dev/TechnicalDebt.md
PR 1368 docs/dev/TechnicalDebt.md
PR 1384 docs/dev/TechnicalDebt.md
PR 1395 docs/dev/TechnicalDebt.md
PR 1601 docs/dev/TechnicalDebt.md
PR 1835 docs/dev/TechnicalDebt.md
PR 4563 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 5021 core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/TechnicalDebt.md
PR 7149 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 7532 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 7565 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 7622 core/libs/prx/libkernel/System/src/Signals.cpp
PR 7804 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 8041 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 8497 core/libs/prx/libkernel/System/src/Signals.cpp
PR 8592 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 9389 core/libs/prx/libkernel/System/src/Signals.cpp
PR 9684 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 9938 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 10749 core/libs/prx/libkernel/System/src/Signals.cpp
core/libs/tests/GuestSignals.cpp
PR 11002 core/libs/prx/libkernel/System/src/Signals.cpp
core/libs/tests/GuestSignals.cpp
PR 11624 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 11628 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 11661 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 11689 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 11698 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 12130 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 12170 core/libs/prx/libkernel/System/src/Signals.cpp
core/libs/tests/GuestSignals.cpp
PR 12544 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 12642 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 12682 core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/System/src/Signals.cpp
PR 12688 core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/System/src/Signals.cpp
PR 12700 core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/System/src/Signals.cpp
PR 12965 core/libs/prx/libkernel/Pthread/src/Thread.cpp
core/libs/prx/libkernel/System/src/Signals.cpp
PR 13582 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 14085 docs/dev/TechnicalDebt.md
PR 14190 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 14212 docs/dev/TechnicalDebt.md
PR 14402 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 14886 core/libs/prx/libkernel/System/src/Signals.cpp
PR 15040 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 15079 docs/dev/TechnicalDebt.md

@Gruzzberry

Copy link
Copy Markdown
Contributor

Draft #11273 (signal calls EFAULT) touches neighbouring lines. Proposal: yours first, mine rebases.

@sherySJ

sherySJ commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

@Silverash-i8 are you still working on this? Main has moved under it: Signals.cpp, GuestSignals.cpp, Thread.cpp and the debt entry (now docs/dev/debt/libkernel.md) conflict, and the 13 and 22 October reviews are open.

If you are not, I would like to carry it as a separate PR with your commit kept and your authorship intact, rebased onto current main and reduced to what main still lacks: a blocked mask per guest thread, started from the creating thread's mask in scePthreadCreate, with the pending set staying on main's MarkPending/TakePending/PendingInSet so sigwait and sigtimedwait still see a signal that raise leaves pending. I would also state which mask SignalBlocked and the ExpireAlarm thread consult, update the pthread_sigmask and sigtimedwait debt lines, drop the comment at Signals.cpp:47-48, and show guest_signals, guest_signal_sets, the sigtimedwait tests and guest_itimer together, with the new guest_signals failing on main. I would not close this PR. If you push your own rebase first I will drop mine.

@xXPBSXx you offered a carry here on 24 October; I see no branch or PR since. Are you still on it? If so I will stay out of the way, otherwise I will start in 48 hours.

@Silverash-i8

Copy link
Copy Markdown
Contributor Author

Yes, I'm still on this, sorry for the silence. I was away for a few weeks and only caught up on the thread today.

I'm redoing it on current main as asked: a blocked mask per thread inherited from the creating thread in scePthreadCreate, main's MarkPending/TakePending/PendingInSet left as the pending set so sigwait and sigtimedwait still see what raise leaves pending, the code comment gone, the debt lines moved to docs/dev/debt/libkernel.md, and a test for the pending set interaction. I'll also say in the description which mask ExpireAlarm and host delivered signals consult.

I expect to push the rebased head within a day or so. xXPBSXx and sherySJ, thank you for offering to carry it. If you'd rather not wait, say so here and I'll check my branch against yours, but I'd like to finish it myself if that's fine with everyone.

AI-assisted (Claude Code); I'll run the tests on my machine before pushing.

@sherySJ

sherySJ commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Thanks, that covers it. Please go ahead; I will not open a carry. Happy to run the guest_signals and guest_itimer tests on Linux against your new head once it is up, if that helps.

The blocked mask of sigprocmask, _sigprocmask, pthread_sigmask and
sigsuspend was one process-wide set, so a thread changing its mask changed
every thread's, and scePthreadCreate did not hand the creating thread's mask
to the new thread. Each guest thread now has its own blocked mask and handler
holds, and a new thread starts with its creator's mask. The pending set stays
the process-wide one, so a signal that raise leaves pending is still taken by
sigwait and sigtimedwait on any thread and delivered by the first thread that
unblocks it. A SIGALRM expiry checks the mask of the thread that armed the
timer.

Fixes boykopovar#2879.

Claude-Session: https://claude.ai/code/session_012SLQbhAiexxW6TtjYToW8a
@Silverash-i8
Silverash-i8 force-pushed the fix/per-thread-signal-mask branch from 066b18d to 694b2b4 Compare November 5, 2026 17:21
@Silverash-i8

Copy link
Copy Markdown
Contributor Author

The rebased head is up (694b2b4704, one commit on current main). It keeps only what main still lacked: a blocked mask per guest thread, inherited from the creating thread in scePthreadCreate, with the pending set left on main's MarkPending/TakePending/PendingInSet so sigwait and sigtimedwait still see what raise leaves pending.

The two questions from the reviews: the SIGALRM expiry checks the mask of the thread that armed the timer, and a host signal such as Ctrl+C is checked against the host thread that receives it. Both are written up in the pthread_sigmask entry in docs/dev/debt/libkernel.md, with the sigpending, sigsuspend and setitimer lines updated. The code comment is gone. The description lists the tests I ran: the signal tests on Windows five times in a row, the same set on Linux (GCC 13.3), and guest_signals and guest_signal_handler_mask failing against main's Signals.cpp and Thread.cpp.

sherySJ, thanks, a Linux run of guest_signals and guest_itimer on your machine against this head would be welcome. Note that three CheckOtherThread*Kept checks in guest_signal_handler_mask asserted the old process-wide behaviour, so I changed them to assert that another thread's mask change leaves this thread's alone.

AI-assisted (Claude Code); I ran the above on my machine.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 5, 2026
@sherySJ

sherySJ commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

@Silverash-i8 Linux run of head 694b2b4 (one commit on current main), Omarchy (Arch) x86_64, g++ 16.2.1, Release, ccache, ctest --repeat until-fail:5:

test 5 runs time per run
guest_signals 5/5 pass 1.6 s
guest_itimer 5/5 pass 0.85 s
guest_signal_handler_mask 5/5 pass 0.01 s
guest_signal_sets 5/5 pass 0.01 s
guest_sigtimedwait 5/5 pass 0.5 s
guest_signal_interrupt 5/5 pass 0.01 s
guest_itimer_pending_default 5/5 pass 0.16 s
guest_itimer_short_expiry 5/5 pass 0.08 s
guest_getitimer 5/5 pass 1.1 s
guest_signal_list 5/5 pass 0.01 s

Negative control: with Signals.cpp and Thread.cpp restored to the merge base (03cfa4c) over your head, guest_signals and guest_signal_handler_mask both abort (SIGABRT, inside raise_nid_postfix). Restoring your versions makes both pass again.

Windows not tested.

AI-assisted: yes.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Hi @adriadam10, the author has rewritten this on current main (694b2b4704), and I read it against the six points in your 13 Oct review. Each looks met to me:

  1. Per-thread mask, pending set on main's structure: the blocked mask and handler holds are now per thread (ThreadSignals, core/libs/prx/libkernel/System/src/Signals.cpp:49-57), and PendingSignals (Signals.cpp:119) is main's process-wide set, unchanged, so a raised blocked signal is still visible to sigwait/sigtimedwait.
  2. Which mask is consulted: SignalBlocked(int) uses the calling thread (Signals.cpp:117). alarm and setitimer record the arming thread (Signals.cpp:471, :574), and ExpireAlarm checks that thread's mask rather than the alarm thread's (Signals.cpp:400, :403). docs/dev/debt/libkernel.md:242 spells this out, including host signals using the receiving host thread's mask.
  3. Debt docs: docs/dev/debt/libkernel.md updates setitimer (193), pthread_sigmask (242), sigpending (249) and sigsuspend (326). The sigtimedwait entry (325) is unchanged, and its process-wide pending set is still accurate.
  4. The code comment at the old Signals.cpp:47-48 is gone, and the PR adds no comments.
  5. No throw handling: main's EINVAL for an unknown how is kept as is (Signals.cpp:593).
  6. Tests on top of main's guest_signals: core/libs/tests/GuestSignals.cpp:217-249 checks creator-mask inheritance and per-thread isolation (the parent's mask stays 1 << 13 after the child sets 0x2, which fails on main's process-wide mask), plus a child taking and unblocking a process-pending signal. GuestSignalHandlerMask.cpp's other-thread checks now expect the handler thread's mask unchanged.

Could you take another look when you have time, and update or dismiss your review if you agree? Thanks.

@oneandonlydean
oneandonlydean dismissed adriadam10’s stale review November 6, 2026 19:28

The author rewrote this on current main (694b2b4). Our re-check of all six points from the 13 Oct review found each met at that head (point-by-point list in #3986 (comment)). No reply in 24 h, so we are dismissing it as stale. Happy to reopen if anything still looks wrong.

@oneandonlydean
oneandonlydean merged commit d85fad4 into boykopovar:main Nov 6, 2026
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs fix error Closes an issue (Fixes #N) libkernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

libkernel: blocked signal masks are shared between threads instead of inherited per thread

9 participants