From 9142802a64106ffc34f5839ac8a9d532e0d70fd3 Mon Sep 17 00:00:00 2001 From: fengwei0328 Date: Fri, 22 Nov 2024 14:26:37 +0800 Subject: [PATCH] The task_dir successfully cleans when the file is absent Signed-off-by: fengwei0328 --- crates/runc-shim/src/service.rs | 6 ++++-- crates/shim/src/asynchronous/util.rs | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/runc-shim/src/service.rs b/crates/runc-shim/src/service.rs index 89c1c543..c33c96d5 100644 --- a/crates/runc-shim/src/service.rs +++ b/crates/runc-shim/src/service.rs @@ -108,7 +108,7 @@ impl Shim for Service { let namespace = self.namespace.as_str(); let bundle = current_dir().map_err(io_error!(e, "get current dir"))?; let opts = read_options(&bundle).await?; - let runtime = read_runtime(&bundle).await?; + let runtime = read_runtime(&bundle).await.unwrap_or_default(); let runc = create_runc( &runtime, @@ -117,7 +117,9 @@ impl Shim for Service { &opts, Some(Arc::new(ShimExecutor::default())), )?; - let pid = read_pid_from_file(&bundle.join(INIT_PID_FILE)).await?; + let pid = read_pid_from_file(&bundle.join(INIT_PID_FILE)) + .await + .unwrap_or_default(); runc.delete(&self.id, Some(&DeleteOpts { force: true })) .await diff --git a/crates/shim/src/asynchronous/util.rs b/crates/shim/src/asynchronous/util.rs index 43e2b88d..824d194d 100644 --- a/crates/shim/src/asynchronous/util.rs +++ b/crates/shim/src/asynchronous/util.rs @@ -99,8 +99,13 @@ pub async fn read_spec(bundle: impl AsRef) -> Result { serde_json::from_str::(content.as_str()).map_err(other_error!("read spec")) } +// read_options reads the option information from the path. +// When the file does not exist, read_options returns nil without an error. pub async fn read_options(bundle: impl AsRef) -> Result { let path = bundle.as_ref().join(OPTIONS_FILE_NAME); + if !path.exists() { + return Ok(Options::default()); + } let opts_str = read_file_to_str(path).await?; let opts = serde_json::from_str::(&opts_str).map_err(other_error!("read options"))?;