From a6b8a2c04df21d940ce0d22128b776c00d460bba Mon Sep 17 00:00:00 2001
From: liuxu <liuxu156@huawei.com>
Date: Fri, 6 Dec 2024 10:39:22 +0800
Subject: [PATCH 07/19] bugfix:do purge container when do_start_container
 failed

Signed-off-by: liuxu <liuxu156@huawei.com>
---
 .../modules/container/restore/restore.c       |  1 +
 .../modules/container/supervisor/supervisor.c |  4 +--
 .../modules/service/service_container.c       | 25 ++++++++++++++++---
 3 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/src/daemon/modules/container/restore/restore.c b/src/daemon/modules/container/restore/restore.c
index 52f68d21..44ed14df 100644
--- a/src/daemon/modules/container/restore/restore.c
+++ b/src/daemon/modules/container/restore/restore.c
@@ -95,6 +95,7 @@ static int restore_supervisor(const container_t *cont)
 
     if (container_supervisor_add_exit_monitor(exit_fifo_fd, exit_fifo, &pid_info, cont)) {
         ERROR("Failed to add exit monitor to supervisor");
+        close(exit_fifo_fd);
         ret = -1;
         goto out;
     }
diff --git a/src/daemon/modules/container/supervisor/supervisor.c b/src/daemon/modules/container/supervisor/supervisor.c
index 294783eb..f77f58d7 100644
--- a/src/daemon/modules/container/supervisor/supervisor.c
+++ b/src/daemon/modules/container/supervisor/supervisor.c
@@ -328,7 +328,6 @@ int container_supervisor_add_exit_monitor(int fd, const char *exit_fifo, const p
 
     if (pid_info == NULL || cont == NULL || cont->common_config == NULL) {
         ERROR("Invalid input arguments");
-        close(fd);
         return -1;
     }
 
@@ -336,7 +335,6 @@ int container_supervisor_add_exit_monitor(int fd, const char *exit_fifo, const p
     cgroup_path = merge_container_cgroups_path(cont->common_config->id, cont->hostconfig);
     if (cgroup_path == NULL) {
         ERROR("Failed to get cgroup path");
-        close(fd);
         return -1;
     }
 #endif
@@ -344,7 +342,6 @@ int container_supervisor_add_exit_monitor(int fd, const char *exit_fifo, const p
     data = util_common_calloc_s(sizeof(struct supervisor_handler_data));
     if (data == NULL) {
         ERROR("Memory out");
-        close(fd);
         return -1;
     }
 
@@ -385,6 +382,7 @@ int container_supervisor_add_exit_monitor(int fd, const char *exit_fifo, const p
     goto out;
 
 err:
+    data->fd = -1;
     supervisor_handler_data_free(data);
 #ifdef ENABLE_OOM_MONITOR
     common_free_cgroup_oom_handler_info(oom_handler_info);
diff --git a/src/daemon/modules/service/service_container.c b/src/daemon/modules/service/service_container.c
index 8e80e936..250e8299 100644
--- a/src/daemon/modules/service/service_container.c
+++ b/src/daemon/modules/service/service_container.c
@@ -283,7 +283,6 @@ static int do_post_start_on_success(container_t *cont, int exit_fifo_fd,
 {
     int ret = 0;
 
-    // exit_fifo_fd was closed in container_supervisor_add_exit_monitor
     if (container_supervisor_add_exit_monitor(exit_fifo_fd, exit_fifo, pid_info, cont)) {
         ERROR("Failed to add exit monitor to supervisor");
         ret = -1;
@@ -936,7 +935,11 @@ static int do_start_container(container_t *cont, const char *console_fifos[], bo
 
     if (runtime_create(id, runtime, &create_params) != 0) {
         ret = -1;
+#ifdef ENABLE_CRI_API_V1
+        goto clean_prepare_container;
+#else
         goto close_exit_fd;
+#endif
     }
 
     start_params.rootpath = cont->root_path;
@@ -959,19 +962,33 @@ static int do_start_container(container_t *cont, const char *console_fifos[], bo
         if (do_post_start_on_success(cont, exit_fifo_fd, exit_fifo, pid_info) != 0) {
             ERROR("Failed to do post start on runtime start success");
             ret = -1;
-            goto clean_resources;
+#ifdef ENABLE_CRI_API_V1
+            goto clean_prepare_container;
+#else
+            goto close_exit_fd;
+#endif
         }
     } else {
         // wait monitor cleanup cgroup and processes finished
         wait_exit_fifo(id, exit_fifo_fd);
+#ifdef ENABLE_CRI_API_V1
+        goto clean_prepare_container;
+#else
         goto close_exit_fd;
+#endif
     }
     goto out;
 
+#ifdef ENABLE_CRI_API_V1
+clean_prepare_container:
+    if (cont->common_config->sandbox_info != NULL &&
+        sandbox_purge_container(cont->common_config) != 0) {
+        ERROR("Failed to remove container %s from sandbox", id);
+    }
+#endif
+
 close_exit_fd:
     close(exit_fifo_fd);
-
-clean_resources:
     clean_resources_on_failure(cont, engine_log_path, loglevel);
 
 out:
-- 
2.23.0

