fix(libkernel): return EINTR from pselect when its mask unblocks a pending signal #10501

Merged
harisibrar1 merged 3 commits from upstream-pr/10501 into main 2026-10-26 05:26:20 +00:00
harisibrar1 commented 2026-10-24 04:39:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T04:39:29Z.


What

pselect (merged in #9273) never returned EINTR. With the canonical pattern of blocking a signal, raising it and waiting with a mask that unblocks it:

sigprocmask(SIG_BLOCK, {SIGUSR1}); raise(SIGUSR1);
pselect(0, NULL, NULL, NULL, &{0, 300 ms}, &empty);

FreeBSD 11.4 runs the handler and returns -1/EINTR at once. Here the handler ran inside the mask swap (_sigprocmask delivers a pending signal that SIG_SETMASK unblocks), and select then waited the full 300 ms and returned 0. With a NULL timeout it never returned. This was raised in the review of #9273.

In FreeBSD's kern_pselect the mask is installed and kern_select sleeps with PCATCH. Before it sleeps, kern_select checks nd, the descriptors and the timeout, then scans once: a ready descriptor wins, a zero timeout returns 0, and only then does a pending signal end the sleep with EINTR (sys/kern/sys_generic.c, release/11.4.0). On EINTR the descriptor sets are not copied out.

The change:

  • Signals.cpp: main's ChangeBlockedMask (from #9499) gains an optional bool* caught, and DeliverUnblockedPending returns whether a guest handler ran. SetSignalMaskCatching_nid_no_patch uses it with SIG_SETMASK, releasing holds like the sigprocmask call it replaces. _sigprocmask itself behaves as before.
  • SetSignalMaskCatching_nid_no_patch is declared once, in prx/libkernel/include/Api.hpp, which both Signals.cpp and Pselect.cpp include.
  • Pselect.cpp: when the swap ran a handler, pselect scans the descriptors once with a zero timeout instead of waiting. A ready descriptor, a zero timeout or an invalid argument (nfds, timeout) keep select's answer. Otherwise the call returns -1/EINTR and leaves the descriptor sets as they were.
  • The pselect debt entry is rewritten. The old text described a narrow race; the remaining gap is that a signal raised during the wait (another thread's pthread_kill, or raise from a handler) still doesn't interrupt it, because select has no EINTR path. It also notes that the mask is the process-wide one.

Tested

Windows 11, WinLibs GCC 15.2.0 r7 (UCRT), Release. Full build of 7784604b0b (current main 47f9cee2cd merged in): 0 failed steps.

  • guest_kernel_sys_pselect passes 3 out of 3 on 7784604b0b. The new cases cover: the pending-signal idiom returns EINTR in under 150 ms with the handler run once, the descriptor set unchanged and the mask restored; a NULL timeout returns EINTR instead of blocking; a ready descriptor still wins (returns 1); a zero timeout still returns 0; an invalid timeout still returns EINVAL; and a mask that keeps the signal blocked still waits the full timeout, with the handler running only when the signal is later unblocked.
  • Negative control: with main's Pselect.cpp and Signals.cpp, the test fails at the first EINTR check (line 129).
  • The related signal, timer and select tests pass (11 of 11, 3 runs), including guest_kernel_sys, guest_signals, guest_sigtimedwait, guest_getitimer, guest_itimer and guest_itimer_pending_default.
  • tools/check_conventions.py --base origin/main passes.
  • Not tested: Linux and macOS.

Overlap

  • #9499 (@EagleBearerYS) landed first as agreed; this now builds on its ChangeBlockedMask instead of keeping a second helper.
  • #10310 edits docs/dev/debt/libkernel.md near the select entries; a rebase there is a text merge only.

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: #9499 (proposed order: #9499 first, then this PR rebased onto its ChangeBlockedMask); #10310 in either order, as a text merge in docs/dev/debt/libkernel.md (asked both authors to confirm below)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4992528579 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10501). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T04:39:29Z. --- ### What `pselect` (merged in #9273) never returned `EINTR`. With the canonical pattern of blocking a signal, raising it and waiting with a mask that unblocks it: ``` sigprocmask(SIG_BLOCK, {SIGUSR1}); raise(SIGUSR1); pselect(0, NULL, NULL, NULL, &{0, 300 ms}, &empty); ``` FreeBSD 11.4 runs the handler and returns -1/`EINTR` at once. Here the handler ran inside the mask swap (`_sigprocmask` delivers a pending signal that `SIG_SETMASK` unblocks), and `select` then waited the full 300 ms and returned 0. With a NULL timeout it never returned. This was raised in the review of #9273. In FreeBSD's `kern_pselect` the mask is installed and `kern_select` sleeps with `PCATCH`. Before it sleeps, `kern_select` checks `nd`, the descriptors and the timeout, then scans once: a ready descriptor wins, a zero timeout returns 0, and only then does a pending signal end the sleep with `EINTR` (`sys/kern/sys_generic.c`, release/11.4.0). On `EINTR` the descriptor sets are not copied out. The change: - `Signals.cpp`: main's `ChangeBlockedMask` (from #9499) gains an optional `bool* caught`, and `DeliverUnblockedPending` returns whether a guest handler ran. `SetSignalMaskCatching_nid_no_patch` uses it with `SIG_SETMASK`, releasing holds like the `sigprocmask` call it replaces. `_sigprocmask` itself behaves as before. - `SetSignalMaskCatching_nid_no_patch` is declared once, in `prx/libkernel/include/Api.hpp`, which both `Signals.cpp` and `Pselect.cpp` include. - `Pselect.cpp`: when the swap ran a handler, `pselect` scans the descriptors once with a zero timeout instead of waiting. A ready descriptor, a zero timeout or an invalid argument (`nfds`, timeout) keep `select`'s answer. Otherwise the call returns -1/`EINTR` and leaves the descriptor sets as they were. - The `pselect` debt entry is rewritten. The old text described a narrow race; the remaining gap is that a signal raised during the wait (another thread's `pthread_kill`, or `raise` from a handler) still doesn't interrupt it, because `select` has no `EINTR` path. It also notes that the mask is the process-wide one. ### Tested Windows 11, WinLibs GCC 15.2.0 r7 (UCRT), Release. Full build of 7784604b0b (current `main` 47f9cee2cd merged in): 0 failed steps. - `guest_kernel_sys_pselect` passes 3 out of 3 on 7784604b0b. The new cases cover: the pending-signal idiom returns `EINTR` in under 150 ms with the handler run once, the descriptor set unchanged and the mask restored; a NULL timeout returns `EINTR` instead of blocking; a ready descriptor still wins (returns 1); a zero timeout still returns 0; an invalid timeout still returns `EINVAL`; and a mask that keeps the signal blocked still waits the full timeout, with the handler running only when the signal is later unblocked. - Negative control: with main's `Pselect.cpp` and `Signals.cpp`, the test fails at the first `EINTR` check (line 129). - The related signal, timer and select tests pass (11 of 11, 3 runs), including `guest_kernel_sys`, `guest_signals`, `guest_sigtimedwait`, `guest_getitimer`, `guest_itimer` and `guest_itimer_pending_default`. - `tools/check_conventions.py --base origin/main` passes. - Not tested: Linux and macOS. ### Overlap - #9499 (@EagleBearerYS) landed first as agreed; this now builds on its `ChangeBlockedMask` instead of keeping a second helper. - #10310 edits `docs/dev/debt/libkernel.md` near the `select` entries; a rebase there is a text merge only. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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) - [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: #9499 (proposed order: #9499 first, then this PR rebased onto its `ChangeBlockedMask`); #10310 in either order, as a text merge in `docs/dev/debt/libkernel.md` (asked both authors to confirm below) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-24 11:20:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T11:20:33Z.

Review: CHANGES_REQUESTED


The pselect change is narrowly scoped and the negative control plus ready-descriptor, timeout, mask-restore, and signal-blocking cases are useful. The author reports the related Windows tests passing on an earlier base with an identical patch-id; current hosted PR build jobs are skipped, and I did not rerun the test suite locally.

Please resolve two merge-order declarations before approval. The body explicitly says #9499 remains open, changes the same Signals.cpp helper, and that #9499 should land first; however the checklist says Depends on: none. The body also says open #10310 changes the pselect debt text and needs a text merge, but no order is declared for that overlap. Please coordinate with those authors and list the agreed order(s) in the checklist, following CONTRIBUTING. The current head is mergeable with main, so I found no separate rebase blocker or code defect in this pass.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638059948 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10501#pullrequestreview-5638059948). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T11:20:33Z. **Review:** CHANGES_REQUESTED --- The pselect change is narrowly scoped and the negative control plus ready-descriptor, timeout, mask-restore, and signal-blocking cases are useful. The author reports the related Windows tests passing on an earlier base with an identical patch-id; current hosted PR build jobs are skipped, and I did not rerun the test suite locally. Please resolve two merge-order declarations before approval. The body explicitly says #9499 remains open, changes the same Signals.cpp helper, and that #9499 should land first; however the checklist says Depends on: none. The body also says open #10310 changes the pselect debt text and needs a text merge, but no order is declared for that overlap. Please coordinate with those authors and list the agreed order(s) in the checklist, following CONTRIBUTING. The current head is mergeable with main, so I found no separate rebase blocker or code defect in this pass.
MarcZX10 commented 2026-10-24 12:59:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T12:59:00Z.

Review: APPROVED


Reviewed current head c7cd45c0da. The previously missing merge-order agreements are now explicit: #9499 confirmed it lands first, and #10310 confirmed either order for the adjacent debt text; the checklist records both. The implementation and regression coverage are unchanged from my prior review. The author reports the Windows focused signal suite and the Linux follow-up reports seven related tests passing three consecutive runs, including a negative control where main fails the new EINTR case. I did not reproduce those runs locally. Hosted structural checks pass; product build jobs are skipped by eligibility. No remaining blocker found.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638327181 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10501#pullrequestreview-5638327181). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T12:59:00Z. **Review:** APPROVED --- Reviewed current head c7cd45c0da49e8a6e2cdff118ea9f2e8e041dad6. The previously missing merge-order agreements are now explicit: #9499 confirmed it lands first, and #10310 confirmed either order for the adjacent debt text; the checklist records both. The implementation and regression coverage are unchanged from my prior review. The author reports the Windows focused signal suite and the Linux follow-up reports seven related tests passing three consecutive runs, including a negative control where main fails the new EINTR case. I did not reproduce those runs locally. Hosted structural checks pass; product build jobs are skipped by eligibility. No remaining blocker found.
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!10501
No description provided.