This is an automated email from the ASF dual-hosted git repository.
acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 3b0bf0cafff sched/task: Count the last exiting thread of a group under
tg_lock.
3b0bf0cafff is described below
commit 3b0bf0cafffb2eb769a5e31523dd2bd53f73c028
Author: Marco Casaroli <[email protected]>
AuthorDate: Wed Nov 18 12:58:15 2026 +0100
sched/task: Count the last exiting thread of a group under tg_lock.
Only the last thread of a task group to exit sends SIGCHLD to the
parent and marks the child status as exited. nxtask_exithook() found
it with sq_is_singular(&tg_members), but a thread leaves tg_members only
later, in group_leave(). Between the two, sched_unlock() can switch the
thread out and other CPUs run. So when two threads of a group exit at
the same time, each one can see the other in the list: no thread sends
SIGCHLD, and the parent waits in waitpid() for ever. The same test
decided the wake-up of waitpid() without CONFIG_SCHED_HAVE_PARENT.
This is seen on qemu-armv8a:citest_smp: getprime ends while the thread
that it joined is still in its exit processing, and NSH waits for ever
in waitpid() with the child status not marked as exited. A 50 ms sleep
added after sched_unlock() in nxtask_exithook() makes it hang at each
getprime run.
Now each exiting thread sets TCB_FLAG_EXIT_COUNTED under tg_lock and
tests whether all of the members have it. Exactly one thread, the last
one, sees all of them set. The exit status of the main thread is saved
before its mark, so the SIGCHLD carries it.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
---
include/nuttx/sched.h | 1 +
sched/task/task_exithook.c | 52 +++++++++++++++++++++++++++++++++++++++++++---
2 files changed, 50 insertions(+), 3 deletions(-)
diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h
index 53e2ecba77a..77c8b993daa 100644
--- a/include/nuttx/sched.h
+++ b/include/nuttx/sched.h
@@ -114,6 +114,7 @@
#define TCB_FLAG_PREEMPT_SCHED (1 << 16) /* Bit 16:
tcb is PREEMPT_SCHED */
#define TCB_FLAG_SIGMASK_SAVED (1 << 17) /* Bit 17:
sigsavedmask to restore */
#define TCB_FLAG_UNINTERRUPTIBLE (1 << 18) /* Bit 18:
Blocking wait ignores signals */
+#define TCB_FLAG_EXIT_COUNTED (1 << 19) /* Bit 19:
Counted as exited in group */
/* Values for struct task_group tg_flags */
diff --git a/sched/task/task_exithook.c b/sched/task/task_exithook.c
index cb177eb60de..e0805f10968 100644
--- a/sched/task/task_exithook.c
+++ b/sched/task/task_exithook.c
@@ -27,6 +27,7 @@
#include <nuttx/config.h>
#include <aio.h>
+#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
@@ -39,6 +40,7 @@
#include <nuttx/sched.h>
#include <nuttx/fs/fs.h>
#include <nuttx/mm/mm.h>
+#include <nuttx/spinlock.h>
#include "sched/sched.h"
#include "group/group.h"
@@ -49,6 +51,50 @@
* Private Functions
****************************************************************************/
+/****************************************************************************
+ * Name: nxtask_lastmember
+ *
+ * Description:
+ * Mark the exiting thread as counted in its task group, and return true
+ * if every member of the group is counted: the thread is the last one of
+ * the group to exit.
+ *
+ * A thread leaves the member list only later, in group_leave(). Two
+ * threads of a group can exit at the same time: on SMP, or when one of
+ * them is switched out before group_leave(). If each one tested only
+ * that it is alone in the member list, each one could see the other, and
+ * the parent would get no SIGCHLD and no exit status. The mark and the
+ * test are done together under tg_lock, so exactly one thread sees all
+ * of the members marked.
+ *
+ ****************************************************************************/
+
+#if defined(HAVE_GROUP_MEMBERS) && \
+ (defined(CONFIG_SCHED_HAVE_PARENT) || defined(CONFIG_SCHED_WAITPID))
+static bool nxtask_lastmember(FAR struct tcb_s *tcb)
+{
+ FAR struct task_group_s *group = tcb->group;
+ FAR struct tcb_s *mtcb;
+ irqstate_t flags;
+ bool last = true;
+
+ flags = spin_lock_irqsave(&group->tg_lock);
+
+ tcb->flags |= TCB_FLAG_EXIT_COUNTED;
+ sq_for_every_entry(&group->tg_members, mtcb, struct tcb_s, member)
+ {
+ if ((mtcb->flags & TCB_FLAG_EXIT_COUNTED) == 0)
+ {
+ last = false;
+ break;
+ }
+ }
+
+ spin_unlock_irqrestore(&group->tg_lock, flags);
+ return last;
+}
+#endif
+
/****************************************************************************
* Name: nxtask_exitstatus
*
@@ -183,7 +229,7 @@ static inline void nxtask_sigchild(pid_t ppid, FAR struct
tcb_s *ctcb,
* should generate SIGCHLD.
*/
- if (sq_is_singular(&chgrp->tg_members))
+ if (nxtask_lastmember(ctcb))
{
/* Mark that all of the threads in the task group have exited */
@@ -387,8 +433,8 @@ static inline void nxtask_exitwakeup(FAR struct tcb_s *tcb,
int status)
/* Is this the last thread in the group? */
-#ifndef CONFIG_DISABLE_PTHREAD
- if (sq_is_singular(&group->tg_members))
+#ifdef HAVE_GROUP_MEMBERS
+ if (nxtask_lastmember(tcb))
#endif
{
/* Yes.. Wakeup any tasks waiting for this task to exit */