Skip to content

Commit 7259c1c

Browse files
jokemanfiremxpv
authored andcommitted
fix(shim): Under certain extreme conditions, the kill cannot be completed
Let kill call in single task for not holding the lock.
1 parent 1458a10 commit 7259c1c

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

crates/runc-shim/src/runc.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -519,12 +519,33 @@ impl ProcessLifecycle<ExecProcess> for RuncExecLifecycle {
519519
} else if p.exited_at.is_some() {
520520
Err(Error::NotFoundError("process already finished".to_string()))
521521
} else {
522-
// TODO this is kill from nix crate, it is os specific, maybe have annotated with target os
523-
kill(
524-
Pid::from_raw(p.pid),
525-
nix::sys::signal::Signal::try_from(signal as i32).unwrap(),
526-
)
527-
.map_err(Into::into)
522+
let pid = p.pid;
523+
let kill_future = tokio::task::spawn_blocking(move || {
524+
kill(
525+
Pid::from_raw(pid),
526+
nix::sys::signal::Signal::try_from(signal as i32).unwrap(),
527+
)
528+
});
529+
530+
match tokio::time::timeout(std::time::Duration::from_secs(3), kill_future).await {
531+
Ok(Ok(result)) => result.map_err(Into::into),
532+
Ok(Err(e)) => Err(Error::Other(format!("kill task error: {}", e))),
533+
Err(_) => {
534+
debug!(
535+
"kill operation timed out for pid {}, signal {}",
536+
pid, signal
537+
);
538+
// timeout also return ok
539+
// For termination signals, it may have taken effect even if it timed out
540+
if signal == 9 || signal == 15 {
541+
Ok(())
542+
} else {
543+
Err(Error::DeadlineExceeded(
544+
"kill operation timed out".to_string(),
545+
))
546+
}
547+
}
548+
}
528549
}
529550
}
530551

crates/shim/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ pub enum Error {
7979
#[error("Failed to send exit event: {0}")]
8080
Send(#[from] std::sync::mpsc::SendError<ExitEvent>),
8181

82+
#[error("Deadline exceeded: {0}")]
83+
DeadlineExceeded(String),
84+
8285
#[error("Other: {0}")]
8386
Other(String),
8487

0 commit comments

Comments
 (0)