fix(libkernel): keep the native signal handler out of the allocator and the pending lock #22307

Closed
Kal0s-design wants to merge 1 commit from upstream-pr/22307 into main
Kal0s-design commented 2026-11-17 11:30:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kal0s-design on 2026-11-17T11:30:08Z.


What

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.

Dispatch runs as the host signal handler. For a signal the guest has blocked it called MarkPending, which locks the pending mutex and inserts into a std::deque. Neither is safe in a signal handler:

  • If the signal interrupts the thread inside malloc or free, the insertion re-enters the allocator and the thread never returns from it.
  • If the signal interrupts the thread while it holds the pending mutex, the handler waits for a lock its own thread owns.

In both cases the pending mutex stays held or contended, so every later kill, sigwait and sigpending on the process queues behind it.

The fix keeps the handler to operations that are safe there:

  • The handler records the signal in a per-signal atomic count and in the pending word, and writes one byte to a pipe.
  • A relay thread reads the pipe, takes the pending mutex and notifies the waiters. A wait that checked the set under the mutex therefore still sees either the signal or the notification.
  • Windows keeps MarkPending: there the CRT calls the handler from raise, on the raising thread and outside any interrupted code.

guest_blocked_signal_allocator is the regression test (POSIX hosts). 64 threads block SIGTERM and loop on malloc/free while 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 ea6760b516 on main 9b7afdd507. The main column is main with only the new test added.

Linux 6.18, GCC 16.1, Release. guest_blocked_signal_allocator, 30 runs per build:

Build This change main
plain 30 of 30 4 of 30; the other 26 stop with "a worker never left the allocator after the handler ran in it"
TSan 30 of 30 0 of 30, signal-unsafe call inside of a signal in MarkPending and CurrentSignals
ASan + UBSan, RelWithDebInfo 30 of 30 30 of 30
ASan + UBSan, Release (the Sanitizers workflow's configuration) 30 of 30 30 of 30

Under ASan the test passes on main as 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:

Build This change main
plain 18 of 18 17 of 18 (the new test)
ASan + UBSan, both configurations 18 of 18 18 of 18
TSan 17 of 18 14 of 18
  • TSan on main fails guest_signals, guest_broken_pipe, guest_signal_handler_mask and the new test. With this change guest_broken_pipe and guest_signal_handler_mask pass, and the signal-unsafe call reports are gone.
  • guest_signals still fails under TSan with a lock-order inversion in DiscardPendingIfIgnored. That is a separate defect, present on main, 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.py passes.

Not tested: macOS, and no title was run.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5355995642 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22307). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-11-17T11:30:08Z. --- ### What 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. `Dispatch` runs as the host signal handler. For a signal the guest has blocked it called `MarkPending`, which locks the pending mutex and inserts into a `std::deque`. Neither is safe in a signal handler: - If the signal interrupts the thread inside `malloc` or `free`, the insertion re-enters the allocator and the thread never returns from it. - If the signal interrupts the thread while it holds the pending mutex, the handler waits for a lock its own thread owns. In both cases the pending mutex stays held or contended, so every later `kill`, `sigwait` and `sigpending` on the process queues behind it. The fix keeps the handler to operations that are safe there: - The handler records the signal in a per-signal atomic count and in the pending word, and writes one byte to a pipe. - A relay thread reads the pipe, takes the pending mutex and notifies the waiters. A wait that checked the set under the mutex therefore still sees either the signal or the notification. - Windows keeps `MarkPending`: there the CRT calls the handler from `raise`, on the raising thread and outside any interrupted code. `guest_blocked_signal_allocator` is the regression test (POSIX hosts). 64 threads block `SIGTERM` and loop on `malloc`/`free` while 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 `ea6760b516` on `main` `9b7afdd507`. The `main` column is `main` with only the new test added. **Linux 6.18, GCC 16.1, Release.** `guest_blocked_signal_allocator`, 30 runs per build: | Build | This change | `main` | |---|---|---| | plain | 30 of 30 | 4 of 30; the other 26 stop with "a worker never left the allocator after the handler ran in it" | | TSan | 30 of 30 | 0 of 30, `signal-unsafe call inside of a signal` in `MarkPending` and `CurrentSignals` | | ASan + UBSan, RelWithDebInfo | 30 of 30 | 30 of 30 | | ASan + UBSan, Release (the Sanitizers workflow's configuration) | 30 of 30 | 30 of 30 | Under ASan the test passes on `main` as 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: | Build | This change | `main` | |---|---|---| | plain | 18 of 18 | 17 of 18 (the new test) | | ASan + UBSan, both configurations | 18 of 18 | 18 of 18 | | TSan | 17 of 18 | 14 of 18 | - TSan on `main` fails `guest_signals`, `guest_broken_pipe`, `guest_signal_handler_mask` and the new test. With this change `guest_broken_pipe` and `guest_signal_handler_mask` pass, and the `signal-unsafe call` reports are gone. - `guest_signals` still fails under TSan with a lock-order inversion in `DiscardPendingIfIgnored`. That is a separate defect, present on `main`, 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.py` passes. Not tested: macOS, and no title was run. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
PrivacyOnion commented 2026-11-17 11:37:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-17T11:37:22Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against ea6760b516:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_blocked_signal_allocator_tests built cleanly.
  • CTest (guest_blocked_signal_allocator):
    1/1 Test #5412: guest_blocked_signal_allocator ...   Passed    1.51 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/System/src/Signals.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #5412: guest_blocked_signal_allocator ...***Failed   15.06 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified POSIX signal handler async-signal safety: WakeFromHandler and NotePendingLwp only call async-signal-safe system calls (write) and lock-free atomic operations, avoiding std::mutex lock and heap allocation in Dispatch.
    • Audited TakePendingLocked logic: properly handles atomic decrements of counted LWP signals, ensures word bits are appropriately synchronized and cleared only when both queued and counted pending signals are 0.
    • Checked EnsureWakeRelay thread lifecycle and pipe initialization: O_NONBLOCK and FD_CLOEXEC set correctly, pipe read loop handles EINTR and EOF cleanly.
    • Audited guest_blocked_signal_allocator_tests test logic: correctly stresses concurrent host signal delivery while workers allocate and free memory under blocked signal mask, verifying signal delivery once unblocked.
    • Audited AGENTS.md compliance, cross-platform compilation conditional compilation (#ifndef _WIN32 / #ifdef _WIN32), and lack of non-conforming comments.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5937708630 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22307#pullrequestreview-5937708630). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-17T11:37:22Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `ea6760b516`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_blocked_signal_allocator_tests` built cleanly. - **CTest** (`guest_blocked_signal_allocator`): ``` 1/1 Test #5412: guest_blocked_signal_allocator ... Passed 1.51 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libkernel/System/src/Signals.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #5412: guest_blocked_signal_allocator ...***Failed 15.06 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified POSIX signal handler async-signal safety: WakeFromHandler and NotePendingLwp only call async-signal-safe system calls (write) and lock-free atomic operations, avoiding std::mutex lock and heap allocation in Dispatch. - Audited TakePendingLocked logic: properly handles atomic decrements of counted LWP signals, ensures word bits are appropriately synchronized and cleared only when both queued and counted pending signals are 0. - Checked EnsureWakeRelay thread lifecycle and pipe initialization: O_NONBLOCK and FD_CLOEXEC set correctly, pipe read loop handles EINTR and EOF cleanly. - Audited guest_blocked_signal_allocator_tests test logic: correctly stresses concurrent host signal delivery while workers allocate and free memory under blocked signal mask, verifying signal delivery once unblocked. - Audited AGENTS.md compliance, cross-platform compilation conditional compilation (#ifndef _WIN32 / #ifdef _WIN32), and lack of non-conforming comments. Looks Good To Me

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!22307
No description provided.