What about this simpler and more direct approach.

From d3a85e214e8ba9a89e2b154480a8fc76a67c0f68 Mon Sep 17 00:00:00 2001
From: Khadem Ullah <14pwcse1...@uetpeshawar.edu.pk>
Date: Tue, 29 Jul 2025 08:59:15 -0700
Subject: [PATCH] app/testpmd: monitor state of primary process when using
 secondary

In secondary processes, accessing device after primary has exited
will cause crash.

This patch adds a mechanism in testpmd to monitor the primary process
from the secondary using `rte_eal_primary_proc_alive()`.
When primary process exits it forces secondary to exit avoiding
issues from cleanup logic.

Fixes: a550baf24af9 ("app/testpmd: support multi-process")
Cc: sta...@dpdk.org

Signed-off-by: Khadem Ullah <14pwcse1...@uetpeshawar.edu.pk>
Signed-off-by: Stephen Hemminger <step...@networkplumber.org>
---
 app/test-pmd/testpmd.c | 41 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 40 insertions(+), 1 deletion(-)

diff --git a/app/test-pmd/testpmd.c b/app/test-pmd/testpmd.c
index bb88555328..7c22046efb 100644
--- a/app/test-pmd/testpmd.c
+++ b/app/test-pmd/testpmd.c
@@ -101,13 +101,15 @@
 uint16_t verbose_level = 0; /**< Silent by default. */
 int testpmd_logtype; /**< Log type for testpmd logs */
 
+/* Maximum delay for exiting after primary process. */
+#define MONITOR_INTERVAL (500 * 1000)
+
 /* use main core for command line ? */
 uint8_t interactive = 0;
 uint8_t auto_start = 0;
 uint8_t tx_first;
 char cmdline_filename[PATH_MAX] = {0};
 bool echo_cmdline_file;
-
 /*
  * NUMA support configuration.
  * When set, the NUMA support attempts to dispatch the allocation of the
@@ -4332,6 +4334,36 @@ signal_handler(int signum __rte_unused)
        prompt_exit();
 }
 
+/* Alarm signal handler, used to check that primary process */
+static void
+monitor_primary(void *arg __rte_unused)
+{
+       if (rte_eal_primary_proc_alive(NULL)) {
+               rte_eal_alarm_set(MONITOR_INTERVAL, monitor_primary, NULL);
+       } else {
+               /*
+                * If primary process exits, then all the device information
+                * is no longer valid. Calling any cleanup code is going to
+                * run into use after free.
+                */
+               fprintf(stderr, "\nPrimary process is no longer active, 
exiting...\n");
+               exit(EXIT_FAILURE);
+       }
+}
+
+/* Setup handler to check when primary exits. */
+static int
+enable_primary_monitor(void)
+{
+       return rte_eal_alarm_set(MONITOR_INTERVAL, monitor_primary, NULL);
+}
+
+static void
+disable_primary_monitor(void)
+{
+       rte_eal_alarm_cancel(monitor_primary, NULL);
+}
+
 int
 main(int argc, char** argv)
 {
@@ -4363,6 +4395,10 @@ main(int argc, char** argv)
                rte_exit(EXIT_FAILURE, "Cannot init EAL: %s\n",
                         rte_strerror(rte_errno));
 
+       if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
+           enable_primary_monitor() < 0)
+               rte_exit(EXIT_FAILURE, "Cannot setup primary monitor");
+
        /* allocate port structures, and init them */
        init_port();
 
@@ -4556,6 +4592,9 @@ main(int argc, char** argv)
                }
        }
 
+       if (rte_eal_process_type() == RTE_PROC_SECONDARY)
+               disable_primary_monitor();
+
        pmd_test_exit();
 
 #ifdef RTE_LIB_PDUMP
-- 
2.47.2

Reply via email to