sched/tcb: fix 12320 regression

This patch attempts to fix regressions reported for pull# 12320.

Signed-off-by: Yanfeng Liu <yfliu2008@qq.com>
diff --git a/sched/init/nx_bringup.c b/sched/init/nx_bringup.c
index c1944a4..152dcc8 100644
--- a/sched/init/nx_bringup.c
+++ b/sched/init/nx_bringup.c
@@ -529,9 +529,11 @@
 
 #if !defined(CONFIG_DISABLE_ENVIRON) && (defined(CONFIG_PATH_INITIAL) || \
      defined(CONFIG_LDPATH_INITIAL))
-  /* We an save a few bytes by discarding the IDLE thread's environment. */
+  /* We would save a few bytes by discarding the IDLE thread's environment.
+   * But when kthreads share the same group, this is no longer proper, so
+   * we can't do clearenv() now.
+   */
 
-  clearenv();
 #endif
 
   sched_trace_end();
diff --git a/sched/sched/sched_releasetcb.c b/sched/sched/sched_releasetcb.c
index 9c74bf1..594da26 100644
--- a/sched/sched/sched_releasetcb.c
+++ b/sched/sched/sched_releasetcb.c
@@ -170,9 +170,9 @@
 
       nxtask_joindestroy(tcb);
 
-      /* Kernel thread and group still reference by pthread */
+      /* Task still referenced by pthread */
 
-      if (ttype != TCB_FLAG_TTYPE_PTHREAD)
+      if (ttype == TCB_FLAG_TTYPE_TASK)
         {
           ttcb = (FAR struct task_tcb_s *)tcb;
           if (!sq_empty(&ttcb->group.tg_members)
diff --git a/sched/task/exit.c b/sched/task/exit.c
index 794bc6e..5971aa9 100644
--- a/sched/task/exit.c
+++ b/sched/task/exit.c
@@ -64,7 +64,11 @@
    * exit through a different mechanism.
    */
 
-  group_kill_children(tcb);
+  if ((tcb->flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_KERNEL)
+    {
+      group_kill_children(tcb);
+    }
+
 #endif
 
   /* Perform common task termination logic.  This will get called again later