From 5e8a16296550f5a816353a607c74645f1cc7a0f6 Mon Sep 17 00:00:00 2001 From: Wildan M Date: Mon, 3 Nov 2025 23:05:36 +0700 Subject: [PATCH] Make sure kill and flush works --- src/bin/repo.rs | 14 +++++++------- src/cook/pty.rs | 12 ++++++++++-- src/cook/script.rs | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/bin/repo.rs b/src/bin/repo.rs index 5e6ffa64a..2d08507c1 100644 --- a/src/bin/repo.rs +++ b/src/bin/repo.rs @@ -6,6 +6,7 @@ use cookbook::cook::fetch::{fetch, fetch_offline}; use cookbook::cook::fs::{create_target_dir, run_command}; use cookbook::cook::package::package; use cookbook::cook::pty::{PtyOut, UnixSlavePty, flush_pty, setup_pty}; +use cookbook::cook::script::KILL_ALL_PID; use cookbook::cook::tree::{display_tree_entry, format_size}; use cookbook::log_to_pty; use cookbook::recipe::{BuildKind, CookRecipe}; @@ -861,7 +862,7 @@ fn run_tui_cook( ); if let Some(log_path) = cooker_config.logs_dir.as_ref() { if let Err(err_ctx) = &handler { - log_to_pty!(&logger, "{:?}", err_ctx) + log_to_pty!(&logger, "\n{:?}", err_ctx) } flush_pty(&mut logger); let log_path = log_path.join(format!("{}.log", name.as_str())); @@ -955,7 +956,7 @@ fn run_tui_cook( && handler.is_err() { if let Err(err_ctx) = &handler { - log_to_pty!(&logger, "{:?}", err_ctx) + log_to_pty!(&logger, "\n{:?}", err_ctx) } flush_pty(&mut logger); let log_path = log_path.join(format!("{}.log", name.as_str())); @@ -1308,12 +1309,11 @@ fn handle_main_event(app: &mut TuiApp, event: &Event) { Key::Char('c') => { // as compilers still running, we use this way to stop it let pid = std::process::id(); - Command::new("pkill") - .arg("-9") - .arg("-P") - .arg(pid.to_string()) + Command::new("bash") + .arg("-c") + .arg(KILL_ALL_PID.replace("$PID", &pid.to_string())) .spawn() - .expect("unable to spawn pkill"); + .expect("unable to spawn kill"); } Key::Up => { app.auto_scroll = false; diff --git a/src/cook/pty.rs b/src/cook/pty.rs index 3fe704612..ae1c2aeca 100644 --- a/src/cook/pty.rs +++ b/src/cook/pty.rs @@ -6,6 +6,7 @@ use std::os::fd::FromRawFd; use std::os::unix::io::AsRawFd; use std::os::unix::process::CommandExt; use std::process::Child; +use std::time::Duration; use std::{io, mem, ptr}; use std::{ io::{PipeReader, PipeWriter}, @@ -60,8 +61,11 @@ pub fn flush_pty(logger: &mut PtyOut) { let Some((pty, file)) = logger else { return; }; + // Not sure if flush actually working let _ = pty.flush(); + std::thread::sleep(Duration::from_millis(100)); let _ = file.flush(); + std::thread::sleep(Duration::from_millis(100)); } pub fn spawn_to_pipe(command: &mut Command, stdout_pipe: &PtyOut) -> Result { @@ -289,6 +293,10 @@ impl PtyFd { Ok(child) } + + fn flush(&mut self) -> std::io::Result<()> { + self.0.flush() + } } /// Represents the master end of a pty. @@ -326,8 +334,8 @@ impl UnixSlavePty { fn spawn_command(&self, builder: &mut Command) -> Result { Ok(self.fd.spawn_command(builder)?) } - fn flush(&self) -> Result<(), anyhow::Error> { - Ok(self.fd.as_file()?.flush()?) + fn flush(&mut self) -> Result<(), anyhow::Error> { + Ok(self.fd.flush()?) } } diff --git a/src/cook/script.rs b/src/cook/script.rs index c7311ac22..540098d79 100644 --- a/src/cook/script.rs +++ b/src/cook/script.rs @@ -352,3 +352,19 @@ if [ "$(git rev-parse HEAD)" != "$(git rev-parse $ORIGIN_BRANCH)" ] then git checkout -B "$(echo "$ORIGIN_BRANCH" | cut -d / -f 2-)" "$ORIGIN_BRANCH" fi"#; + +pub static KILL_ALL_PID: &str = r#" +THISPID=$$ +CHILDREN=$(ps -o pid= --ppid $PID | grep -v $THISPID); + +ALL_DESCENDANTS=''; + +while [ -n "$CHILDREN" ]; do + ALL_DESCENDANTS="$ALL_DESCENDANTS $CHILDREN"; + CHILDREN=$(ps -o pid= --ppid $(echo $CHILDREN) | tr '\n' ' '); +done; + +if [ -n "$ALL_DESCENDANTS" ]; then + kill -9 $ALL_DESCENDANTS; +fi +"#;