runc: avoid panic when the child process get signalled

On Unix, tokio::process::ExitStatus::code() will return None if the
process was terminated by a signal. So handle to avoid panicking
caused by unwrap().

Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
This commit is contained in:
Liu Jiang 2022-02-13 15:42:34 +08:00
parent 8f20cfb58c
commit cd965aa06a
1 changed files with 4 additions and 6 deletions

View File

@ -49,11 +49,9 @@ pub trait ProcessMonitor {
.expect("failed to take pid of the container process.");
let out = chi.wait_with_output().await?;
let ts = OffsetDateTime::now_utc();
match tx.send(Exit {
ts,
pid,
status: out.status.code().unwrap(),
}) {
// On Unix, out.status.code() will return None if the process was terminated by a signal.
let status = out.status.code().unwrap_or(-1);
match tx.send(Exit { ts, pid, status }) {
Ok(_) => Ok(out),
Err(e) => {
error!("command {:?} exited but receiver dropped.", cmd);
@ -120,7 +118,7 @@ mod tests {
let (tx, rx) = channel();
let output = monitor.start(cmd, tx).await.unwrap();
assert!(output.stdout.len() > 0);
assert!(!output.stdout.is_empty());
assert_eq!(output.stderr.len(), 0);
let status = monitor.wait(rx).await.unwrap();
assert_eq!(status.status, 0);