From d124bff77f2de85adc9d7eb514b403466943f0f9 Mon Sep 17 00:00:00 2001 From: Mark <> Date: Sat, 17 Feb 2024 12:06:04 +0100 Subject: [PATCH] drop stdin on childproc_await to prevent deadlocks, add flush() call after childproc_write_* --- mers_lib/src/program/configs/with_command_running.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/mers_lib/src/program/configs/with_command_running.rs b/mers_lib/src/program/configs/with_command_running.rs index fa809cb..257a4f9 100755 --- a/mers_lib/src/program/configs/with_command_running.rs +++ b/mers_lib/src/program/configs/with_command_running.rs @@ -108,7 +108,7 @@ impl Config { .spawn() { Ok(mut child) => { - let a = child.stdin.take().unwrap(); + let a = Some(child.stdin.take().unwrap()); let b = BufReader::new(child.stdout.take().unwrap()); let c = BufReader::new(child.stderr.take().unwrap()); Data::new(ChildProcess(Arc::new(Mutex::new((child, a, b, c))))) @@ -160,13 +160,14 @@ impl Config { Arc::new(data::tuple::TupleT(vec![])), ])) } else { - return Err(format!("childproc_exited called on non-ChildProcess type {a}").into()); + return Err(format!("childproc_await called on non-ChildProcess type {a}").into()); } }), run: Arc::new(|a, _i| { let a = a.get(); let child = a.as_any().downcast_ref::().unwrap(); let mut child = child.0.lock().unwrap(); + drop(child.1.take()); match child.0.wait() { Ok(s) => if let Some(s) = s.code() { Data::new(data::int::Int(s as _)) @@ -199,7 +200,7 @@ impl Config { let child = child.as_any().downcast_ref::().unwrap(); let mut child = child.0.lock().unwrap(); let buf = bytes.iterable().unwrap().map(|v| v.get().as_any().downcast_ref::().unwrap().0.max(0).min(255) as u8).collect::>(); - if child.1.write_all(&buf).is_ok() { + if child.1.as_mut().is_some_and(|v| v.write_all(&buf).is_ok() && v.flush().is_ok()) { Data::new(data::bool::Bool(true)) } else { Data::new(data::bool::Bool(false)) @@ -228,7 +229,7 @@ impl Config { let child = child.as_any().downcast_ref::().unwrap(); let mut child = child.0.lock().unwrap(); let buf = string.as_any().downcast_ref::().unwrap().0.as_bytes(); - if child.1.write_all(buf).is_ok() { + if child.1.as_mut().is_some_and(|v| v.write_all(buf).is_ok() && v.flush().is_ok()) { Data::new(data::bool::Bool(true)) } else { Data::new(data::bool::Bool(false)) @@ -361,7 +362,7 @@ pub struct ChildProcess( Arc< Mutex<( std::process::Child, - ChildStdin, + Option, BufReader, BufReader, )>,