Skip to content

sched/signal: Keep the pending signals of each thread apart. - #21216

Merged
xiaoxiang781216 merged 3 commits into
apache:masterfrom
casaroli:sig-pending-thread-up
Nov 2, 2026
Merged

xiaoxiang781216 merged 3 commits into
apache:masterfrom
casaroli:sig-pending-thread-up

Conversation

@casaroli

@casaroli casaroli commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

All pending signals of a task group are in one list, tg_sigpendingq. A signal sent to one thread (pthread_kill(), tgkill()) that the thread cannot take at once (it blocks the signal, or it waits in a system call of a protected or kernel build) waits in this list, marked with the TCB of the thread (sigpend->tcb). Two problems:

  • sched/signal: Release the signals pending for a thread when it exits. nxsig_cleanup() released only the lists in the TCB. The entry stayed in the list of the group with a pointer to the freed TCB: the signal was never delivered, its memory was lost, and a later thread at the same TCB address took it, out of turn. The new nxsig_discard_threadsignals() removes the entries of a thread from the list of its group; nxtask_exithook() calls it before the thread leaves its group. The exit sets TCB_FLAG_EXIT_PROCESSING in a critical section before that, and nxsig_tcbdispatch() now checks the flag again in the critical section where it queues the signal (it can leave the critical section to allocate), so no entry comes after the discard, also with SMP.
  • sched/signal: Keep the pending signal of each target apart. nxsig_add_pendingsignal() looked for an entry with the same signal number only. A second pending signal for another thread, or for the whole group, was merged into the entry of the first thread, and the other thread never got it. It now looks for an entry with the same signal number and the same target. POSIX keeps the pending signals of each thread and of the process apart; sigpending() still returns both for the calling thread.
  • One commit describes pending signals in Documentation/reference/user/07_signals.rst.

How it shows on master: ostest on qemu-armv8a:knsh with CONFIG_SIG_SIGUSR1_ACTION does not finish. In signest_test a signal for the waiter thread runs its handler in the main thread of ostest (waiter_action: Delivered to wrong TID 40, expected 99), and the test asserts (signest.c:256, signest.c:115).

Merge order: apache/nuttx-apps#3947 (testing/ostest: Catch the SIGUSR1 that wakes up the mqueue receiver.) first, then this PR. mqueue_test wakes up its receiver with SIGUSR1 and does not catch it. With this PR the receiver of the kernel build also gets the signal, so with CONFIG_SIG_SIGUSR1_ACTION the default action ends ostest in mqueue_test; the apps PR catches the signal. No CI configuration runs ostest with CONFIG_SIG_SIGUSR1_ACTION, so each PR builds and passes CI without the other.

Impact

Behaviour only, for signals that are pending for one thread: they are discarded when the thread exits, and each thread has its own pending instance. Nothing changes for a signal sent to the whole group or for real-time signals (they are queued). The number of pending entries for one signal number is now at most one per thread plus one for the group. No change to a structure or an interface. All build modes and architectures; the code is not built with CONFIG_DISABLE_ALL_SIGNALS.

Not a breaking change, also for the flat build: no interface, structure or configuration changes. Only a signal that was lost or delivered to the wrong thread is now delivered as POSIX requires.

Testing

QEMU with HVF, CONFIG_SIG_SIGUSR1_ACTION, CONFIG_SCHED_CHILD_STATUS and CONFIG_CANCELLATION_POINTS; the flat builds also with CONFIG_SIG_DEFAULT. The tests ran on 23f5a4f: master 7505b87 (#21072) does not build for arm64 (lib_libvsprintf.c:219 and :232, -Werror=shadow; fixed by #21210).

Test program sigpendthr, the same results on qemu-armv8a:knsh (kernel), qemu-armv8a:nsh (flat) and qemu-armv8a:nsh_smp (flat, SMP, 2 CPUs):

Case master this PR
merge: two threads block SIGUSR2, each gets one with pthread_kill(), the process gets one with kill(); then all unblock (handled by thread 1, thread 2, main; want >= 1, >= 1 and 3 in all) 1, 0, 0: FAIL 2, 1, 0: PASS
wait: a thread blocks SIGUSR2 and gets one with pthread_kill(), the process one with kill(); sigtimedwait() with a zero timeout in the thread (want 2 signals) 1: FAIL 2: PASS
exit: 10 times, a thread that blocks SIGUSR1 gets it with pthread_kill() and exits, then a new thread that does not block SIGUSR1 runs (want 0 SIGUSR1 in new threads) 10: FAIL 0: PASS

ostest exit status:

NuttX apps qemu-armv8a:knsh qemu-armv8a:nsh qemu-armv8a:nsh + CONFIG_SIG_DEFAULT
master master no end: assertion in signest_test (above) 0 256: ends in mqueue_test
master the apps PR 0 0 0
this PR master 256: ends in mqueue_test (SIGUSR1 default action) 0 256: ends in mqueue_test
this PR the apps PR 0 0 0

Rows 2 and 3 are from the earlier base 3122150; rows 1 and 4 are on 23f5a4f. qemu-armv8a:nsh_smp (2 CPUs) + CONFIG_SIG_DEFAULT with the apps PR, master and this PR: every test up to the spinlock test passes, and then the spinlock test (10000000 loops) does not end in 20 minutes on both (not changed here).

Build, each OK: qemu-armv8a:nsh (flat), also with CONFIG_ENABLE_PARTIAL_SIGNALS; qemu-armv8a:nsh_smp (flat, SMP); qemu-armv8a:knsh (kernel); rv-virt:pnsh64 (protected, rv64); rv-virt:knsh64 and rv-virt:ksmp64 (kernel, rv64, SMP); mps2-an500:knsh (protected, Cortex-M7). No new warning. With CONFIG_DISABLE_ALL_SIGNALS, qemu-armv8a:nsh compiles the kernel and then fails to link in apps: system/nxinit calls sigprocmask() and ppoll(), which this configuration does not have (not changed here).

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

@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: M The size of the change in this PR is medium labels Oct 30, 2026
@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text +250 B (+0.4%, 65,636 B / 262,144 B, total: 25% used)

esp32-devkitc

  • ROM: .flash.text +160 B (+0.1%, 128,228 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +160 B (+0.2%, 94,100 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +276 B (+0.3%, 85,084 B / 4,194,304 B, total: 2% used)

mirtoo

  • kseg0_progmem: .text +284 B (+0.4%, 67,552 B / 131,072 B, total: 52% used)

qemu-armv8a

  • Code: .text.mqueue_test +60 B, .text.nxsig_tcbdispatch +12 B, .text.nxtask_exithook +8 B, .text.wakeup_action +28 B (+0.1%, 354,532 B)

qemu-intel64

  • Code: .text +381 B (+0.0%, 8,687,922 B)

rx65n-rsk2mb

  • ROM: .text +176 B (+0.2%, 90,232 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +688 B (+0.2%, 384,432 B)

stm32-nucleo-f103rb

  • flash: .text +152 B (+0.4%, 34,316 B / 131,072 B, total: 26% used)

Catch memory regressions in CI with MemBrowse. Free for open source.

A signal sent to one thread (pthread_kill(), tgkill()) that the thread
cannot take at once (it blocks the signal, or it waits in a system call
of a protected or kernel build) waits in the pending list of the group,
marked with the TCB of the thread.  When the thread exited,
nxsig_cleanup() released only the lists in the TCB: the entry stayed in
the list of the group with a pointer to the freed TCB.  It was never
delivered, its memory was lost, and a new thread at the same TCB address
got it when it unblocked the signal, out of turn.

Add nxsig_discard_threadsignals(), which releases the entries of a thread
from the pending list of its group.  nxtask_exithook() calls it before
the thread leaves its group.  The exit sets TCB_FLAG_EXIT_PROCESSING in a
critical section before that.  nxsig_tcbdispatch() now checks the flag
again in the critical section where it queues the signal (it can leave
the critical section to allocate before), so no entry comes after the
discard, also with SMP.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
All pending signals of a group are in one list; an entry for a signal
sent to one thread is marked with that thread.  nxsig_add_pendingsignal()
looked for an entry with the same signal number only, so a second
pending signal for another thread, or for the whole group, was merged
into the entry of the first thread.  The other thread never got its
signal: two threads that block SIGUSR2 and each get one with
pthread_kill() saw one signal between them.

Look for an entry with the same signal number and the same target (the
thread, or the group).  POSIX keeps the pending signals of each thread
and of the process apart.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
Say that a signal sent to one thread and a signal sent to the group are
pending apart, and that the signals pending for a thread are discarded
when it exits.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
@casaroli
casaroli force-pushed the sig-pending-thread-up branch from 84c1b26 to b595e52 Compare October 31, 2026 09:10
@casaroli
casaroli marked this pull request as ready for review October 31, 2026 22:10
@casaroli
casaroli marked this pull request as draft November 1, 2026 11:28
@casaroli
casaroli marked this pull request as ready for review November 1, 2026 11:28
@casaroli
casaroli marked this pull request as draft November 1, 2026 18:58
@casaroli
casaroli marked this pull request as ready for review November 1, 2026 18:59
@xiaoxiang781216
xiaoxiang781216 merged commit d093626 into apache:master Nov 2, 2026
134 of 138 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: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants