[RFC 1/6] eal: avoid using pthread_cancel

Stephen Hemminger stephen at networkplumber.org
Wed Sep 24 18:51:08 CEST 2025


The multi-process handling thread can be managed more safely
by closing the underlying socket rather than pthread_cancel.
Closing the socket in the main thread will cause the read()
in the MP handler to unblock.

The use of atomic for updating the socket fd is not needed.
The fd for the socket is set before the thread is created
and not modified again until handler has exited.

Signed-off-by: Stephen Hemminger <stephen at networkplumber.org>
---
 lib/eal/common/eal_common_proc.c | 24 ++++++------------------
 1 file changed, 6 insertions(+), 18 deletions(-)

diff --git a/lib/eal/common/eal_common_proc.c b/lib/eal/common/eal_common_proc.c
index 0dea787e38..8e60e815ed 100644
--- a/lib/eal/common/eal_common_proc.c
+++ b/lib/eal/common/eal_common_proc.c
@@ -34,7 +34,7 @@
 #include "eal_filesystem.h"
 #include "eal_internal_cfg.h"
 
-static RTE_ATOMIC(int) mp_fd = -1;
+static int mp_fd = -1;
 static rte_thread_t mp_handle_tid;
 static char mp_filter[PATH_MAX];   /* Filter for secondary process sockets */
 static char mp_dir_path[PATH_MAX]; /* The directory path for all mp sockets */
@@ -406,17 +406,9 @@ mp_handle(void *arg __rte_unused)
 {
 	struct mp_msg_internal msg;
 	struct sockaddr_un sa;
-	int fd;
-
-	while ((fd = rte_atomic_load_explicit(&mp_fd, rte_memory_order_relaxed)) >= 0) {
-		int ret;
-
-		ret = read_msg(fd, &msg, &sa);
-		if (ret <= 0)
-			break;
 
+	while (read_msg(mp_fd, &msg, &sa) > 0)
 		process_msg(&msg, &sa);
-	}
 
 	return 0;
 }
@@ -656,7 +648,7 @@ rte_mp_channel_init(void)
 		EAL_LOG(ERR, "failed to create mp thread: %s",
 			strerror(errno));
 		close(dir_fd);
-		close(rte_atomic_exchange_explicit(&mp_fd, -1, rte_memory_order_relaxed));
+		close(mp_fd);
 		return -1;
 	}
 
@@ -670,15 +662,11 @@ rte_mp_channel_init(void)
 void
 rte_mp_channel_cleanup(void)
 {
-	int fd;
-
-	fd = rte_atomic_exchange_explicit(&mp_fd, -1, rte_memory_order_relaxed);
-	if (fd < 0)
-		return;
+	/* shutdown() will cause recvmsg to unblock */
+	shutdown(mp_fd, SHUT_RDWR);
 
-	pthread_cancel((pthread_t)mp_handle_tid.opaque_id);
 	rte_thread_join(mp_handle_tid, NULL);
-	close_socket_fd(fd);
+	close_socket_fd(mp_fd);
 }
 
 /**
-- 
2.47.3



More information about the dev mailing list