From 98b8e8cea061d65ece3865ce8e772f5b4226199e Mon Sep 17 00:00:00 2001
From: jiangheng <jiangheng14@huawei.com>
Date: Tue, 27 Feb 2024 22:37:47 +0800
Subject: [PATCH] fix rpc_pool create failed coredump

---
 src/lstack/core/lstack_thread_rpc.c    | 34 ++++++++++++++++++++------
 src/lstack/include/lstack_thread_rpc.h |  6 ++++-
 2 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/src/lstack/core/lstack_thread_rpc.c b/src/lstack/core/lstack_thread_rpc.c
index 1fdb037..20c5a43 100644
--- a/src/lstack/core/lstack_thread_rpc.c
+++ b/src/lstack/core/lstack_thread_rpc.c
@@ -36,6 +36,27 @@ static inline __attribute__((always_inline)) struct rpc_msg *get_rpc_msg(struct
     return msg;
 }
 
+static void rpc_msg_init(struct rpc_msg *msg, rpc_msg_func func, struct rpc_msg_pool *pool)
+{
+    msg->rpcpool = pool;
+    pthread_spin_init(&msg->lock, PTHREAD_PROCESS_PRIVATE);
+    msg->func = func;
+    msg->sync_flag = 1;
+    msg->recall_flag = 0;
+}
+
+static struct rpc_msg *rpc_msg_alloc_except(rpc_msg_func func)
+{
+    struct rpc_msg *msg = calloc(1, sizeof(struct rpc_msg));
+    if (msg == NULL) {
+        return NULL;
+    }
+
+    rpc_msg_init(msg, func, NULL);
+
+    return msg;
+}
+
 static struct rpc_msg *rpc_msg_alloc(rpc_msg_func func)
 {
     struct rpc_msg *msg = NULL;
@@ -45,14 +66,15 @@ static struct rpc_msg *rpc_msg_alloc(rpc_msg_func func)
         if (g_rpc_pool == NULL) {
             LSTACK_LOG(INFO, LSTACK, "g_rpc_pool calloc failed\n");
             g_rpc_stats.call_alloc_fail++;
-            return NULL;
+            exit(-1);
         }
 
         g_rpc_pool->mempool = create_mempool("rpc_pool", RPC_MSG_MAX, sizeof(struct rpc_msg),
             0, rte_gettid());
         if (g_rpc_pool->mempool == NULL) {
+            LSTACK_LOG(INFO, LSTACK, "rpc_pool create failed, errno is %d\n", errno);
             g_rpc_stats.call_alloc_fail++;
-            return NULL;
+            exit(-1);
         }
     }
 
@@ -61,12 +83,8 @@ static struct rpc_msg *rpc_msg_alloc(rpc_msg_func func)
         g_rpc_stats.call_alloc_fail++;
         return NULL;
     }
-    msg->rpcpool = g_rpc_pool;
+    rpc_msg_init(msg, func, g_rpc_pool);
 
-    pthread_spin_init(&msg->lock, PTHREAD_PROCESS_PRIVATE);
-    msg->func = func;
-    msg->sync_flag = 1;
-    msg->recall_flag = 0;
     return msg;
 }
 
@@ -247,7 +265,7 @@ int32_t rpc_call_close(rpc_queue *queue, int fd)
 
 int32_t rpc_call_stack_exit(rpc_queue *queue)
 {
-    struct rpc_msg *msg = rpc_msg_alloc(stack_exit_by_rpc);
+    struct rpc_msg *msg = rpc_msg_alloc_except(stack_exit_by_rpc);
     if (msg == NULL) {
         return -1;
     }
diff --git a/src/lstack/include/lstack_thread_rpc.h b/src/lstack/include/lstack_thread_rpc.h
index 30caa66..4d89604 100644
--- a/src/lstack/include/lstack_thread_rpc.h
+++ b/src/lstack/include/lstack_thread_rpc.h
@@ -104,7 +104,11 @@ static inline __attribute__((always_inline)) void rpc_call(rpc_queue *queue, str
 static inline __attribute__((always_inline)) void rpc_msg_free(struct rpc_msg *msg)
 {
     pthread_spin_destroy(&msg->lock);
-    rte_mempool_put(msg->rpcpool->mempool, (void *)msg);
+    if (msg->rpcpool != NULL && msg->rpcpool->mempool != NULL) {
+        rte_mempool_put(msg->rpcpool->mempool, (void *)msg);
+    } else {
+        free(msg);
+    }
 }
 
 #endif
-- 
2.27.0

