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 0e27bd4b6778632bf45a2a54f4bc05ac4e364613
Author: gaohedong <[email protected]>
AuthorDate: Mon Nov 9 18:11:16 2026 +0800

    netutils/iperf: wake and join report thread safely on stop
    
    The previous change blocks the stop signals in every thread except the
    traffic thread, so that a process-directed kill() always interrupts
    the traffic thread. The stop signal therefore can no longer interrupt
    the report thread's sleep(), and stopping a test waits up to a whole
    interval before the join returns. Wait on a semaphore with an interval
    timeout instead, so the stop path can wake it at once. A second wakeup
    signal would need another group-wide disposition to save and restore,
    and could be lost if sent before the thread enters sleep().
    
    Add iperf_stop_report() for the four traffic paths. It joins the report
    thread only if it was started, which fixes joining an invalid thread
    when pthread_create() failed or a UDP server stopped before its first
    packet.
    
    Signed-off-by: gaohedong <[email protected]>
    Assisted-by: Kiro:claude-opus-4
---
 netutils/iperf/iperf.c | 63 ++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 53 insertions(+), 10 deletions(-)

diff --git a/netutils/iperf/iperf.c b/netutils/iperf/iperf.c
index ab52b208d..824b39b45 100644
--- a/netutils/iperf/iperf.c
+++ b/netutils/iperf/iperf.c
@@ -31,6 +31,7 @@
 #include <netpacket/rpmsg.h>
 #include <pthread.h>
 #include <sched.h>
+#include <semaphore.h>
 #include <signal.h>
 #include <stdbool.h>
 #include <sys/prctl.h>
@@ -83,6 +84,7 @@ struct iperf_ctrl_t
   pid_t pid;
   pthread_t report_thread;
   pthread_t traffic_thread;
+  sem_t report_wakeup;
   volatile sig_atomic_t finish;
   uintmax_t total_len;
   uint32_t buffer_len;
@@ -125,6 +127,7 @@ inline static bool iperf_is_tcp_server(FAR struct 
iperf_ctrl_t *ctrl);
 static void iperf_stop_sigset(FAR sigset_t *set);
 static void iperf_stop_handler(int signo, FAR siginfo_t *info,
                                FAR void *context);
+static void iperf_stop_report(FAR struct iperf_ctrl_t *ctrl);
 static int iperf_get_socket_error_code(int sockfd);
 static int iperf_show_socket_error_reason(FAR const char *str, int sockfd);
 static FAR void *iperf_report_task(FAR void *arg);
@@ -220,7 +223,7 @@ static void iperf_stop_sigset(FAR sigset_t *set)
  *   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.
+ *   wakes the report thread through iperf_stop_report().
  *
  ****************************************************************************/
 
@@ -235,6 +238,29 @@ static void iperf_stop_handler(int signo, FAR siginfo_t 
*info,
     }
 }
 
+/****************************************************************************
+ * Name: iperf_stop_report
+ *
+ * Description:
+ *   Request stop, wake the report thread and join it.  The report thread
+ *   may never have been started (pthread_create() failed, or a UDP server
+ *   stopped before its first packet), in which case there is nothing to
+ *   join.  Called from ordinary thread context only.
+ *
+ ****************************************************************************/
+
+static void iperf_stop_report(FAR struct iperf_ctrl_t *ctrl)
+{
+  ctrl->finish = 1;
+  sem_post(&ctrl->report_wakeup);
+
+  if (ctrl->report_thread != 0)
+    {
+      pthread_join(ctrl->report_thread, NULL);
+      ctrl->report_thread = 0;
+    }
+}
+
 /****************************************************************************
  * Name: iperf_get_socket_error_code
  *
@@ -365,6 +391,7 @@ static FAR void *iperf_report_task(FAR void *arg)
   FAR struct iperf_ctrl_t *ctrl = arg;
   uint32_t interval = ctrl->cfg.interval;
   uint32_t time = ctrl->cfg.time;
+  struct timespec deadline;
   struct timespec now;
   struct timespec start;
   uintmax_t now_len;
@@ -387,7 +414,22 @@ static FAR void *iperf_report_task(FAR void *arg)
       uintmax_t last_len;
       struct timespec last;
 
-      sleep(interval);
+      /* Wait for the next interval, or for iperf_stop_report() to wake
+       * us up early when the test is stopped.
+       */
+
+      if (clock_gettime(CLOCK_MONOTONIC, &deadline) != 0)
+        {
+          break;
+        }
+
+      deadline.tv_sec += interval;
+      ret = sem_clockwait(&ctrl->report_wakeup, CLOCK_MONOTONIC, &deadline);
+      if (ctrl->finish || (ret < 0 && errno != ETIMEDOUT))
+        {
+          break;
+        }
+
       last_len = now_len;
       last = now;
       now_len = ctrl->total_len;
@@ -422,6 +464,8 @@ static FAR void *iperf_report_task(FAR void *arg)
              );
     }
 
+  /* Tell the traffic thread the test is over (e.g. -t elapsed). */
+
   ctrl->finish = 1;
   return NULL;
 }
@@ -680,8 +724,7 @@ static int iperf_tcp_server(FAR struct iperf_ctrl_t *ctrl,
       close(sockfd);
     }
 
-  ctrl->finish = true;
-  pthread_join(ctrl->report_thread, NULL);
+  iperf_stop_report(ctrl);
   close(listen_socket);
 
   return 0;
@@ -909,8 +952,7 @@ static int iperf_udp_server(FAR struct iperf_ctrl_t *ctrl,
         }
     }
 
-  ctrl->finish = true;
-  pthread_join(ctrl->report_thread, NULL);
+  iperf_stop_report(ctrl);
 
 #ifdef IPERF_HAVE_NETPOLL
   if ((ctrl->cfg.flag & IPERF_FLAG_NETPOLL) == 0)
@@ -1051,8 +1093,7 @@ static int iperf_udp_client(FAR struct iperf_ctrl_t *ctrl,
         }
     }
 
-  ctrl->finish = true;
-  pthread_join(ctrl->report_thread, NULL);
+  iperf_stop_report(ctrl);
 #ifdef IPERF_HAVE_NETPOLL
   if ((ctrl->cfg.flag & IPERF_FLAG_NETPOLL) == 0)
 #endif
@@ -1137,8 +1178,7 @@ static int iperf_tcp_client(FAR struct iperf_ctrl_t *ctrl,
         }
     }
 
-  ctrl->finish = true;
-  pthread_join(ctrl->report_thread, NULL);
+  iperf_stop_report(ctrl);
   close(sockfd);
 
   return 0;
@@ -1289,6 +1329,8 @@ int iperf_start(FAR struct iperf_cfg_t *cfg)
       return -1;
     }
 
+  sem_init(&ctrl.report_wakeup, 0, 0);
+
   memset(ctrl.buffer, 0, ctrl.buffer_len);
   pthread_attr_init(&attr);
   param.sched_priority = IPERF_TRAFFIC_TASK_PRIORITY;
@@ -1370,6 +1412,7 @@ int iperf_start(FAR struct iperf_cfg_t *cfg)
   sigaction(SIGUSR1, &oldusr1, NULL);
 
   free(ctrl.buffer);
+  sem_destroy(&ctrl.report_wakeup);
   return ret;
 }
 

Reply via email to