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

commit 9cc9b17878e95dd74f811a2ee397f4c3a6d8b9ee
Author: Marco Casaroli <[email protected]>
AuthorDate: Wed Nov 11 20:02:54 2026 +0100

    sched/group: Do not wait for the threads that wait in pthread_join().
    
    When a thread calls exit() and CONFIG_GROUP_KILL_CHILDREN_TIMEOUT_MS is
    not 0, group_kill_children() sends SIGTERM to the other threads of the
    group and waits for them to exit.  With a negative value (the default),
    it waits until they all exit.
    
    A thread that waits in pthread_join() never exits there.  pthread_join()
    waits without interruption, and in a system call the default action of
    SIGTERM runs only when the call returns.  Only the thread that calls
    exit() can end this wait, when it exits.  Thus, when a thread calls
    exit() while the main thread waits for it in pthread_join(), the process
    never ends.  This occurs in a protected or kernel build with SIG_DEFAULT.
    
    Stop the wait when each other thread of the group waits in pthread_join()
    on its join semaphore.  The threads are then canceled, as before after
    the wait.  Read the member list with tg_lock held.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 sched/group/group_killchildren.c | 49 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)

diff --git a/sched/group/group_killchildren.c b/sched/group/group_killchildren.c
index 6af686e21b3..5a7345fdfed 100644
--- a/sched/group/group_killchildren.c
+++ b/sched/group/group_killchildren.c
@@ -30,11 +30,14 @@
 #include <nuttx/debug.h>
 #include <pthread.h>
 #include <sched.h>
+#include <stdbool.h>
 #include <stdint.h>
 #include <sys/types.h>
 
+#include <nuttx/nuttx.h>
 #include <nuttx/sched.h>
 #include <nuttx/signal.h>
+#include <nuttx/spinlock.h>
 
 #include "sched/sched.h"
 #include "group/group.h"
@@ -73,6 +76,49 @@ static int group_kill_children_handler(pid_t pid, FAR void 
*arg)
 
   return OK;
 }
+
+/****************************************************************************
+ * Name: group_kill_children_joining
+ *
+ * Description:
+ *   Check if each member of the group, except tcb, waits in pthread_join().
+ *   Such a member waits for tcb or for another member that also waits in
+ *   pthread_join().  Only tcb can end these waits, when it exits.  The
+ *   SIGTERM does not end them: pthread_join() waits without interruption,
+ *   and in a system call the default action runs only when the call
+ *   returns.
+ *
+ * Input Parameters:
+ *   tcb - TCB of the task to be retained.
+ *
+ * Returned Value:
+ *   true if each other member of the group waits in pthread_join().
+ *
+ ****************************************************************************/
+
+static bool group_kill_children_joining(FAR struct tcb_s *tcb)
+{
+  FAR struct task_group_s *group = tcb->group;
+  FAR struct tcb_s *mtcb;
+  FAR sq_entry_t *curr;
+  irqstate_t flags;
+  bool joining = true;
+
+  flags = spin_lock_irqsave(&group->tg_lock);
+  sq_for_every(&group->tg_members, curr)
+    {
+      mtcb = container_of(curr, struct tcb_s, member);
+      if (mtcb != tcb && (mtcb->task_state != TSTATE_WAIT_SEM ||
+                          mtcb->waitobj != &mtcb->join_sem))
+        {
+          joining = false;
+          break;
+        }
+    }
+
+  spin_unlock_irqrestore(&group->tg_lock, flags);
+  return joining;
+}
 #endif
 
 /****************************************************************************
@@ -186,7 +232,8 @@ int group_kill_children(FAR struct tcb_s *tcb)
       while (1)
         {
           if (sq_empty(&tcb->group->tg_members) ||
-              sq_is_singular(&tcb->group->tg_members))
+              sq_is_singular(&tcb->group->tg_members) ||
+              group_kill_children_joining(tcb))
             {
               break;
             }

Reply via email to