Skip to content

Commit

Permalink
add exec delete timeout
Browse files Browse the repository at this point in the history
Signed-off-by: kuangmingfu <[email protected]>
  • Loading branch information
mingfukuang committed Aug 3, 2024
1 parent 6a5c9c2 commit f9810c1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
3 changes: 2 additions & 1 deletion crates/runc-shim/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/

use std::env;
use tokio::runtime::Builder;

use containerd_shim::{asynchronous::run, parse};
use tokio::runtime::Builder;

mod cgroup_memory;
mod common;
Expand Down
3 changes: 3 additions & 0 deletions crates/runc-shim/src/runc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,9 @@ impl ProcessLifecycle<ExecProcess> for RuncExecLifecycle {

async fn delete(&self, p: &mut ExecProcess) -> Result<()> {
self.exit_signal.signal();
self.exit_signal
.wait_for_exit(tokio::time::Duration::from_secs(2))
.await;
let exec_pid_path = Path::new(self.bundle.as_str()).join(format!("{}.pid", p.id));
remove_file(exec_pid_path).await.unwrap_or_default();
Ok(())
Expand Down
18 changes: 17 additions & 1 deletion crates/shim/src/asynchronous/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use nix::{
unistd::Pid,
};
use signal_hook_tokio::Signals;
use tokio::{io::AsyncWriteExt, sync::Notify};
use tokio::{self, io::AsyncWriteExt, sync::Notify, time::Duration};

use crate::{
args,
Expand Down Expand Up @@ -237,6 +237,22 @@ impl ExitSignal {
notified.await;
}
}

/// Wait for the exit signal to be set or return after a timeout.
pub async fn wait_for_exit(&self, timeout_duration: Duration) {
let timeout_task = async {
tokio::time::sleep(timeout_duration).await;
};

let exit_task = async {
self.wait().await;
};

tokio::select! {
_ = timeout_task => {},
_ = exit_task => {},
}
}
}

/// Spawn is a helper func to launch shim process asynchronously.
Expand Down

0 comments on commit f9810c1

Please sign in to comment.