fix(libkernel): keep the signal mask per thread #3986
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3986"
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 Silverash-i8 on 2026-10-11T20:39:12Z.
What
Fixes #2879.
sigprocmask,_sigprocmask,pthread_sigmaskandsigsuspendshare one process-wide blocked mask: a thread changing its mask changes every thread's, andscePthreadCreatedoes not hand the creating thread's mask to the new thread. Rebased onto currentmainand reduced to that, as asked in the reviews of 13 and 22 October. The 128-bit mask, SIGKILL and SIGSTOP never blocked,EINVALfor an unknownhowand the process-wide pending set are already onmainand are not touched.ThreadSignalsinSignals.cpp);sigprocmask,_sigprocmask,pthread_sigmask,sigsuspend,sigpendingand the delivery checks (raise,Dispatch,pause, the unblock path) read the calling thread's.scePthreadCreatestarts the new thread with its creator's mask.main's structure (MarkPending,TakePending,PendingInSet). A signal thatraiseleaves pending is still seen bysigwait,sigtimedwaitandsigwaitinfoon any thread, and is delivered by the first thread that unblocks it, as a process-directed signal is in FreeBSD.SIGALRMexpiry 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,SIGTERMfrom 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 indocs/dev/debt/libkernel.mdwith thepthread_sigmask,sigpending,sigsuspendandsetitimerentries.SIGALRMblocked stays pending.Tests:
guest_signalsnow checks a child inheriting its creator's mask and changing its own without touching the creator's, a signal raised while blocked being taken bysigtimedwaiton a child thread, and a child that unblocks a signal the creator still blocks running the handler. The threeCheckOtherThread*Keptchecks inguest_signal_handler_maskasserted 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
main03cfa4c067:guest_signals,guest_signal_sets,guest_signal_handler_mask,guest_signal_interrupt,guest_signal_list,guest_sigtimedwait,guest_itimer(with_pending_defaultand_short_expiry),guest_getitimer,guest_legacy_signal_mask(and_forwarding),guest_user_context(and_exit),guest_sigaltstack,guest_kernel_sys_pselectandguest_sigvecpass 18 of 18 five times in a row (--repeat until-fail:5).agc_driver*: everything passes excepttest_registrationandselect_tests_MissingCommandTests(both need the Windows symlink privilege,WinError 1314) andguest_video_out_high_frame_rate, which skips when run alone and failed once under-j8load; none touches signals.guest_signals,guest_signal_sets,guest_signal_handler_mask,guest_signal_interrupt,guest_sigtimedwait,guest_itimer(with_pending_defaultand_short_expiry),guest_getitimer,guest_legacy_signal_mask(and_forwarding),guest_user_context(and_exit) andguest_kernel_sys_pselectpass 14 of 14. Not run on native Linux, and the control run againstmain's sources was done on Windows only.main'sSignals.cppandThread.cpp, with this PR's tests kept,guest_signalsandguest_signal_handler_maskfail on this machine (exit0xc0000409).tools/check_conventions.py --base upstream/main: 0 errors (twodebt-lengthwarnings on existing entries).Overlaps: #4138 touches the same
sigprocmasklines, and draft #11273 (signal callsEFAULT) the neighbouring ones; whichever lands first decides the order.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)https://claude.ai/code/session_012SLQbhAiexxW6TtjYToW8a
Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-11T21:21:04Z.
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_exitpass clean.scePthreadCreatecorrectly inherit masks with zero deadlocks or regressions.tools/check_conventions.pypasses clean.Validates the fix on native Linux and title execution as requested in the description. LGTM!
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T23:20:07Z.
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.Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T16:00:48Z.
Review: COMMENTED
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, parent0a8b1d32b, in a worktree: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 reachingbits[2]andbits[3]). That is the half a green head cannot show.Two checks on the change itself:
GuestSignalSethas heldstd::uint32_t bits[4](Signals.cpp:24) since before this change; what was wrong is the mask bookkeeping — a singlestd::atomic<std::uint32_t> blockedMask, the operation applied toset->bits[0]only, andbits[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),_beginthreadexon the Windows path (Thread.cpp:549), the entered thread adopted throughpthread_self()(Thread.cpp:692).mainand 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-4138exits 1 withCONFLICT (content)incore/libs/prx/libkernel/System/src/Signals.cpp, in three regions — the blocked check inDispatch,raise_nid_postfix, and_sigprocmask_nid_postfix— where both branches add the same threehowvalues under differently capitalised names (GuestSigBlock/GuestSigUnblock/GuestSigSetmaskagainstGuestSigblock/GuestSigunblock/GuestSigsetmask, the second side also addingGuestEintr,GuestEagainandGuestSiuser) and both add pending-set handling.pr-3986withpr-4383andpr-4138withpr-4383produce 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-47on 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.mdin the same pull request is the place for it.Written with AI assistance.
Hermes Agent 🤖 at your service
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T07:07:49Z.
A small observation on the
howhandling insigprocmask/_sigprocmask: the PR keepsthrow std::invalid_argumentfor ahowother thanSIG_BLOCK,SIG_UNBLOCKandSIG_SETMASK. A throw out of a guest call ends the process, while FreeBSD 11.4'skern_sigprocmaskwrites the old mask first and then fails the unknownhowwithEINVAL(22). ReturningEINVALthere would match it. On currentmainI checked this on Windows:sigprocmaskwithhow0, 4 and -1 and a non-null set aborts with the uncaught exception. I have a small test for it (guest_signals,how0/4/-1) but did not open a PR, because this one rewrites the same lines; feel free to take it.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T10:14:11Z.
Review: DISMISSED
Thanks for this, the per-thread mask is the right model. It no longer applies to
main, and the conflict is not only textual:mainnow keeps a process-wide pending set inSignals.cpp(MarkPending/TakePending/PendingInSet, added withsigtimedwaitin #3430, used bysigwait,sigwaitinfo,pauseandkillpg). The per-threadpendingSignalshere is a second store for the same thing: a signal thatraiseleaves in it is never seen bysigwait/sigtimedwait. Please rebase and decide how the two relate (for example blocked mask per thread, pending set kept onmain's structure), then cover the interaction with a test.ExpireAlarm(setitimer/alarm, #3800) readsblockedMaskforSIGALRMandSignalBlockedonly 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.mdwas split per library: the entry now lives indocs/dev/debt/libkernel.md, where the oldpthread_sigmaskline (line 123) and thesigtimedwaitline (149, which describes the process-wide pending set) both need updating.Signals.cpp:47-48.how(write the old mask, returnEINVALinstead ofthrow, per FreeBSD 11.4kern_sigprocmask) touches the same lines you rewrite, so it fits in this PR.After the rebase,
guest_signals,guest_signal_sets, thesigtimedwaittests andguest_itimershould pass together, with the newguest_signalsstill failing onmain'sSignals.cpp/Thread.cpp.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T03:40:20Z.
Conflicts with main; please rebase. Still open from 2026-10-13: @adriadam10's review above (how the per-thread mask relates to main's process-wide pending set,
ExpireAlarm/SignalBlocked, the debt entry now indocs/dev/debt/libkernel.md, and theSignals.cpp:47-48comment).