From 8030e469b22083e7ecf9615555e57a509807655a Mon Sep 17 00:00:00 2001 From: Yipeng Yin Date: Thu, 2 Mar 2023 15:46:05 +0800 Subject: [PATCH] fix(runtime-rs): add exited state to ensure cleanup Set process status to exited at end of io wait, which indicate process exited only, but stop process has not been finished. Otherwise, the cleanup_container will be skipped. Fixes: #6393 Signed-off-by: Yipeng Yin --- .../crates/runtimes/common/src/types/mod.rs | 1 + .../common/src/types/trans_into_shim.rs | 1 + .../src/container_manager/container_inner.rs | 24 ++++++++++--------- .../src/container_manager/process.rs | 2 +- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/runtime-rs/crates/runtimes/common/src/types/mod.rs b/src/runtime-rs/crates/runtimes/common/src/types/mod.rs index 0e6f80a4f6..3772a8a7cd 100644 --- a/src/runtime-rs/crates/runtimes/common/src/types/mod.rs +++ b/src/runtime-rs/crates/runtimes/common/src/types/mod.rs @@ -184,6 +184,7 @@ pub enum ProcessStatus { Stopped = 3, Paused = 4, Pausing = 5, + Exited = 6, } #[derive(Debug, Clone)] diff --git a/src/runtime-rs/crates/runtimes/common/src/types/trans_into_shim.rs b/src/runtime-rs/crates/runtimes/common/src/types/trans_into_shim.rs index 3c3134e8fd..345e02d932 100644 --- a/src/runtime-rs/crates/runtimes/common/src/types/trans_into_shim.rs +++ b/src/runtime-rs/crates/runtimes/common/src/types/trans_into_shim.rs @@ -56,6 +56,7 @@ impl From for api::Status { ProcessStatus::Stopped => api::Status::STOPPED, ProcessStatus::Paused => api::Status::PAUSED, ProcessStatus::Pausing => api::Status::PAUSING, + ProcessStatus::Exited => api::Status::STOPPED, } } } diff --git a/src/runtime-rs/crates/runtimes/virt_container/src/container_manager/container_inner.rs b/src/runtime-rs/crates/runtimes/virt_container/src/container_manager/container_inner.rs index 5e1cbb3984..b041af0762 100644 --- a/src/runtime-rs/crates/runtimes/virt_container/src/container_manager/container_inner.rs +++ b/src/runtime-rs/crates/runtimes/virt_container/src/container_manager/container_inner.rs @@ -200,20 +200,22 @@ impl ContainerInner { return Ok(()); } - self.check_state(vec![ProcessStatus::Running]) + self.check_state(vec![ProcessStatus::Running, ProcessStatus::Exited]) .await .context("check state")?; - // if use force mode to stop container, stop always successful - // send kill signal to container - // ignore the error of sending signal, since the process would - // have been killed and exited yet. - self.signal_process(process, Signal::SIGKILL as u32, false) - .await - .map_err(|e| { - warn!(logger, "failed to signal kill. {:?}", e); - }) - .ok(); + if state == ProcessStatus::Running { + // if use force mode to stop container, stop always successful + // send kill signal to container + // ignore the error of sending signal, since the process would + // have been killed and exited yet. + self.signal_process(process, Signal::SIGKILL as u32, false) + .await + .map_err(|e| { + warn!(logger, "failed to signal kill. {:?}", e); + }) + .ok(); + } match process.process_type { ProcessType::Container => self diff --git a/src/runtime-rs/crates/runtimes/virt_container/src/container_manager/process.rs b/src/runtime-rs/crates/runtimes/virt_container/src/container_manager/process.rs index 5953f2f69b..438a817e20 100644 --- a/src/runtime-rs/crates/runtimes/virt_container/src/container_manager/process.rs +++ b/src/runtime-rs/crates/runtimes/virt_container/src/container_manager/process.rs @@ -182,7 +182,7 @@ impl Process { drop(exit_status); let mut status = status.write().await; - *status = ProcessStatus::Stopped; + *status = ProcessStatus::Exited; drop(status); drop(exit_notifier);