mirror of
https://github.com/telemt/telemt.git
synced 2026-10-11 11:55:59 +03:00
Process-wide concurrency + Cancellation ownership fixes
This commit is contained in:
@@ -3,6 +3,8 @@ use tokio::process::Command;
|
||||
|
||||
use crate::util::trusted_command::resolve_trusted_helper;
|
||||
|
||||
const COMMAND_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30);
|
||||
|
||||
pub(super) async fn run_command(
|
||||
binary: &str,
|
||||
args: &[&str],
|
||||
@@ -18,21 +20,26 @@ pub(super) async fn run_command(
|
||||
}
|
||||
command.stdout(std::process::Stdio::null());
|
||||
command.stderr(std::process::Stdio::piped());
|
||||
command.kill_on_drop(true);
|
||||
let mut child = command
|
||||
.spawn()
|
||||
.map_err(|e| format!("spawn {binary} failed: {e}"))?;
|
||||
if let Some(blob) = stdin
|
||||
&& let Some(mut writer) = child.stdin.take()
|
||||
{
|
||||
writer
|
||||
.write_all(blob.as_bytes())
|
||||
let output = tokio::time::timeout(COMMAND_TIMEOUT, async move {
|
||||
if let Some(blob) = stdin
|
||||
&& let Some(mut writer) = child.stdin.take()
|
||||
{
|
||||
writer
|
||||
.write_all(blob.as_bytes())
|
||||
.await
|
||||
.map_err(|e| format!("stdin write {binary} failed: {e}"))?;
|
||||
}
|
||||
child
|
||||
.wait_with_output()
|
||||
.await
|
||||
.map_err(|e| format!("stdin write {binary} failed: {e}"))?;
|
||||
}
|
||||
let output = child
|
||||
.wait_with_output()
|
||||
.await
|
||||
.map_err(|e| format!("wait {binary} failed: {e}"))?;
|
||||
.map_err(|e| format!("wait {binary} failed: {e}"))
|
||||
})
|
||||
.await
|
||||
.map_err(|_| format!("{binary} timed out after {}s", COMMAND_TIMEOUT.as_secs()))??;
|
||||
if output.status.success() {
|
||||
return Ok(());
|
||||
}
|
||||
@@ -48,10 +55,11 @@ pub(super) async fn run_command_stdout(binary: &str, args: &[&str]) -> Result<St
|
||||
let Some(command_path) = resolve_trusted_helper(binary) else {
|
||||
return Err(format!("{binary} is not available"));
|
||||
};
|
||||
let output = Command::new(command_path)
|
||||
.args(args)
|
||||
.output()
|
||||
let mut command = Command::new(command_path);
|
||||
command.args(args).kill_on_drop(true);
|
||||
let output = tokio::time::timeout(COMMAND_TIMEOUT, command.output())
|
||||
.await
|
||||
.map_err(|_| format!("{binary} timed out after {}s", COMMAND_TIMEOUT.as_secs()))?
|
||||
.map_err(|e| format!("wait {binary} failed: {e}"))?;
|
||||
if output.status.success() {
|
||||
return Ok(String::from_utf8_lossy(&output.stdout).to_string());
|
||||
|
||||
Reference in New Issue
Block a user