This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 8a282a51d4259e5f05a2cb08b60f942176112f17
Author: Marco Casaroli <[email protected]>
AuthorDate: Mon Nov 16 21:18:42 2026 +0100

    sched/task: Remove the child entry of a task that never ran.
    
    nxtask_setup_scheduler() makes the new task a child of the caller: with
    CONFIG_SCHED_CHILD_STATUS it adds a child status entry to the group of
    the caller, else it increments tg_nchildren.  Only the exit of the
    child, waitpid(), waitid() and the reparent functions undo that.  When
    the task never runs, nothing undoes it: after an error in nxtask_init()
    after nxtask_setup_scheduler(), and after nxtask_uninit() (for example
    posix_spawn() with POSIX_SPAWN_SETSCHEDPARAM and priority 0, which
    fails in spawn_execattrs()).
    
    So each such error leaves a child that does not exist.  Its entry stays
    in the group until the caller ends (heap memory after the preallocated
    entries), and waitpid(-1) and wait() do not return ECHILD after the
    caller reaped all its real children: with WNOHANG they return 0, else
    they wait for ever.
    
    Remove the entry (or decrement tg_nchildren under tg_lock) in both
    undo paths.  The caller of nxtask_init() and nxtask_uninit() is the
    parent that nxtask_setup_scheduler() used.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 sched/task/task_init.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/sched/task/task_init.c b/sched/task/task_init.c
index 9d590079589..3d5f38cb3d9 100644
--- a/sched/task/task_init.c
+++ b/sched/task/task_init.c
@@ -43,6 +43,43 @@
 #include "task/task.h"
 #include "tls/tls.h"
 
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nxtask_drop_child
+ *
+ * Description:
+ *   Remove the child entry that nxtask_setup_scheduler() added to the
+ *   group of the caller, for a new task that never ran.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_SCHED_HAVE_PARENT
+static void nxtask_drop_child(FAR struct tcb_s *tcb)
+{
+  FAR struct task_group_s *pgrp = this_task()->group;
+#  ifndef CONFIG_SCHED_CHILD_STATUS
+  irqstate_t flags;
+#  endif
+
+#  ifdef CONFIG_SCHED_CHILD_STATUS
+  group_free_child(group_remove_child(pgrp, tcb->pid));
+#  else
+  flags = spin_lock_irqsave(&pgrp->tg_lock);
+  if (pgrp->tg_nchildren > 0)
+    {
+      pgrp->tg_nchildren--;
+    }
+
+  spin_unlock_irqrestore(&pgrp->tg_lock, flags);
+#  endif
+}
+#else
+#  define nxtask_drop_child(tcb)
+#endif
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -231,6 +268,7 @@ errout_with_group:
       dq_rem((FAR dq_entry_t *)tcb, list_inactivetasks());
       leave_critical_section(flags);
 
+      nxtask_drop_child(tcb);
       nxsched_releasepid(tcb->pid);
       tcb->pid = 0;
     }
@@ -288,6 +326,10 @@ errout_with_group:
 
 void nxtask_uninit(FAR struct tcb_s *tcb)
 {
+  /* The task never ran: the caller has no child to wait for */
+
+  nxtask_drop_child(tcb);
+
   /* Release all resources associated with the TCB... Including the TCB
    * itself.
    */

Reply via email to