Skip to content

Commit

Permalink
check builtin/custom spawning thread status
Browse files Browse the repository at this point in the history
change thread handle return type from () to CmdResult
fix #22
  • Loading branch information
tao-guo committed Apr 5, 2021
1 parent 33d9a9d commit 3982d31
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
21 changes: 7 additions & 14 deletions src/child.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ impl CmdChildHandle {
}
Self::ThreadChild(t) => {
let status = t.child.join();
if let Err(e) = status {
return Err(Error::new(
match status {
Err(e) => Err(Error::new(
ErrorKind::Other,
format!("{} thread exited with error: {:?}", t.cmd, e),
));
)),
Ok(result) => result,
}
Ok(())
}
Self::SyncChild(s) => {
if let Some(mut out) = s.output {
Expand All @@ -55,7 +55,7 @@ impl CmdChildHandle {
}
}

pub fn wait_last_with_output(self) -> Result<Vec<u8>> {
pub fn wait_with_output(self) -> Result<Vec<u8>> {
match self {
Self::ProcChild(p) => {
let output = p.child.wait_with_output()?;
Expand All @@ -70,14 +70,7 @@ impl CmdChildHandle {
}
}
Self::ThreadChild(t) => {
let status = t.child.join();
if let Err(e) = status {
return Err(Error::new(
ErrorKind::Other,
format!("{} thread exited with error: {:?}", t.cmd, e),
));
}
Ok(vec![])
panic!("{} thread should not be waited for output", t.cmd);
}
Self::SyncChild(s) => {
if let Some(mut out) = s.output {
Expand Down Expand Up @@ -132,7 +125,7 @@ pub struct CmdProcChild {
}

pub struct CmdThreadChild {
pub child: JoinHandle<()>,
pub child: JoinHandle<CmdResult>,
pub cmd: String,
pub stderr_logging: Option<CmdIn>,
}
Expand Down
8 changes: 3 additions & 5 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ impl WaitFun {

pub fn wait_raw_result_nolog(&mut self) -> Result<Vec<u8>> {
let handle = self.0.pop().unwrap();
let wait_last = handle.wait_last_with_output();
let wait_last = handle.wait_with_output();
match wait_last {
Err(e) => {
let _ = WaitCmd::wait_children(&mut self.0);
Expand Down Expand Up @@ -284,7 +284,7 @@ impl WaitFun {
pub fn wait_result_nolog(&mut self) -> FunResult {
// wait last process result
let handle = self.0.pop().unwrap();
let wait_last = handle.wait_last_with_output();
let wait_last = handle.wait_with_output();
match wait_last {
Err(e) => {
let _ = WaitCmd::wait_children(&mut self.0);
Expand Down Expand Up @@ -474,9 +474,7 @@ impl Cmd {

let internal_cmd = CMD_MAP.lock().unwrap()[&arg0];
if pipe_out {
let handle = std::thread::spawn(move || {
internal_cmd(&mut env).unwrap();
});
let handle = std::thread::spawn(move || internal_cmd(&mut env));
Ok(CmdChildHandle::ThreadChild(CmdThreadChild {
child: handle,
stderr_logging: self.stderr_logging,
Expand Down

0 comments on commit 3982d31

Please sign in to comment.