Skip to content

Commit 9638187

Browse files
pussuwxiaoxiang781216
authored andcommitted
sched/nxtask_sigchild: Set process exit code to group exit code
There is an issue where the wrong process exit code is given to the parent when a process exits. This happens when the process has pthreads running user code i.e. not within a cancel point / system call. Why does this happen ? When exit() is called, the following steps are done: - group_kill_children(), which tells the children to die via pthread_cancel() Then, one of two things can happen: 1. if the child is in a cancel point, it gets scheduled to allow it to leave the cancel point and gets destroyed immediately 2. if the child is not in a cancel point, a "cancel pending" flag is set and the child will die when the next cancel point is encountered So what is the problem here? The last thread alive dispatches SIGCHLD to the parent, which carries the process's exit code. The group head has the only meaningful exit code and this is what should be passed. However, in the second case, the group head exits before the child, taking the process exit code to its grave. The child that was alive will exit next and will pass its "status" to the parent process, but this status is not the correct value to pass. This commit fixes the issue by passing the group head's exit code ALWAYS to the parent process.
1 parent 1949991 commit 9638187

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

include/nuttx/sched.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,9 @@ struct task_group_s
454454
#else
455455
uint16_t tg_nchildren; /* This is the number active children */
456456
#endif
457+
/* Group exit status ******************************************************/
458+
459+
int tg_exitcode; /* Exit code (status) for group */
457460
#endif /* CONFIG_SCHED_HAVE_PARENT */
458461

459462
#if defined(CONFIG_SCHED_WAITPID) && !defined(CONFIG_SCHED_HAVE_PARENT)

sched/task/task_exithook.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static inline void nxtask_exitstatus(FAR struct task_group_s *group,
8080
}
8181
#else
8282

83-
# define nxtask_exitstatus(group,status)
83+
# define nxtask_exitstatus(group,status) (group)->tg_exitcode = (status);
8484

8585
#endif /* CONFIG_SCHED_CHILD_STATUS */
8686

@@ -188,7 +188,7 @@ static inline void nxtask_sigchild(pid_t ppid, FAR struct tcb_s *ctcb,
188188
info.si_errno = OK;
189189
info.si_value.sival_ptr = NULL;
190190
info.si_pid = chgrp->tg_pid;
191-
info.si_status = status;
191+
info.si_status = pgrp->tg_exitcode;
192192

193193
/* Send the signal to one thread in the group */
194194

0 commit comments

Comments
 (0)