From 924a7462bd0621bfe405da43b632a1193227e05e Mon Sep 17 00:00:00 2001 From: Charlotte Meyer Date: Fri, 23 Sep 2022 22:03:10 +0000 Subject: [PATCH] fix(runtime): grab terminal even if error occured --- crates/oyster_runtime/src/pipeline.rs | 33 ++++++++++++++------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/crates/oyster_runtime/src/pipeline.rs b/crates/oyster_runtime/src/pipeline.rs index f677b94..347315b 100644 --- a/crates/oyster_runtime/src/pipeline.rs +++ b/crates/oyster_runtime/src/pipeline.rs @@ -101,32 +101,33 @@ impl Shell { &mut self, pipeline: &ast::Pipeline, ) -> Result { - let mut cmds = pipeline.0.iter().map(PreparedCommand::from); let mut pgid = Pid::from_raw(0); + let status = (|| { + let mut cmds = pipeline.0.iter().map(PreparedCommand::from); - let mut last_cmd = cmds.next().expect("pipelines need to have >1 commands"); - for mut cmd in cmds { - let (output, input) = create_pipe()?; - cmd.stdin = Some(output); + let mut last_cmd = cmds.next().expect("pipelines need to have >1 commands"); + for mut cmd in cmds { + let (output, input) = create_pipe()?; + cmd.stdin = Some(output); - match last_cmd.redirect { - Redirect::None => (), - Redirect::Stdout => last_cmd.stdout = Some(input), + match last_cmd.redirect { + Redirect::None => (), + Redirect::Stdout => last_cmd.stdout = Some(input), + } + + last_cmd.spawn(&mut pgid)?; + + last_cmd = cmd; } last_cmd.spawn(&mut pgid)?; - last_cmd = cmd; - } + wait_pgid(pgid) + })(); - last_cmd.spawn(&mut pgid)?; - - // TODO: kill children if error occured - - let status = wait_pgid(pgid)?; let _ = unistd::tcsetpgrp(libc::STDIN_FILENO, unistd::getpgid(None).unwrap()); - Ok(status) + status } }