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 9b1e2d5f3837059acded8fb98e5c85a9fd443865
Author: gaohedong <[email protected]>
AuthorDate: Mon Nov 9 18:12:39 2026 +0800

    netutils/iperf: retry abort and report incomplete stop
    
    A stop signal that lands after the traffic thread has checked finish
    but before it blocks in accept() or recv() is consumed by the handler
    without waking it, and the thread then blocks with nobody left to wake
    it. Re-send the signal every 100 ms until every instance has unlinked
    itself, which happens only after its traffic thread is joined.
    
    An instance whose task is gone (kill() fails with ESRCH) is not waited
    for.
    
    Give up after 5 s and return -ETIMEDOUT, e.g. for the netpoll receive
    path, which cannot be interrupted. iperf -a now exits with EXIT_FAILURE
    in that case instead of always reporting success.
    
    Signed-off-by: gaohedong <[email protected]>
    Assisted-by: Kiro:claude-opus-4
---
 netutils/iperf/iperf.c      | 58 +++++++++++++++++++++++++++++++++++----------
 netutils/iperf/iperf_main.c | 12 +++++++---
 2 files changed, 54 insertions(+), 16 deletions(-)

diff --git a/netutils/iperf/iperf.c b/netutils/iperf/iperf.c
index 824b39b45..df29c25bf 100644
--- a/netutils/iperf/iperf.c
+++ b/netutils/iperf/iperf.c
@@ -72,6 +72,10 @@
 
 #define IPERF_MAX_DELAY              64
 #define IPERF_SOCKET_RX_TIMEOUT      10
+#define IPERF_ABORT_TIMEOUT          5
+#define IPERF_ABORT_RETRY_MSEC       100
+#define IPERF_ABORT_RETRIES          \
+  (IPERF_ABORT_TIMEOUT * 1000 / IPERF_ABORT_RETRY_MSEC)
 
 /****************************************************************************
  * Private Types
@@ -898,9 +902,9 @@ 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.
+          /* netpoll receive has no cancellation point.  Avoid entering it
+           * after an abort; an in-progress call is reported by iperf_stop()
+           * when its bounded wait expires.
            */
 
           if (ctrl->finish)
@@ -1388,8 +1392,8 @@ int iperf_start(FAR struct iperf_cfg_t *cfg)
     {
       pthread_join(ctrl.traffic_thread, NULL);
 
-      /* Unlink only after the join, so iperf_stop() can reach the
-       * instance for as long as its traffic thread runs.
+      /* Unlink only after the join: iperf_stop() treats an empty list as
+       * "all stopped" and must never see a ctrl whose stack is gone.
        */
 
       pthread_mutex_lock(&g_iperf_ctrl_mutex);
@@ -1427,20 +1431,48 @@ int iperf_start(FAR struct iperf_cfg_t *cfg)
 int iperf_stop(void)
 {
   FAR sq_entry_t *p;
+  int retry;
+  int count;
 
-  /* 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.
+  /* A kill that lands after the traffic thread checked finish but before
+   * it blocked is consumed by the handler without waking it, so keep
+   * re-sending until every instance has unregistered or the retry budget
+   * runs out.  Only interruptible waits can be broken this way.
    */
 
-  pthread_mutex_lock(&g_iperf_ctrl_mutex);
-  sq_for_every(&g_iperf_ctrl_list, p)
+  for (retry = 0; ; retry++)
     {
-      FAR struct iperf_ctrl_t *ctrl = (FAR struct iperf_ctrl_t *)p;
+      count = 0;
+      pthread_mutex_lock(&g_iperf_ctrl_mutex);
+      sq_for_every(&g_iperf_ctrl_list, p)
+        {
+          FAR struct iperf_ctrl_t *ctrl = (FAR struct iperf_ctrl_t *)p;
+
+          /* ESRCH means the owning task is gone and the instance can no
+           * longer be running, so do not wait for it.
+           */
 
-      kill(ctrl->pid, SIGUSR1);
+          if (kill(ctrl->pid, SIGUSR1) == 0 || errno != ESRCH)
+            {
+              count++;
+            }
+        }
+
+      pthread_mutex_unlock(&g_iperf_ctrl_mutex);
+
+      if (count == 0 || retry >= IPERF_ABORT_RETRIES)
+        {
+          break;
+        }
+
+      usleep(IPERF_ABORT_RETRY_MSEC * 1000);
+    }
+
+  if (count > 0)
+    {
+      printf("iperf: %d instance(s) did not stop\n", count);
+      return -ETIMEDOUT;
     }
 
-  pthread_mutex_unlock(&g_iperf_ctrl_mutex);
   return 0;
 }
diff --git a/netutils/iperf/iperf_main.c b/netutils/iperf/iperf_main.c
index ef042fed1..ed74f7c93 100644
--- a/netutils/iperf/iperf_main.c
+++ b/netutils/iperf/iperf_main.c
@@ -266,8 +266,15 @@ int main(int argc, FAR char *argv[])
 
   if (iperf.abort != 0)
     {
-      iperf_stop();
+      int ret = iperf_stop();
+
       printf("ERROR: abort: %d\n", iperf.abort);
+      if (ret < 0)
+        {
+          printf("ERROR: abort incomplete: %d\n", -ret);
+          return EXIT_FAILURE;
+        }
+
       goto out;
     }
 
@@ -414,8 +421,7 @@ int main(int argc, FAR char *argv[])
     }
 
   iperf_printcfg(&cfg);
-  iperf_start(&cfg);
-  return 0;
+  return iperf_start(&cfg) < 0 ? EXIT_FAILURE : 0;
 
 out:
   iperf_showusage(argv[0]);

Reply via email to