Skip to content

sched/signal: Restore the mask of pselect() after its signals are dispatched. - #20720

Merged
xiaoxiang781216 merged 2 commits into
apache:masterfrom
casaroli:pselect-mask-restore
Oct 23, 2026
Merged

xiaoxiang781216 merged 2 commits into
apache:masterfrom
casaroli:pselect-mask-restore

Conversation

@casaroli

@casaroli casaroli commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

pselect(), ppoll() and sigsuspend() install a temporary signal mask and restore the old one before they return. In a system call, a signal that the temporary mask unblocks is not delivered at once: it goes back on the pending list, to be dispatched at the end of the system call. By then the old mask was restored, so a signal that the old mask blocks stayed pending and its handler never ran; every later pselect() returned EINTR at once.

In a system call, the temporary mask now stays and the old one is saved in the TCB. nxsig_unmask_pendingsignal(), at the end of the system call, dispatches the pending signals with the temporary mask and then restores the old mask. Outside a system call the mask is restored at once, as before.

A second commit updates sigsuspend() in Documentation/reference/user/07_signals.rst: the handlers run before the original mask comes back, and the restore of the mask is no longer listed as a difference from POSIX (POSIX requires it).

Impact

Kernel builds (pselect() etc. as system calls): the handler of a signal unblocked only by the temporary mask now runs once. Flat builds behave as before.

Testing

Builds: qemu-armv8a:nsh, qemu-armv8a:knsh, lm3s6965-ek:qemu-flat.

Runtime, QEMU 11 with HVF on an Apple M4: ostest on qemu-armv8a:knsh (kernel build) and qemu-armv8a:nsh (flat build) ends with ostest_main: Exiting with status 0.

tools/checkpatch.sh -c -u -m -g passes.

A test program on qemu-armv8a:knsh: block SIGUSR1, raise it, then call pselect() three times with an empty mask and a 200 ms timeout.

master:

pseltest: pselect 0 returned -1 (Interrupted system call), handler ran 0 times
pseltest: pselect 1 returned -1 (Interrupted system call), handler ran 0 times
pseltest: pselect 2 returned -1 (Interrupted system call), handler ran 0 times
pseltest: FAIL

This PR:

pseltest: pselect 0 returned -1 (Interrupted system call), handler ran 1 times
pseltest: pselect 1 returned 0 (timeout), handler ran 1 times
pseltest: pselect 2 returned 0 (timeout), handler ran 1 times
pseltest: PASS

…patched.

pselect(), ppoll() and sigsuspend() install a temporary signal mask and
restore the old one before they return.  In a system call, a signal
that the temporary mask unblocks is not delivered at once: it goes back
on the list of pending signals, to be dispatched at the end of the
system call.  By then the old mask was restored, so a signal that the
old mask blocks stayed pending, and its handler never ran.  Every later
pselect() found it and returned EINTR at once.

For example, a program that blocks SIGCHLD and unblocks it only in
pselect() never runs its SIGCHLD handler, and its pselect() loop spins.

In a system call, keep the temporary mask and save the old one in the
TCB.  nxsig_unmask_pendingsignal(), at the end of the system call,
dispatches the pending signals with the temporary mask, which queues
their actions, and then restores the old mask.  Outside a system call
the mask is restored at once, as before.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
@github-actions github-actions Bot added Area: OS Components OS Components issues Size: S The size of the change in this PR is small labels Oct 17, 2026
@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report

esp32-devkitc

  • ROM: .flash.text +84 B (+0.1%, 126,268 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +84 B (+0.1%, 90,464 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +76 B (+0.1%, 84,092 B / 4,194,304 B, total: 2% used)

qemu-armv8a

  • Code: .text.nxsig_unmask_pendingsignal -124 B, .text.ppoll +12 B, .text.pthread_start +4 B, .text.up_initial_state +4 B (+0.1%, 348,620 B)
  • Data: .bss.g_idletcb +8 B (+0.0%, 77,506 B)

qemu-intel64

  • Code: .text +140 B (+0.0%, 8,683,594 B)

rx65n-rsk2mb

  • RAM: .bss +16 B (+0.2%, 6,602 B / 262,144 B, total: 3% used)
  • ROM: .text +80 B (+0.1%, 87,492 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +272 B (+0.1%, 376,048 B)
  • Data: .bss +16 B (+0.1%, 11,835 B)

stm32-nucleo-f103rb

  • sram: .bss +16 B (+0.7%, 2,468 B / 20,480 B, total: 12% used)
    No memory changes detected for:
  • arduino-mega2560
  • mirtoo

sigsuspend() runs the handlers of the signals that its mask unblocks
before it restores the original mask, also in a system call.  The page
did not say this, and it listed the restore of the mask as a difference
from POSIX, which requires it.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation and removed Area: OS Components OS Components issues labels Oct 17, 2026
@acassis

acassis commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

@xiaoxiang781216 PTAL

@xiaoxiang781216
xiaoxiang781216 merged commit ef61162 into apache:master Oct 23, 2026
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants