From 934d289aa535bbb87bfe484c4de34275b968fb87 Mon Sep 17 00:00:00 2001
From: zhongtao <zhongtao17@huawei.com>
Date: Wed, 8 May 2024 11:40:40 +0800
Subject: [PATCH 81/85] set the sandbox status to not ready under abnormal
 circumstances

Signed-off-by: zhongtao <zhongtao17@huawei.com>
---
 src/daemon/sandbox/sandbox.cc | 34 +++++++++++++++++++++++++---------
 src/daemon/sandbox/sandbox.h  |  1 +
 2 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/src/daemon/sandbox/sandbox.cc b/src/daemon/sandbox/sandbox.cc
index bae5b8db..279bf628 100644
--- a/src/daemon/sandbox/sandbox.cc
+++ b/src/daemon/sandbox/sandbox.cc
@@ -371,6 +371,8 @@ void Sandbox::DoUpdateStatus(std::unique_ptr<ControllerSandboxStatus> status, Er
     m_state.exitedAt = status->exitedAt;
     if (status->state == std::string(SANDBOX_READY_STATE_STR)) {
         m_state.status = SANDBOX_STATUS_RUNNING;
+    } else {
+        m_state.status = SANDBOX_STATUS_STOPPED;
     }
 }
 
@@ -459,6 +461,24 @@ auto Sandbox::Save(Errors &error) -> bool
     return true;
 }
 
+bool Sandbox::DoStatusUpdateAndWaitInLoad(const std::string &sandboxID, Errors &error)
+{
+    if (!UpdateStatus(error)) {
+        ERROR("Failed to update status of Sandbox, id='%s'", sandboxID.c_str());
+        return false;
+    }
+
+    // Regardless of whether the sandbox is ready,
+    // Wait() is required to call to monitor whether the kuasar sandbox is ready or exits.
+    // TODO: distinguish the meaning of Wait() return value in different states of sandbox
+    if (!m_controller->Wait(shared_from_this(), sandboxID, error)) {
+        ERROR("Failed to restore wait callback");
+        return false;
+    }
+
+    return true;
+}
+
 auto Sandbox::Load(Errors &error) -> bool
 {
     if (!LoadState(error)) {
@@ -478,15 +498,11 @@ auto Sandbox::Load(Errors &error) -> bool
 
     LoadNetworkSetting();
 
-    if (!UpdateStatus(error)) {
-        ERROR("Failed to update status of Sandbox, id='%s'", m_id.c_str());
-        return false;
-    }
-
-    // TODO: distinguish the meaning of Wait() return value in different states of sandbox
-    if (!m_controller->Wait(shared_from_this(), m_id, error)) {
-        ERROR("Failed to restore wait callback");
-        return false;
+    // When the sandbox status acquisition fails or wait fails, the sandbox status is set to not ready,
+    // and the user decides whether to delete the sandbox.
+    if (!DoStatusUpdateAndWaitInLoad(m_id, error)) {
+        WriteGuard<RWMutex> lock(m_stateMutex);
+        m_state.status = SANDBOX_STATUS_STOPPED;
     }
 
     return true;
diff --git a/src/daemon/sandbox/sandbox.h b/src/daemon/sandbox/sandbox.h
index 20a8e338..42fbee2a 100644
--- a/src/daemon/sandbox/sandbox.h
+++ b/src/daemon/sandbox/sandbox.h
@@ -156,6 +156,7 @@ private:
     auto SetupSandboxFiles(Errors &error) -> bool;
     void DoUpdateStatus(std::unique_ptr<ControllerSandboxStatus> status, Errors &error);
     void DoUpdateExitedStatus(const ControllerExitInfo &exitInfo);
+    bool DoStatusUpdateAndWaitInLoad(const std::string &sandboxID, Errors &error);
 
     auto GetMetadataJsonPath() ->  std::string;
     auto GetStatePath() -> std::string;
-- 
2.34.1

