From 39824db32a1e6a1b794f3d0b8a1cce385dece19b Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Tue, 16 Jul 2024 20:32:18 +0100 Subject: fix(gui): kill child on block stop (#2288) --- ui/backend/src/pty.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'ui/backend/src/pty.rs') diff --git a/ui/backend/src/pty.rs b/ui/backend/src/pty.rs index 07857824..45502892 100644 --- a/ui/backend/src/pty.rs +++ b/ui/backend/src/pty.rs @@ -12,6 +12,7 @@ pub struct Pty { pub master: Arc>>, pub reader: Arc>>, + pub child: Arc>>, } impl Pty { @@ -29,15 +30,8 @@ impl Pty { let cmd = CommandBuilder::new_default_prog(); - tokio::task::spawn_blocking(move || { - let mut child = pair.slave.spawn_command(cmd).unwrap(); - // Wait for the child to exit - let _ = child.wait().unwrap(); - - // Ensure slave is dropped - // This closes file handles, we can deadlock if this is not done correctly. - drop(pair.slave); - }); + let child = pair.slave.spawn_command(cmd).unwrap(); + drop(pair.slave); // Handle input -> write to master writer let (master_tx, mut master_rx) = tokio::sync::mpsc::channel::(32); @@ -66,6 +60,7 @@ impl Pty { tx: master_tx, master: Arc::new(Mutex::new(pair.master)), reader: Arc::new(Mutex::new(reader)), + child: Arc::new(Mutex::new(child)), }) } @@ -109,4 +104,17 @@ impl Pty { self.send_bytes(bytes).await } + + pub async fn kill_child(&self) -> Result<()> { + let mut child = self + .child + .lock() + .map_err(|e| eyre!("Failed to lock pty child: {e}"))?; + + child + .kill() + .map_err(|e| eyre!("Failed to kill child: {e}"))?; + + Ok(()) + } } -- cgit v1.3.1