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

   depends-on: apache/nuttx/pull/21778
   
   ## Summary
   
   When a thread calls `exit()` while the main thread waits for it in 
`pthread_join()`, the process never ends in a protected or kernel build with 
`CONFIG_SIG_DEFAULT=y`. The shell that started it waits forever.
   
   Cause: with `CONFIG_GROUP_KILL_CHILDREN_TIMEOUT_MS=-1` (the default), 
`group_kill_children()` sends `SIGTERM` to the other threads and waits until 
they all exit. `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 the join, and it waits for the joiner: a 
deadlock.
   
   This PR has two commits:
   
   1. `sched/group`: stop the wait when each other thread of the group waits in 
`pthread_join()` on its `join_sem`. Then nothing can end those waits, and the 
threads are canceled at once, as they were after the wait. The check reads 
`tg_members` with `tg_lock` held (as `group_join()`/`group_leave()` do) and the 
task states in the critical section that `group_kill_children()` already holds. 
This fix alone removes the hang with the timeout -1.
   2. `sched/Kconfig`: make `GROUP_KILL_CHILDREN_TIMEOUT_MS` 0 by default. Then 
`exit()` cancels the other threads at once and does not send `SIGTERM`, as 
POSIX `exit()` and the default action of a fatal signal do. With -1, any thread 
that blocks or catches `SIGTERM`, or waits without interruption in another 
system call, still keeps the process alive. The help text is corrected (the 
signal is `SIGTERM`, not `SIGQUIT`; `task_restart()` also uses the option).
   
   ## Impact
   
   Only configurations with `CONFIG_SIG_DEFAULT=y` and 
`CONFIG_SIG_SIGKILL_ACTION=y` (about 140 defconfigs, and ESP32-S3 selects 
`SIG_DEFAULT`) have the option.
   
   Commit 1: no change unless all other threads wait in `pthread_join()`; then 
`exit()` ends instead of hanging.
   
   Commit 2 is a behavior change: by default, `exit()` (and `task_restart()`) 
no longer sends `SIGTERM` to the other threads and no longer waits for them; it 
cancels them at once. A `SIGTERM` handler of another thread does not run on 
`exit()` any more. A board that needs the old behavior sets the option to -1 or 
to a timeout in milliseconds. No defconfig and no file in Documentation/ or in 
nuttx-apps refers to the option. No API or ABI change. With the option at 0 the 
signal-and-wait code is not compiled, so the image gets smaller.
   
   The `depends-on` is for the CI: master does not link `verdin-mx8mp:rpmsg` 
without #21729.
   
   ## Testing
   
   Test program `exitjoin`: main creates a thread and calls `pthread_join()`; 
the thread calls `exit(7)`. Also the LTP cases `pthread_spin_init/4-1` and 
`pthread_spin_destroy/3-1` (both end with `exit()` in a thread while main 
joins). Each run: `exitjoin` 5 times, the two LTP cases, `ostest`, `exitjoin` 
again. Master is 52e6c0abcab.
   
   | Configuration | Timeout | Master | This PR |
   |---|---|---|---|
   | `qemu-armv8a:knsh` (HVF), `SIG_DEFAULT` | -1 | hang: `exitjoin` and both 
LTP cases never end (`ps`: main `Waiting Semaphore`) | pass, `ostest` status 0 |
   | `qemu-armv8a:knsh` (HVF), `SIG_DEFAULT` | 0 | pass, `ostest` status 0 | 
pass, `ostest` status 0 (new default) |
   | `rv-virt:knsh64` (TCG), `SIG_DEFAULT` | -1 | hang (same) | pass, `ostest` 
status 0 |
   | `rv-virt:knsh64` (TCG), `SIG_DEFAULT` | 0 | pass, `ostest` status 0 | 
pass, `ostest` status 0 (new default) |
   | `mps2-an500:knsh` (protected, TCG), `SIG_DEFAULT`, `exitjoin` 3 times | -1 
| hang at the first run | pass |
   | `mps2-an500:knsh` (protected, TCG), `SIG_DEFAULT`, `exitjoin` 3 times | 0 
| - | pass (new default) |
   | `qemu-armv8a:nsh` (flat, HVF), `exitjoin` 5 times | - | - | pass, `ostest` 
status 0 |
   | `qemu-armv8a:nsh_smp` (flat, 2 CPUs, HVF), `SIG_DEFAULT`, `exitjoin` 5 
times | -1 | - | pass, `ostest` status 0 |
   
   All builds of this PR with `EXTRAFLAGS=-Werror`. The runs with the timeout 
-1 compile and run the new check (with 0 it is not compiled). 
`tools/checkpatch.sh -c -u -m -g` passes on both commits.
   


-- 
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