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-apps.git

commit 727afcd59d0541c9c1c1cc8b4c55b991f71cc713
Author: gaohedong <[email protected]>
AuthorDate: Mon Nov 9 18:12:19 2026 +0800

    netutils/iperf: fix abort across task groups
    
    iperf -a runs in its own task group, so pthread_kill() with a traffic
    thread id from another group cannot reach the instance. Wake each
    instance with kill() on its pid instead.
    
    Block the stop signals in every thread except the traffic thread, so a
    process-directed signal is always taken there and interrupts its
    blocking socket call with EINTR. Pass ctrl to the handler via sa_user,
    publish the instance under the list lock before creating the traffic
    thread, unlink it only after the join, and restore the signal mask
    before the old dispositions so a pending stop signal is still handled
    while ctrl is valid.
    
    Signed-off-by: gaohedong <[email protected]>
    Assisted-by: Kiro:claude-opus-4
---
 netutils/iperf/iperf.c | 220 +++++++++++++++++++++++++++++--------------------
 1 file changed, 129 insertions(+), 91 deletions(-)

diff --git a/netutils/iperf/iperf.c b/netutils/iperf/iperf.c
index f9f4a4ece..ab52b208d 100644
--- a/netutils/iperf/iperf.c
+++ b/netutils/iperf/iperf.c
@@ -80,9 +80,10 @@ struct iperf_ctrl_t
 {
   FAR struct iperf_ctrl_t *flink;
   struct iperf_cfg_t cfg;
+  pid_t pid;
   pthread_t report_thread;
   pthread_t traffic_thread;
-  bool finish;
+  volatile sig_atomic_t finish;
   uintmax_t total_len;
   uint32_t buffer_len;
   FAR uint8_t *buffer;
@@ -121,17 +122,18 @@ inline static bool iperf_is_udp_client(FAR struct 
iperf_ctrl_t *ctrl);
 inline static bool iperf_is_udp_server(FAR struct iperf_ctrl_t *ctrl);
 inline static bool iperf_is_tcp_client(FAR struct iperf_ctrl_t *ctrl);
 inline static bool iperf_is_tcp_server(FAR struct iperf_ctrl_t *ctrl);
-static void iperf_wakeup_handler(int signo);
-static void iperf_stop_handler(int signo);
+static void iperf_stop_sigset(FAR sigset_t *set);
+static void iperf_stop_handler(int signo, FAR siginfo_t *info,
+                               FAR void *context);
 static int iperf_get_socket_error_code(int sockfd);
 static int iperf_show_socket_error_reason(FAR const char *str, int sockfd);
-static void iperf_report_task(FAR void *arg);
+static FAR void *iperf_report_task(FAR void *arg);
 static int iperf_start_report(FAR struct iperf_ctrl_t *ctrl);
 static int iperf_run_tcp_server(FAR struct iperf_ctrl_t *ctrl);
 static int iperf_run_udp_server(FAR struct iperf_ctrl_t *ctrl);
 static int iperf_run_udp_client(FAR struct iperf_ctrl_t *ctrl);
 static int iperf_run_tcp_client(FAR struct iperf_ctrl_t *ctrl);
-static void iperf_task_traffic(FAR void *arg);
+static FAR void *iperf_task_traffic(FAR void *arg);
 static uint32_t iperf_get_buffer_len(FAR struct iperf_ctrl_t *ctrl);
 
 /****************************************************************************
@@ -195,67 +197,41 @@ inline static bool iperf_is_tcp_server(FAR struct 
iperf_ctrl_t *ctrl)
 }
 
 /****************************************************************************
- * Name: iperf_wakeup_handler
+ * Name: iperf_stop_sigset
  *
  * Description:
- *   Empty handler for the signal used to wake the traffic thread.
- *
- *   The body is intentionally empty and must stay that way: what releases
- *   the traffic thread from accept() or recv() is the delivery of the
- *   signal itself, which completes the call with EINTR, not anything done
- *   here.  The handler exists only so the signal has a disposition that
- *   does not terminate the task; SIG_IGN would not do, since an ignored
- *   signal does not interrupt a blocking call.
+ *   Build the signal set used to stop a traffic thread.
  *
  ****************************************************************************/
 
-static void iperf_wakeup_handler(int signo)
+static void iperf_stop_sigset(FAR sigset_t *set)
 {
-  UNUSED(signo);
+  sigemptyset(set);
+  sigaddset(set, SIGINT);
+  sigaddset(set, SIGTERM);
+  sigaddset(set, SIGPIPE);
+  sigaddset(set, SIGUSR1);
 }
 
 /****************************************************************************
  * Name: iperf_stop_handler
  *
  * Description:
- *   Ask every running instance to stop: set the finish flag and wake its
- *   traffic thread out of any blocking socket call.  iperf_stop() reuses it
- *   from ordinary context under the list mutex.
- *
- *   Where the default action terminates the task, it does so on the spot:
- *   iperf_start() never reaches the code that unlinks the instance, and the
- *   traffic thread never closes its listening socket, so the port stays
- *   bound and a later run fails with EADDRINUSE.  Requesting a stop instead
- *   lets the existing exit paths run to completion.
- *
- *   Only async-signal-safe work is done here: the finish flag is a single
- *   word, and pthread_kill() is on the list of functions a handler may
- *   call.  The heavier teardown -- unlinking, close(), free() -- is left to
- *   the threads themselves once they unwind.
- *
- *   The kill is what actually ends the test.  Setting finish alone is not
- *   enough for a traffic thread parked in accept() or recv(), which does
- *   not re-test the flag until its current call returns; a server with no
- *   client would never return.  Directing the signal at that thread
- *   completes its blocking call with EINTR regardless of which group member
- *   the kernel happens to run this handler on.
+ *   Request a graceful stop of this iperf instance.  Only sets the finish
+ *   flag; delivery of the signal itself interrupts a blocking socket call
+ *   on the traffic thread with EINTR.  The traffic thread's exit path then
+ *   stops and joins the report thread.
  *
  ****************************************************************************/
 
-static void iperf_stop_handler(int signo)
+static void iperf_stop_handler(int signo, FAR siginfo_t *info,
+                               FAR void *context)
 {
-  FAR sq_entry_t *p;
+  FAR struct iperf_ctrl_t *ctrl = info->si_user;
 
-  sq_for_every(&g_iperf_ctrl_list, p)
+  if (ctrl != NULL)
     {
-      FAR struct iperf_ctrl_t *ctrl = (FAR struct iperf_ctrl_t *)p;
-
-      ctrl->finish = true;
-
-      if (ctrl->traffic_thread != 0)
-        {
-          pthread_kill(ctrl->traffic_thread, SIGUSR1);
-        }
+      ctrl->finish = 1;
     }
 }
 
@@ -384,7 +360,7 @@ static double ts_diff(const struct timespec *a, const 
struct timespec *b)
  *
  ****************************************************************************/
 
-static void iperf_report_task(FAR void *arg)
+static FAR void *iperf_report_task(FAR void *arg)
 {
   FAR struct iperf_ctrl_t *ctrl = arg;
   uint32_t interval = ctrl->cfg.interval;
@@ -446,9 +422,8 @@ static void iperf_report_task(FAR void *arg)
              );
     }
 
-  ctrl->finish = true;
-
-  pthread_exit(NULL);
+  ctrl->finish = 1;
+  return NULL;
 }
 
 /****************************************************************************
@@ -464,6 +439,8 @@ static int iperf_start_report(FAR struct iperf_ctrl_t *ctrl)
   struct sched_param param;
   pthread_attr_t attr;
   pthread_t thread;
+  sigset_t oldset;
+  sigset_t set;
   int ret;
 
   pthread_attr_init(&attr);
@@ -471,14 +448,21 @@ static int iperf_start_report(FAR struct iperf_ctrl_t 
*ctrl)
   pthread_attr_setschedparam(&attr, &param);
   pthread_attr_setstacksize(&attr, IPERF_REPORT_TASK_STACK);
 
-  ret = pthread_create(&thread, &attr, (FAR void *)iperf_report_task,
-                       ctrl);
+  /* Create the report thread with the stop signals blocked so it inherits
+   * the mask; process-directed stop signals must reach the traffic thread.
+   * pthread_sigmask() only fails for an invalid 'how'.
+   */
+
+  iperf_stop_sigset(&set);
+  pthread_sigmask(SIG_BLOCK, &set, &oldset);
+  ret = -pthread_create(&thread, &attr, iperf_report_task, ctrl);
+  pthread_sigmask(SIG_SETMASK, &oldset, NULL);
   pthread_attr_destroy(&attr);
-  if (ret != 0)
+  if (ret < 0)
     {
       printf("iperf_thread: pthread_create failed: %d, %s\n",
-             ret, IPERF_REPORT_TASK_NAME);
-      return -1;
+             -ret, IPERF_REPORT_TASK_NAME);
+      return ret;
     }
 
   ctrl->report_thread = thread;
@@ -677,14 +661,14 @@ static int iperf_tcp_server(FAR struct iperf_ctrl_t *ctrl,
                * exits after finishing a single connection.
                */
 
-              ctrl->finish = true;
+              ctrl->finish = 1;
               break;
             }
           else if (actual_recv < 0)
             {
               iperf_show_socket_error_reason("tcp server recv",
                                              sockfd);
-              ctrl->finish = true;
+              ctrl->finish = 1;
               break;
             }
           else
@@ -871,6 +855,16 @@ static int iperf_udp_server(FAR struct iperf_ctrl_t *ctrl,
 #ifdef IPERF_HAVE_NETPOLL
       if ((ctrl->cfg.flag & IPERF_FLAG_NETPOLL) != 0)
         {
+          /* netpoll receive has no cancellation point and cannot be
+           * interrupted by the stop signal.  Avoid entering it after an
+           * abort.
+           */
+
+          if (ctrl->finish)
+            {
+              break;
+            }
+
           actual_recv = netpoll_udp_recv(&ctrl->netpoll, buffer, want_recv);
 
           /* netpoll_udp_recv() does not fill remote_addr the way recvfrom()
@@ -1171,12 +1165,28 @@ static int iperf_run_tcp_client(FAR struct iperf_ctrl_t 
*ctrl)
  *
  ****************************************************************************/
 
-static void iperf_task_traffic(FAR void *arg)
+static FAR void *iperf_task_traffic(FAR void *arg)
 {
   FAR struct iperf_ctrl_t *ctrl = arg;
+  sigset_t set;
 
   prctl(PR_SET_NAME, IPERF_TRAFFIC_TASK_NAME);
 
+  /* The only thread in the group that takes the stop signals.  A signal
+   * that arrived while all threads had it blocked is delivered here and
+   * sets finish before any socket call.
+   */
+
+  iperf_stop_sigset(&set);
+  pthread_sigmask(SIG_UNBLOCK, &set, NULL);
+
+  if (ctrl->finish)
+    {
+      /* Stopped before the test started; skip the socket setup. */
+
+      return NULL;
+    }
+
   if (iperf_is_udp_client(ctrl))
     {
       iperf_run_udp_client(ctrl);
@@ -1201,7 +1211,7 @@ static void iperf_task_traffic(FAR void *arg)
     }
 
   printf("iperf exit\n");
-  pthread_exit(NULL);
+  return NULL;
 }
 
 static uint32_t iperf_get_buffer_len(FAR struct iperf_ctrl_t *ctrl)
@@ -1259,7 +1269,8 @@ int iperf_start(FAR struct iperf_cfg_t *cfg)
   struct sigaction oldpipe;
   struct sigaction oldusr1;
   pthread_attr_t attr;
-  FAR void *retval;
+  sigset_t oldset;
+  sigset_t set;
   int ret;
 
   if (!cfg)
@@ -1269,7 +1280,7 @@ int iperf_start(FAR struct iperf_cfg_t *cfg)
 
   memset(&ctrl, 0, sizeof(ctrl));
   memcpy(&ctrl.cfg, cfg, sizeof(*cfg));
-  ctrl.finish = false;
+  ctrl.pid = getpid();
   ctrl.buffer_len = iperf_get_buffer_len(&ctrl);
   ctrl.buffer = (FAR uint8_t *)malloc(ctrl.buffer_len);
   if (ctrl.buffer == NULL)
@@ -1298,53 +1309,68 @@ int iperf_start(FAR struct iperf_cfg_t *cfg)
    * learn when to quit, and losing it leaves the terminal unable to exit.
    */
 
+  /* Block the stop signals here so that only the traffic thread, which
+   * unblocks them, can take a process-directed kill().
+   */
+
+  iperf_stop_sigset(&set);
+  pthread_sigmask(SIG_BLOCK, &set, &oldset);
+
   memset(&act, 0, sizeof(act));
+  act.sa_sigaction = iperf_stop_handler;
+  act.sa_flags = SA_SIGINFO;
+  act.sa_user = &ctrl;
   sigemptyset(&act.sa_mask);
-  act.sa_handler = iperf_stop_handler;
   sigaction(SIGINT, &act, &oldint);
   sigaction(SIGTERM, &act, &oldterm);
   sigaction(SIGPIPE, &act, &oldpipe);
-
-  act.sa_handler = iperf_wakeup_handler;
   sigaction(SIGUSR1, &act, &oldusr1);
 
-  ret = pthread_create(&ctrl.traffic_thread, &attr,
-                       (FAR void *)iperf_task_traffic, &ctrl);
-  pthread_attr_destroy(&attr);
-  if (ret != 0)
-    {
-      printf("iperf_task_traffic: create task failed: %d\n", ret);
-
-      sigaction(SIGINT, &oldint, NULL);
-      sigaction(SIGTERM, &oldterm, NULL);
-      sigaction(SIGPIPE, &oldpipe, NULL);
-      sigaction(SIGUSR1, &oldusr1, NULL);
-      free(ctrl.buffer);
-      ctrl.buffer = NULL;
-      return -1;
-    }
-
-  /* Publish the instance before waiting for the worker to finish, so the
-   * stop path can find it.
+  /* Add the instance and create its traffic thread while holding the list
+   * lock, so abort cannot observe a half-created instance.
    */
 
   pthread_mutex_lock(&g_iperf_ctrl_mutex);
   sq_addlast((FAR sq_entry_t *)&ctrl, &g_iperf_ctrl_list);
+  ret = -pthread_create(&ctrl.traffic_thread, &attr,
+                        iperf_task_traffic, &ctrl);
+  if (ret < 0)
+    {
+      sq_rem((FAR sq_entry_t *)&ctrl, &g_iperf_ctrl_list);
+    }
+
   pthread_mutex_unlock(&g_iperf_ctrl_mutex);
+  pthread_attr_destroy(&attr);
+
+  if (ret == 0)
+    {
+      pthread_join(ctrl.traffic_thread, NULL);
 
-  pthread_join(ctrl.traffic_thread, &retval);
+      /* Unlink only after the join, so iperf_stop() can reach the
+       * instance for as long as its traffic thread runs.
+       */
 
-  pthread_mutex_lock(&g_iperf_ctrl_mutex);
-  sq_rem((FAR sq_entry_t *)&ctrl, &g_iperf_ctrl_list);
-  pthread_mutex_unlock(&g_iperf_ctrl_mutex);
+      pthread_mutex_lock(&g_iperf_ctrl_mutex);
+      sq_rem((FAR sq_entry_t *)&ctrl, &g_iperf_ctrl_list);
+      pthread_mutex_unlock(&g_iperf_ctrl_mutex);
+    }
+  else
+    {
+      printf("iperf_task_traffic: create task failed: %d\n", -ret);
+    }
+
+  /* Restore the mask before the dispositions, so a pending stop signal is
+   * still handled by iperf_stop_handler while ctrl is valid.
+   */
 
+  pthread_sigmask(SIG_SETMASK, &oldset, NULL);
   sigaction(SIGINT, &oldint, NULL);
   sigaction(SIGTERM, &oldterm, NULL);
   sigaction(SIGPIPE, &oldpipe, NULL);
   sigaction(SIGUSR1, &oldusr1, NULL);
 
   free(ctrl.buffer);
-  return 0;
+  return ret;
 }
 
 /****************************************************************************
@@ -1357,9 +1383,21 @@ int iperf_start(FAR struct iperf_cfg_t *cfg)
 
 int iperf_stop(void)
 {
+  FAR sq_entry_t *p;
+
+  /* Each instance may run in its own task group, so wake it with a
+   * process-directed signal; its traffic thread is the only thread that
+   * accepts it.
+   */
+
   pthread_mutex_lock(&g_iperf_ctrl_mutex);
-  iperf_stop_handler(0);
-  pthread_mutex_unlock(&g_iperf_ctrl_mutex);
+  sq_for_every(&g_iperf_ctrl_list, p)
+    {
+      FAR struct iperf_ctrl_t *ctrl = (FAR struct iperf_ctrl_t *)p;
+
+      kill(ctrl->pid, SIGUSR1);
+    }
 
+  pthread_mutex_unlock(&g_iperf_ctrl_mutex);
   return 0;
 }

Reply via email to