Repository navigation
sched/signal: Keep the pending signals of each thread apart. - #21216
Merged
Merged
Conversation
|
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
force-pushed
the
sig-pending-thread-up
branch
from
October 31, 2026 09:10
84c1b26 to
b595e52
Compare
casaroli
marked this pull request as ready for review
October 31, 2026 22:10
casaroli
requested review from
jerpelea,
masayuki2009,
pussuw,
xiaoxiang781216 and
yamt
as code owners
October 31, 2026 22:10
xiaoxiang781216
approved these changes
Nov 1, 2026
casaroli
marked this pull request as draft
November 1, 2026 11:28
casaroli
marked this pull request as ready for review
November 1, 2026 11:28
casaroli
marked this pull request as draft
November 1, 2026 18:58
casaroli
marked this pull request as ready for review
November 1, 2026 18:59
acassis
approved these changes
Nov 1, 2026
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: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 newnxsig_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 setsTCB_FLAG_EXIT_PROCESSINGin a critical section before that, andnxsig_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.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.Documentation/reference/user/07_signals.rst.How it shows on master: ostest on qemu-armv8a:knsh with
CONFIG_SIG_SIGUSR1_ACTIONdoes not finish. Insignest_testa 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_testwakes up its receiver with SIGUSR1 and does not catch it. With this PR the receiver of the kernel build also gets the signal, so withCONFIG_SIG_SIGUSR1_ACTIONthe default action ends ostest inmqueue_test; the apps PR catches the signal. No CI configuration runs ostest withCONFIG_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_STATUSandCONFIG_CANCELLATION_POINTS; the flat builds also withCONFIG_SIG_DEFAULT. The tests ran on 23f5a4f: master 7505b87 (#21072) does not build for arm64 (lib_libvsprintf.c:219and: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):pthread_kill(), the process gets one withkill(); then all unblock (handled by thread 1, thread 2, main; want >= 1, >= 1 and 3 in all)pthread_kill(), the process one withkill();sigtimedwait()with a zero timeout in the thread (want 2 signals)pthread_kill()and exits, then a new thread that does not block SIGUSR1 runs (want 0 SIGUSR1 in new threads)ostest exit status:
CONFIG_SIG_DEFAULTsignest_test(above)mqueue_testmqueue_test(SIGUSR1 default action)mqueue_testRows 2 and 3 are from the earlier base 3122150; rows 1 and 4 are on 23f5a4f. qemu-armv8a:nsh_smp (2 CPUs) +
CONFIG_SIG_DEFAULTwith 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. WithCONFIG_DISABLE_ALL_SIGNALS, qemu-armv8a:nsh compiles the kernel and then fails to link in apps:system/nxinitcallssigprocmask()andppoll(), which this configuration does not have (not changed here).tools/checkpatch.sh -c -u -m -gpasses.