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