From a2ed1886a091d1edfb54fdec38db277e0d0e29f5 Mon Sep 17 00:00:00 2001
From: "Neil.wrz" <wangrunze13@huawei.com>
Date: Tue, 13 Dec 2022 01:34:12 -0800
Subject: [PATCH 62/65] isulad shim wait for all child process

Signed-off-by: Neil.wrz <wangrunze13@huawei.com>
---
 src/cmd/isulad-shim/process.c                  | 18 ++++++++++++++++++
 src/daemon/modules/plugin/plugin.c             |  2 +-
 .../modules/runtime/isula/isula_rt_ops.c       |  4 ++--
 src/utils/cutils/utils.h                       |  2 +-
 test/cutils/utils_utils/utils_utils_ut.cc      |  6 +++---
 5 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/src/cmd/isulad-shim/process.c b/src/cmd/isulad-shim/process.c
index c8ce7a44..1fc95525 100644
--- a/src/cmd/isulad-shim/process.c
+++ b/src/cmd/isulad-shim/process.c
@@ -39,6 +39,7 @@
 #include "terminal.h"
 #include "utils_array.h"
 #include "utils_string.h"
+#include "utils.h"
 
 #define MAX_EVENTS 100
 #define DEFAULT_IO_COPY_BUF (16 * 1024)
@@ -1206,10 +1207,20 @@ out:
     return ret;
 }
 
+static int try_wait_all_child() {
+    if (waitpid(-1, NULL, WNOHANG) == -1 && errno == ECHILD) {
+        // all child handled
+        return 0;
+    }
+
+    return 1;
+}
+
 int process_signal_handle_routine(process_t *p)
 {
     int ret = SHIM_ERR;
     bool exit_shim = false;
+    int nret = 0;
     int i;
 
     for (;;) {
@@ -1235,6 +1246,13 @@ int process_signal_handle_routine(process_t *p)
         }
         if (exit_shim) {
             process_kill_all(p);
+            
+            // wait atmost 120 seconds
+            DO_RETRY_CALL(120, 1000000, nret, try_wait_all_child, 0);
+            if (nret != 0) {
+                write_message(g_log_fd, ERR_MSG, "Failed to wait all child after 120 seconds");
+            }
+
             process_delete(p);
             if (p->exit_fd > 0) {
                 (void)write_nointr(p->exit_fd, &status, sizeof(int));
diff --git a/src/daemon/modules/plugin/plugin.c b/src/daemon/modules/plugin/plugin.c
index 18035518..53afeeaf 100644
--- a/src/daemon/modules/plugin/plugin.c
+++ b/src/daemon/modules/plugin/plugin.c
@@ -485,7 +485,7 @@ static int pm_register_plugin(const char *name, const char *addr)
         goto failed;
     }
 
-    DO_RETYR_CALL(PLUGIN_ACTIVATE_MAX_RETRY, 1000000, err, pm_activate_plugin, plugin);
+    DO_RETRY_CALL(PLUGIN_ACTIVATE_MAX_RETRY, 1000000, err, pm_activate_plugin, plugin);
     if (err != 0) {
         ERROR("active plugin failed");
         goto failed;
diff --git a/src/daemon/modules/runtime/isula/isula_rt_ops.c b/src/daemon/modules/runtime/isula/isula_rt_ops.c
index bfe7de08..dd1bb4e8 100644
--- a/src/daemon/modules/runtime/isula/isula_rt_ops.c
+++ b/src/daemon/modules/runtime/isula/isula_rt_ops.c
@@ -968,13 +968,13 @@ int rt_isula_clean_resource(const char *id, const char *runtime, const rt_clean_
     }
 
     // retry 10 count call runtime kill, every call sleep 1s
-    DO_RETYR_CALL(10, 1000000, nret, runtime_call_kill_force, workdir, runtime, id);
+    DO_RETRY_CALL(10, 1000000, nret, runtime_call_kill_force, workdir, runtime, id);
     if (nret != 0) {
         WARN("call runtime force kill failed");
     }
 
     // retry 10 count call runtime delete, every call sleep 1s
-    DO_RETYR_CALL(10, 1000000, nret, runtime_call_delete_force, workdir, runtime, id);
+    DO_RETRY_CALL(10, 1000000, nret, runtime_call_delete_force, workdir, runtime, id);
     if (nret != 0) {
         WARN("call runtime force delete failed");
     }
diff --git a/src/utils/cutils/utils.h b/src/utils/cutils/utils.h
index 72cab9f2..fec6d879 100644
--- a/src/utils/cutils/utils.h
+++ b/src/utils/cutils/utils.h
@@ -392,7 +392,7 @@ int convert_v2_runtime(const char *runtime, char *binary);
  *  0 is cb successful at least once;
  *  1 is all cb are failure;
 */
-#define DO_RETYR_CALL(retry_cnt, interval_us, ret, cb, ...) do {    \
+#define DO_RETRY_CALL(retry_cnt, interval_us, ret, cb, ...) do {    \
         size_t i = 0;                                               \
         for(; i < retry_cnt; i++) {                                 \
             ret = cb(__VA_ARGS__);                                  \
diff --git a/test/cutils/utils_utils/utils_utils_ut.cc b/test/cutils/utils_utils/utils_utils_ut.cc
index 5bd98d47..c8f38717 100644
--- a/test/cutils/utils_utils/utils_utils_ut.cc
+++ b/test/cutils/utils_utils/utils_utils_ut.cc
@@ -285,15 +285,15 @@ TEST(utils_utils, test_do_retry_call)
     int nret;
 
     global_total = 0;
-    DO_RETYR_CALL(10, 100, nret, retry_call_test, 0);
+    DO_RETRY_CALL(10, 100, nret, retry_call_test, 0);
     ASSERT_EQ(nret, 0);
     ASSERT_EQ(global_total, 0);
     global_total = 0;
-    DO_RETYR_CALL(10, 100, nret, retry_call_test, 5);
+    DO_RETRY_CALL(10, 100, nret, retry_call_test, 5);
     ASSERT_EQ(nret, 0);
     ASSERT_EQ(global_total, 5);
     global_total = 0;
-    DO_RETYR_CALL(10, 100, nret, retry_call_test, 11);
+    DO_RETRY_CALL(10, 100, nret, retry_call_test, 11);
     ASSERT_EQ(global_total, 10);
     ASSERT_EQ(nret, -1);
 }
\ No newline at end of file
-- 
2.25.1

