From 9cbb78380ae908120e7d76fe36bbbb05960fd46b Mon Sep 17 00:00:00 2001 From: Bittrance Date: Sun, 1 Oct 2023 23:24:09 +0200 Subject: [PATCH] Give IO threads time to generate events after exit. --- src/actions.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/actions.rs b/src/actions.rs index de375fa..f9c1a35 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -120,11 +120,12 @@ where let mut child = command.spawn().map_err(GitOpsError::ActionError)?; let stdout = child.stdout.take().unwrap(); let stderr = child.stderr.take().unwrap(); - // TODO Proper cleanup; break read threads, et c - emit_data(name.to_string(), stdout, SourceType::StdOut, sink); - emit_data(name.to_string(), stderr, SourceType::StdErr, sink); + let out_t = emit_data(name.to_string(), stdout, SourceType::StdOut, sink); + let err_t = emit_data(name.to_string(), stderr, SourceType::StdErr, sink); loop { if let Some(exit) = child.try_wait().map_err(GitOpsError::ActionError)? { + out_t.join().unwrap()?; + err_t.join().unwrap()?; sink.lock().unwrap()(WorkloadEvent::ActionExit(name.to_string(), exit))?; if exit.success() { break Ok(ActionResult::Success); @@ -134,6 +135,8 @@ where } if Instant::now() > deadline { child.kill().map_err(GitOpsError::ActionError)?; + out_t.join().unwrap()?; + err_t.join().unwrap()?; sink.lock().unwrap()(WorkloadEvent::Timeout(name.to_string()))?; break Ok(ActionResult::Failure); } @@ -200,4 +203,15 @@ mod tests { let res = run_action("test", &action, workdir.path(), deadline, &sink); assert!(matches!(res, Ok(ActionResult::Failure))); } + + #[test] + #[cfg(unix)] + fn timing_out_action() { + let action = shell_action("sleep 1"); + let workdir = tempdir().unwrap(); + let deadline = Instant::now(); + let sink = Arc::new(Mutex::new(move |_| Ok(()))); + let res = run_action("test", &action, workdir.path(), deadline, &sink); + assert!(matches!(res, Ok(ActionResult::Failure))); + } }