From b8fd21e636b643fe9f257a77808d53b067f3d105 Mon Sep 17 00:00:00 2001
From: songbuhuang <544824346@qq.com>
Date: Wed, 3 Aug 2022 16:06:16 +0800
Subject: [PATCH 08/21] stop health check monitor before stopping container

Signed-off-by: songbuhuang <544824346@qq.com>
---
 src/daemon/executor/container_cb/execution.c         |  2 --
 src/daemon/executor/container_cb/execution_extend.c  |  2 +-
 src/daemon/modules/api/container_api.h               |  2 +-
 .../modules/container/health_check/health_check.c    | 12 ++----------
 src/daemon/modules/service/service_container.c       |  3 +++
 test/mocks/health_check_mock.cc                      |  4 ++--
 test/mocks/health_check_mock.h                       |  2 +-
 .../execute/execution_extend/execution_extend_ut.cc  |  2 +-
 8 files changed, 11 insertions(+), 18 deletions(-)

diff --git a/src/daemon/executor/container_cb/execution.c b/src/daemon/executor/container_cb/execution.c
index edc8b42e..68d0d8d6 100644
--- a/src/daemon/executor/container_cb/execution.c
+++ b/src/daemon/executor/container_cb/execution.c
@@ -676,8 +676,6 @@ static int container_stop_cb(const container_stop_request *request, container_st
         goto pack_response;
     }
 
-    container_stop_health_checks(id);
-
     if (stop_container(cont, timeout, force, false)) {
         cc = ISULAD_ERR_EXEC;
         container_state_set_error(cont->state, (const char *)g_isulad_errmsg);
diff --git a/src/daemon/executor/container_cb/execution_extend.c b/src/daemon/executor/container_cb/execution_extend.c
index 9c2a213b..b0da705e 100644
--- a/src/daemon/executor/container_cb/execution_extend.c
+++ b/src/daemon/executor/container_cb/execution_extend.c
@@ -715,7 +715,7 @@ static int do_pause_container(container_t *cont)
     params.rootpath = cont->root_path;
     params.state = cont->state_path;
 
-    container_stop_health_checks(cont->common_config->id);
+    container_stop_health_checks(cont);
 
     if (runtime_pause(id, cont->runtime, &params)) {
         container_update_health_monitor(cont->common_config->id);
diff --git a/src/daemon/modules/api/container_api.h b/src/daemon/modules/api/container_api.h
index 1140d4d5..ed97633f 100644
--- a/src/daemon/modules/api/container_api.h
+++ b/src/daemon/modules/api/container_api.h
@@ -254,7 +254,7 @@ extern char *container_exit_fifo_create(const char *cont_state_path);
 extern int container_exit_fifo_open(const char *cont_exit_fifo);
 
 void container_init_health_monitor(const char *id);
-void container_stop_health_checks(const char *container_id);
+void container_stop_health_checks(container_t *cont);
 
 bool container_is_in_gc_progress(const char *id);
 
diff --git a/src/daemon/modules/container/health_check/health_check.c b/src/daemon/modules/container/health_check/health_check.c
index b2feee91..273d3531 100644
--- a/src/daemon/modules/container/health_check/health_check.c
+++ b/src/daemon/modules/container/health_check/health_check.c
@@ -182,23 +182,15 @@ static void close_health_check_monitor(container_t *cont)
 
 // Called when the container is being stopped (whether because the health check is
 // failing or for any other reason).
-void container_stop_health_checks(const char *container_id)
+void container_stop_health_checks(container_t *cont)
 {
-    container_t *cont = NULL;
-
-    if (container_id == NULL) {
-        return;
-    }
-
-    cont = containers_store_get(container_id);
     if (cont == NULL) {
-        ERROR("Failed to get container info");
         return;
     }
+
     if (cont->state != NULL && cont->state->state != NULL && cont->state->state->health != NULL) {
         close_health_check_monitor(cont);
     }
-    container_unref(cont);
 }
 
 /* health check manager free */
diff --git a/src/daemon/modules/service/service_container.c b/src/daemon/modules/service/service_container.c
index a9b14043..2f688f57 100644
--- a/src/daemon/modules/service/service_container.c
+++ b/src/daemon/modules/service/service_container.c
@@ -1401,6 +1401,9 @@ int stop_container(container_t *cont, int timeout, bool force, bool restart)
         ret = -1;
         goto out;
     }
+
+    container_stop_health_checks(cont);
+
     // set AutoRemove flag to false before stop so the container won't be
     // removed during restart process
     if (restart) {
diff --git a/test/mocks/health_check_mock.cc b/test/mocks/health_check_mock.cc
index eab18be7..5e2f210b 100644
--- a/test/mocks/health_check_mock.cc
+++ b/test/mocks/health_check_mock.cc
@@ -32,10 +32,10 @@ void container_update_health_monitor(const char *container_id)
     return;
 }
 
-void container_stop_health_checks(const char *container_id)
+void container_stop_health_checks(container_t *cont)
 {
     if (g_health_check_mock != nullptr) {
-        return g_health_check_mock->ContainerStopHealthCheck(container_id);
+        return g_health_check_mock->ContainerStopHealthCheck(cont);
     }
     return;
 }
diff --git a/test/mocks/health_check_mock.h b/test/mocks/health_check_mock.h
index ab8e20b0..29dad8ca 100644
--- a/test/mocks/health_check_mock.h
+++ b/test/mocks/health_check_mock.h
@@ -22,7 +22,7 @@
 class MockHealthCheck {
 public:
     MOCK_METHOD1(UpdateHealthMonitor, void(const char *container_id));
-    MOCK_METHOD1(ContainerStopHealthCheck, void(const char *container_id));
+    MOCK_METHOD1(ContainerStopHealthCheck, void(container_t *cont));
 };
 
 void MockHealthCheck_SetMock(MockHealthCheck* mock);
diff --git a/test/services/execution/execute/execution_extend/execution_extend_ut.cc b/test/services/execution/execute/execution_extend/execution_extend_ut.cc
index 03872340..e4e6d8d4 100644
--- a/test/services/execution/execute/execution_extend/execution_extend_ut.cc
+++ b/test/services/execution/execute/execution_extend/execution_extend_ut.cc
@@ -204,7 +204,7 @@ void invokeStateSetPaused(container_state_t *s)
     return;
 }
 
-void invokeContainerStopHealthCheck(const char *container_id)
+void invokeContainerStopHealthCheck(container_t *cont)
 {
     return;
 }
-- 
2.25.1

