Repository navigation
sched: Reap the children when SIGCHLD is ignored or SA_NOCLDWAIT is set. - #21352
Merged
Merged
Conversation
sigaction(SIGCHLD) with SA_NOCLDWAIT set GROUP_FLAG_NOCLDWAIT, but no call cleared it. A process that set SA_NOCLDWAIT once, for example to start one child, never kept the exit status of a child again. XSI also gives SIGCHLD set to SIG_IGN the effect of SA_NOCLDWAIT. NuttX kept the status in this case. Now sigaction(SIGCHLD) sets the flag for SA_NOCLDWAIT or SIG_IGN, and clears it for all other actions. SIG_IGN counts only with CONFIG_SIG_DEFAULT: without it, SIG_IGN and SIG_DFL are the same value. The old action has SA_NOCLDWAIT when the flag is set, so a restore of the old action keeps the flag. The flag changes only after the new action is in place, so a sigaction() that fails with ENOMEM changes nothing. nxsig_dupactions() gives the child of fork() or vfork() the signal actions of its parent, but not the flag: the child ignored SIGCHLD and still kept the status of its own children. It now copies the flag too. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
casaroli
requested review from
Donny9,
GUIDINGLI,
anchao,
jerpelea,
masayuki2009,
pussuw,
xiaoxiang781216,
yamt and
yf13
as code owners
November 2, 2026 11:01
|
With SA_NOCLDWAIT (or SIGCHLD set to SIG_IGN), waitpid() and waitid() returned the PID and the exit status of the first child that exited. XSI says that the children do not become zombies, so the call blocks until all children of the process exit and then fails with ECHILD. A wait for a specific child used nxsched_get_tcb(pid) to see if the child still ran. A child that sent SIGCHLD can still be found during its exit, so a retry after the signal could wait for ever. Add group_has_child(): it finds a running child of a task group (any child or one PID) and skips the threads that are in exit processing. Without retention, waitpid() and waitid() now retry after each SIGCHLD until no child is left, then fail with ECHILD. The wait loops read GROUP_FLAG_NOCLDWAIT again after each signal. Another thread can set SA_NOCLDWAIT while the wait blocks, and that frees the child status structures. The loops then found no structure for the waited-for child (DEBUGASSERT, then a NULL pointer access). Now a wait for a child without a child status structure continues while the child runs and then fails with ECHILD. This also applies when another thread already took the status of the same child. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
sigaction(SIGCHLD) with SIG_IGN suppresses the retention of the child status like SA_NOCLDWAIT, and any other action keeps it again. Without retention, the wait functions wait for all children and fail with ECHILD. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]>
casaroli
force-pushed
the
fb-nocldwait-up
branch
from
November 2, 2026 17:33
4bdc7f3 to
b84031a
Compare
xiaoxiang781216
approved these changes
Nov 2, 2026
casaroli
marked this pull request as draft
November 2, 2026 22:02
casaroli
marked this pull request as ready for review
November 2, 2026 22:02
casaroli
marked this pull request as draft
November 3, 2026 08:53
casaroli
marked this pull request as ready for review
November 3, 2026 08:53
Contributor
|
ci broken is fixed by #21370, please ignore it instead retrigger ci. |
acassis
approved these changes
Nov 3, 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
With
CONFIG_SCHED_CHILD_STATUS,SA_NOCLDWAITandSIGCHLDset toSIG_IGNdid not follow POSIX (XSI):sigaction(SIGCHLD)withSA_NOCLDWAITsetGROUP_FLAG_NOCLDWAIT, but no call cleared it. A process that usedSA_NOCLDWAITonce never kept the exit status of a child again. The old action thatsigaction()returned did not haveSA_NOCLDWAIT, so a save and restore also lost the setting.SIGCHLDset toSIG_IGNthe same effect asSA_NOCLDWAIT. NuttX kept the exit status in this case.waitpid()andwaitid()returned the PID and the status of the first child that exited. XSI says that the call blocks until all children of the process exit, and then fails withECHILD.fork()orvfork()gets the signal actions of its parent (nxsig_dupactions()), but not the flag. A child that inheritedSIG_IGNorSA_NOCLDWAITforSIGCHLDstill kept the status of its own children.SA_NOCLDWAITwhilewaitpid(pid)orwaitid(P_PID)waited, the wait loop did not find the child status structure of the waited-for child any more (DEBUGASSERT, then a NULL pointer access).The first commit makes
sigaction(SIGCHLD)set the flag forSA_NOCLDWAITorSIG_IGNand clear it for all other actions; the old action hasSA_NOCLDWAITwhen the flag is set.SIG_IGNcounts only withCONFIG_SIG_DEFAULT, because without itSIG_IGNandSIG_DFLhave the same value. The flag changes only after the new action is in place, so asigaction()that fails withENOMEMchanges nothing.nxsig_dupactions()now also copies the flag to the child offork()orvfork().The second commit adds
group_has_child(), which finds a running child (any child or one PID) and skips the threads in exit processing. Without retention,waitpid()andwaitid()retry after eachSIGCHLDuntil no child is left, then fail withECHILD. A wait for one PID usednxsched_get_tcb(pid), which also finds a child that is in its exit, so a retry after the signal could wait for ever.group_has_child()finds only a child process with that PID, so a negative PID still fails withECHILD. The wait loops read the flag again after each signal, and a wait for a child without a child status structure continues while the child runs and then fails withECHILD.The third commit updates the
sigaction()differences and the child status section of the task control page.Impact
Only configurations with
CONFIG_SCHED_HAVE_PARENTandCONFIG_SCHED_CHILD_STATUS(the default in kernel builds), in all build modes. WithoutCONFIG_SCHED_CHILD_STATUSnothing changes.SA_NOCLDWAIT(orSIGCHLDtoSIG_IGN), or afork()child of such a program, and then callswait(),waitpid(),waitid()orpclose()getsECHILDafter its children exit, not the status of one child. ostest already acceptsECHILDthere.SA_NOCLDWAITfor a while and then sets another action forSIGCHLDkeeps the exit status of later children again, so it must wait for them. For example, ostest setsSA_NOCLDWAITat the start, and itssighand_testsets aSIGCHLDhandler and thenSIG_DFL: the tests after it keep the child status.NuttX still decides when a child is created whether its status is kept (the child status structure is allocated only while the flag is clear). A child that starts while the flag is set and exits after it is cleared does not become waitable. XSI decides at the exit of the child. This PR does not change that, and the documentation says it.
Testing
New test program
nocldwait.c(eleven cases, run in this order in one process) onqemu-armv8a:knsh(kernel build, QEMU with HVF; children fromposix_spawn()andvfork()) and onqemu-armv8a:nsh(flat build; children fromtask_create()andvfork()), both withCONFIG_SCHED_WAITPID,CONFIG_SCHED_CHILD_STATUSandCONFIG_SIG_DEFAULT(knsh also withCONFIG_PIPESfor case 11). Cases 1 to 10 give the same results on both builds; case 11 (fork()) runs on knsh only:waitpid()gets the statusSA_NOCLDWAIT, then a handler without it: status keptECHILD)SIG_IGN:waitpid(pid)fails withECHILDSIG_IGN:waitpid(-1)waits for all children (200 ms and 1000 ms), thenECHILDECHILDafter about 1000 ms)SA_NOCLDWAIT; a restore keeps itSIG_DFL: status keptECHILD)SIG_IGN:waitid(P_ALL)waits for all children (200 ms and 1000 ms), thenECHILDECHILDafter about 1000 ms)SIG_IGN:waitpid(-1, WNOHANG)gives 0 while a child runs,ECHILDafter it exitsvfork()child: status kept withSIG_DFL,ECHILDwithSIG_IGNECHILDwithSIG_DFL)waitpid(pid)waits, another thread setsSA_NOCLDWAIT, kills another child, and kills the waited-for child 300 ms later: wait for the child, thenECHILDECHILDafter the waited-for child ends)fork()child of a parent withSIGCHLDset toSIG_IGNforks a grandchild: after its exit,waitpid(grandchild)andwaitpid(-1, WNOHANG)fail withECHILDwaitpid(grandchild)returned its PID)SA_NOCLDWAITin the parentwaitpid(grandchild)returned its PID: the parent had the flag, the child did not)Case 10 in a new process (
nocldrace.c, knsh: the status is kept at the start; whilewaitpid(pid)waits, a thread setsSA_NOCLDWAIT, kills the other child, and kills the waited-for child 300 ms later): master stops withAssertion failed : at file: sched/sched_waitpid.c:117(DEBUGASSERT(child)); with this PRwaitpid()fails withECHILDafter the waited-for child ends.Without
CONFIG_SCHED_CHILD_STATUS(qemu-armv8a:knsh), master and this PR give the same results (cases 3, 5 (restore), 8 (running) and 9 (SIG_IGN) pass, the others fail as expected without retention).killpgrp.c(the test ofkill()to a process group:waitpid(-pid, WNOHANG)andwaitid(P_PID, -pid, WNOHANG)withSA_NOCLDWAITfail withECHILD) passes 11/11 on master and with this PR, knsh and flat.ostest exits with status 0 on master and with this PR:
qemu-armv8a:knshandqemu-armv8a:nsh(with the options above), and the flatqemu-armv8a:nshrun with the test programs; alsoqemu-armv8a:knshwithoutCONFIG_SCHED_CHILD_STATUS(base 2e6e1b0).Build, each OK (
-Werror, no warnings insched/):qemu-armv8a:nsh(flat, arm64), the same withCONFIG_DISABLE_PTHREADandCONFIG_SCHED_CHILD_STATUS,qemu-armv8a:nsh_smpwithCONFIG_SCHED_CHILD_STATUSandCONFIG_DEBUG_ASSERTIONS,qemu-armv8a:knsh(kernel, arm64),mps2-an500:knsh(protected, Cortex-M7) withCONFIG_SCHED_CHILD_STATUSandCONFIG_SIG_DEFAULT,mps2-an500:nsh(flat, Cortex-M7) withCONFIG_SCHED_HAVE_PARENTand withoutCONFIG_SCHED_CHILD_STATUS,rv-virt:nsh64(flat, rv64), also withCONFIG_SCHED_HAVE_PARENTand withoutCONFIG_SCHED_CHILD_STATUS,rv-virt:pnsh64(protected, rv64) andrv-virt:knsh64(kernel, rv64) withCONFIG_SCHED_CHILD_STATUS.Base: master 1b0691e. The results without
CONFIG_SCHED_CHILD_STATUSare from the base 2e6e1b0 (before #21144; the rebase changed only the lines that #21144 also changed).tools/checkpatch.sh -c -u -m -gpasses.