casaroli opened a new pull request, #22005:
URL: https://github.com/apache/nuttx/pull/22005

   ## Summary
   
   Only the last thread of a task group to exit sends `SIGCHLD` to the parent 
and marks the child status as exited. `nxtask_exithook()` finds 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 on SMP the other threads run at the same time. So when two threads of a 
group exit together, each one can see the other in the list: no thread sends 
`SIGCHLD`, and the parent waits in `waitpid()` for ever. The same test decides 
the wake-up of `waitpid()` without `CONFIG_SCHED_HAVE_PARENT`.
   
   This PR marks each exiting thread with `TCB_FLAG_EXIT_COUNTED` under 
`tg_lock` and tests in the same lock whether all of the members are marked. 
Exactly one thread, the last one, sees all of them marked. The exit status of 
the main thread is saved before its mark, so the `SIGCHLD` carries it. 
`nxtask_exithook()` runs in a critical section in all of its callers, as 
`group_cancel_children_handler()` does when it sets a flag of another thread.
   
   ## Impact
   
   - A parent no longer waits for ever for a child whose last two threads exit 
at the same time. Nothing changes when one thread exits after the other.
   - One new TCB flag bit (19). The test walks the member list of the group 
under `tg_lock`, once per exiting thread.
   - Size: qemu-armv8a:nsh +116 B, qemu-armv8a:knsh +128 B, mps2-an500:nsh with 
`CONFIG_SCHED_WAITPID` (no `CONFIG_SCHED_HAVE_PARENT`) +504 B; with 
`CONFIG_DISABLE_PTHREAD` nothing changes.
   
   ## Testing
   
   The hang is seen in CI on qemu-armv8a:citest_smp (NTFC): `getprime` ends 
while the thread it joined is still in its exit processing, and NSH waits for 
ever. With gdb on the hung QEMU: NSH (`sh`) waits for `SIGCHLD` in 
`nxsig_clockwait()` from `waitpid()`, the child status of `getprime` is in the 
list of NSH with `ch_flags = 0` (not exited), no signal is pending, and 
`getprime` and its thread are gone. It happens in 1 of 6 NTFC runs in the CI 
docker image and in 2 of 32 runs on QEMU TCG `-smp 4` on the Mac. In the same 
setup with this PR, 8 of 8 NTFC runs and 16 of 16 TCG runs pass.
   
   To show the race, a 50 ms `nxsched_usleep()` after `sched_unlock()` in 
`nxtask_exithook()` (only while another member is in the list; not part of this 
PR) makes master hang at the first `getprime` each time (2 of 2). With this PR 
and the same sleep, `getprime` returns 3 of 3 times (2 of 2 runs).
   
   | Test | master | this PR |
   |---|---|---|
   | citest_smp + 50 ms sleep: `getprime` x3 | hangs at the first | 3 of 3 
return |
   | ostest, qemu-armv8a:knsh (kernel, HVF) | status 0 | status 0 |
   | ostest, qemu-armv8a:nsh (flat, HVF) | status 0 | status 0 |
   
   Builds with `-Werror`, 0 warnings: qemu-armv8a:nsh (flat), qemu-armv8a:knsh 
(kernel), qemu-armv8a:citest_smp (SMP), rv-virt:pnsh64 (protected, rv64), 
mps2-an500:nsh with `CONFIG_SCHED_WAITPID`, mps2-an500:nsh with 
`CONFIG_DISABLE_PTHREAD`, `CONFIG_SCHED_HAVE_PARENT` and `CONFIG_SCHED_WAITPID`.
   
   `tools/checkpatch.sh -c -u -m -g` passes.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to