fix(libkernel): keep the native signal handler out of the allocator and the pending lock #22391
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/22391"
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 Kal0s-design on 2026-11-17T13:52:00Z.
What
This replaces #22307, same commit
ea6760b516. The open-pull-request limit closed that one automatically while #20009 was open; #20009 is now closed, but GitHub refuses to reopen #22307 (the request fails with a validation error), so this is a new pull request for the same change. #22307 carries an approving review with a negative control and a second independent confirmation on another machine and compiler.A host signal that arrives for a guest-blocked signal can deadlock the thread it lands on, and with it every later sender and waiter.
Dispatchruns as the host signal handler. For a signal the guest has blocked it calledMarkPending, which locks the pending mutex and inserts into astd::deque. Neither is safe in a signal handler:mallocorfree, the insertion re-enters the allocator and the thread never returns from it.In both cases the pending mutex stays held or contended, so every later
kill,sigwaitandsigpendingon the process queues behind it.The fix keeps the handler to operations that are safe there:
MarkPending: there the CRT calls the handler fromraise, on the raising thread and outside any interrupted code.guest_blocked_signal_allocatoris the regression test (POSIX hosts). 64 threads blockSIGTERMand loop onmalloc/freewhile the main thread sends each of them the host signal four times; the test fails if a worker never leaves the allocator.#19463 also changes
Signals.cpp(ktimer delivery). It does not touch the handler path, but the two will need a rebase against each other.Tested
Head
ea6760b516onmain9b7afdd507. Themaincolumn ismainwith only the new test added.Linux 6.18, GCC 16.1, Release.
guest_blocked_signal_allocator, 30 runs per build:mainsignal-unsafe call inside of a signalinMarkPendingandCurrentSignalsUnder ASan the test passes on
mainas well: ASan's allocator does not deadlock on re-entry, so those builds do not show the defect.The 18 tests of the signal, itimer, ktimer and pselect set, one run each:
mainmainfailsguest_signals,guest_broken_pipe,guest_signal_handler_maskand the new test. With this changeguest_broken_pipeandguest_signal_handler_maskpass, and thesignal-unsafe callreports are gone.guest_signalsstill fails under TSan with a lock-order inversion inDiscardPendingIfIgnored. That is a separate defect, present onmain, and not addressed here.Windows, WinLibs GCC 15, Release: the 16 tests of the same set that exist on Windows pass in 3 of 3 runs. The handler path is unchanged there.
tools/check_conventions.pypasses.Not tested: macOS, and no title was run.
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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-17T13:55:55Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
ea6760b516:tools/check_conventions.pypassed.guest_blocked_signal_allocator_testsbuilt cleanly.guest_blocked_signal_allocator):core/libs/prx/libkernel/System/src/Signals.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T16:35:36Z.
Review: COMMENTED
Reviewed — comments
The signal handler path avoids the pending mutex and allocator on POSIX, but setup and wake-relay failures are not handled safely.
What I ran
Reviewed head
ea6760b516c7a05ab51f6f9642a76dfa07fe8784. No commands or tests were run. Readcore/libs/prx/libkernel/CMakeLists.txtandcore/libs/prx/libkernel/System/src/Signals.cpp;review/files.jsonand the excluded test source were unavailable through the reader.Results
Findings
Blocking
None verified.
Non-blocking
EnsureWakeRelayignoresfcntlfailures and publishes the writer after starting the relay. If settingO_NONBLOCKfails, the signal handler'swritecan block when the pipe fills; ifFD_CLOEXECsetup fails, descriptors can leak into child processes. Consider checking these calls and failing setup before installing the handler. (core/libs/prx/libkernel/System/src/Signals.cpp:179-182)WakeFromHandlerignores write errors other thanEINTR, including a full pipe. The relay's one-byte-per-read design normally ensures a pending notification, but explicitly documenting or handling saturation would make the wake-up guarantee easier to verify. (core/libs/prx/libkernel/System/src/Signals.cpp:185-190)Not verified
The new regression test source was excluded by rule and could not be read;
review/files.jsonwas also unavailable. No build, test suite, negative control, Windows or macOS run was performed. Author-reported test results in the PR description are not independent verification.To get this to a good state
Nothing is required. Optional: check descriptor setup failures and clarify the full-pipe wake behavior.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T20:59:15Z.
Review: COMMENTED
Reviewed — comments
The change addresses the allocator and pending-lock hazards with an atomic pending count and a wake relay; I found one potential loss of wakeups when the nonblocking pipe fills.
What I ran
Reviewed head
e74538c7ecdd305569d4d8e79017f6ada9ad4423against the prepared diff. I did not run commands or tests.Results
Findings
Blocking
None.
Non-blocking
core/libs/prx/libkernel/System/src/Signals.cpp:193: when the nonblocking pipe is full,writefails withEAGAINandWakeFromHandlerdrops that wake byte. A relay may drain bytes already in the pipe, but if a signal is recorded after the relay's last drain and its write getsEAGAIN, a waiter can remain asleep despite pending state. Consider ensuring a wake is retried or otherwise guaranteed when the pipe is full, while keeping the handler async-signal-safe.Not verified
core/libs/tests/GuestBlockedSignalAllocator.cppwas not read; it was excluded by the review bundle rule.Signals.cppandCMakeLists.txtwere read; these are all files listed as reviewed in the manifest.To get this to a good state
Nothing is required. Consider the optional pipe-full wake handling noted above.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T04:54:54Z.
Review: COMMENTED
Reviewed — comments
Changes are needed to address the concurrency concerns below before this change is ready for approval.
What I ran
Reviewed head
8160336c8dcc12842afb1e13480613ab95daca96. No tests or builds were run; author-reported results in the description were not independently verified.Results
Findings
Blocking
None verified.
Non-blocking
Signals.cpp:197caps the LWP count at 1024, butMarkPendingalso applies that cap to the combined LWP and queued signals atSignals.cpp:257. The newNotePendingLwppath does not check or reserve space againstqueued[guest], so a full queue can still accumulate 1024 LWP entries. Please confirm the intended per-signal limit and enforce it across both sources if they share that limit.Signals.cpp:221allows concurrent callers to each create a pipe and relay thread whilewakeWriteris still-1; the last store wins and leaves earlier descriptors and relay threads alive. Serialize one-time setup or publish a single initialized instance.Signals.cpp:240retries interrupted writes but ignores other errors, includingEAGAINwhen the nonblocking pipe is full. A write failure can leave a waiter asleep despite a pending signal. Ensure a full pipe implies a relay notification is already pending, and handle other failures consistently.Sources
Not verified
CMakeLists.txtandSignals.cppwere read; no changed files were excluded by the review manifest. The description-named test source was not present in the directory listing, so its contents and whether it is available outside the listed paths are not verified.To get this to a good state
Nothing is required for merge based on verified blocking defects. Optional: clarify and enforce the shared pending-signal cap, make relay initialization single-instance, and account for non-
EINTRpipe write failures.Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T15:57:18Z.
Review: COMMENTED
I ran
guest_blocked_signal_allocatorwith the patch applied on Linux. It passed the allocator and blocked-signal regression fixture.AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.