From f91d5242beeeb66968a4466030f1c2725582c311 Mon Sep 17 00:00:00 2001 From: ESPIE Date: Thu, 2 May 2024 18:12:52 +0200 Subject: [PATCH] fix: changed `std::process::Command` to `tokio::process::Command` to be non blocking Signed-off-by: ESPIE --- src/agent/src/agents/rust.rs | 17 ++++++++++------- src/agent/src/workload/service.rs | 8 +++++--- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/agent/src/agents/rust.rs b/src/agent/src/agents/rust.rs index b8c70ec..1117cc5 100644 --- a/src/agent/src/agents/rust.rs +++ b/src/agent/src/agents/rust.rs @@ -6,7 +6,8 @@ use std::collections::HashSet; use std::future::Future; use std::pin::Pin; use std::sync::Arc; -use std::{fs::create_dir_all, process::Command}; +use std::{fs::create_dir_all}; +use tokio::process::Command; use tokio::sync::Mutex; #[derive(Deserialize)] @@ -39,9 +40,9 @@ impl RustAgent { .spawn() .expect("Failed to build function"); - child_processes.lock().await.insert(child.id()); + child_processes.lock().await.insert(child.id().unwrap()); - let output = child.wait_with_output().expect("Failed to wait on child"); + let output = child.wait_with_output().await.expect("Failed to wait on child"); Ok(AgentOutput { exit_code: output.status.code().unwrap(), @@ -49,11 +50,13 @@ impl RustAgent { stderr: std::str::from_utf8(&output.stderr).unwrap().to_string(), }) } else { - let output = Command::new("cargo") + let child = Command::new("cargo") .arg("build") .current_dir(function_dir) - .output() + .spawn() .expect("Failed to build function"); + + let output = child.wait_with_output().await.expect("Failed to wait on child"); Ok(AgentOutput { exit_code: output.status.code().unwrap(), @@ -158,9 +161,9 @@ impl Agent for RustAgent { .spawn() .expect("Failed to run function"); - child_processes.lock().await.insert(child.id()); + child_processes.lock().await.insert(child.id().unwrap()); - let output = child.wait_with_output().expect("Failed to wait on child"); + let output = child.wait_with_output().await.expect("Failed to wait on child"); let agent_output = AgentOutput { exit_code: output.status.code().unwrap(), diff --git a/src/agent/src/workload/service.rs b/src/agent/src/workload/service.rs index d28d991..3c25477 100644 --- a/src/agent/src/workload/service.rs +++ b/src/agent/src/workload/service.rs @@ -52,11 +52,13 @@ impl WorkloadRunner for WorkloadRunnerService { let child_processes = CHILD_PROCESSES.lock().await; for &child_id in child_processes.iter() { - nix::sys::signal::kill( + match nix::sys::signal::kill( nix::unistd::Pid::from_raw(child_id as i32), nix::sys::signal::Signal::SIGTERM, - ) - .unwrap(); + ) { + Ok(_) => println!("Sent SIGTERM to child process {}", child_id), + Err(e) => println!("Failed to send SIGTERM to child process {}: {}", child_id, e), + } } process::exit(0);