From 7b499c7d7b7079b1cb4a362c0052c70e405d07f3 Mon Sep 17 00:00:00 2001 From: Solomon Jacobs Date: Thu, 9 Nov 2023 10:41:46 +0100 Subject: [PATCH 1/7] I: remove async-std: add tokio as a dependency Adds two new dependencies: * tokio * tokio_utils We want to remove async-std, since it is no longer maintained. --- v2/robotmk/Cargo.lock | 200 +++++++++++++++++++++++++++++++++++++++++- v2/robotmk/Cargo.toml | 2 + 2 files changed, 201 insertions(+), 1 deletion(-) diff --git a/v2/robotmk/Cargo.lock b/v2/robotmk/Cargo.lock index 1959a079..b5e389df 100644 --- a/v2/robotmk/Cargo.lock +++ b/v2/robotmk/Cargo.lock @@ -17,6 +17,18 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "ahash" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91429305e9f0a25f6205c5b8e0d2db09e0708a7a6df0f42212bb56c32c8ac97a" +dependencies = [ + "cfg-if", + "once_cell", + "version_check", + "zerocopy", +] + [[package]] name = "aho-corasick" version = "1.1.2" @@ -26,6 +38,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "allocator-api2" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5" + [[package]] name = "android-tzdata" version = "0.1.1" @@ -154,7 +172,7 @@ dependencies = [ "polling", "rustix 0.37.27", "slab", - "socket2", + "socket2 0.4.10", "waker-fn", ] @@ -266,6 +284,12 @@ version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" +[[package]] +name = "bytes" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" + [[package]] name = "camino" version = "1.1.6" @@ -608,6 +632,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "hashbrown" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f93e7192158dbcda357bdec5fb5788eebf8bbac027f3f33e719d29135ae84156" +dependencies = [ + "ahash", + "allocator-api2", +] + [[package]] name = "heck" version = "0.4.1" @@ -722,6 +756,16 @@ version = "0.4.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da2479e8c062e40bf0066ffa0bc823de0a9368974af99c9f6df941d2c231e03f" +[[package]] +name = "lock_api" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" version = "0.4.20" @@ -755,6 +799,17 @@ dependencies = [ "adler", ] +[[package]] +name = "mio" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3dce281c5e46beae905d4de1870d8b1509a9142b62eedf18b443b011ca8343d0" +dependencies = [ + "libc", + "wasi", + "windows-sys", +] + [[package]] name = "nix" version = "0.27.1" @@ -793,6 +848,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "object" version = "0.32.1" @@ -814,6 +879,29 @@ version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb813b8af86854136c6922af0598d719255ecb2179515e6e7730d468f05c9cae" +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + [[package]] name = "pin-project-lite" version = "0.2.13" @@ -949,6 +1037,8 @@ dependencies = [ "serde_json", "sysinfo", "tempfile", + "tokio", + "tokio-util", "walkdir", ] @@ -1037,6 +1127,15 @@ dependencies = [ "serde", ] +[[package]] +name = "signal-hook-registry" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" +dependencies = [ + "libc", +] + [[package]] name = "slab" version = "0.4.9" @@ -1046,6 +1145,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "smallvec" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a" + [[package]] name = "socket2" version = "0.4.10" @@ -1056,6 +1161,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "socket2" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b5fac59a5cb5dd637972e5fca70daf0523c9067fcdc4842f053dae04a18f8e9" +dependencies = [ + "libc", + "windows-sys", +] + [[package]] name = "strsim" version = "0.10.0" @@ -1121,6 +1236,54 @@ dependencies = [ "syn", ] +[[package]] +name = "tokio" +version = "1.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f38200e3ef7995e5ef13baec2f432a6da0aa9ac495b2c0e8f3b7eec2c92d653" +dependencies = [ + "backtrace", + "bytes", + "libc", + "mio", + "num_cpus", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2 0.5.5", + "tokio-macros", + "windows-sys", +] + +[[package]] +name = "tokio-macros" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-util" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5419f34732d9eb6ee4c3578b7989078579b7f039cbbb9ca2c4da015749371e15" +dependencies = [ + "bytes", + "futures-core", + "futures-io", + "futures-sink", + "futures-util", + "hashbrown", + "pin-project-lite", + "slab", + "tokio", + "tracing", +] + [[package]] name = "tracing" version = "0.1.40" @@ -1136,6 +1299,9 @@ name = "tracing-core" version = "0.1.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +dependencies = [ + "once_cell", +] [[package]] name = "unicode-ident" @@ -1155,6 +1321,12 @@ version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a72e1902dde2bd6441347de2b70b7f5d59bf157c6c62f0c44572607a1d55bbe" +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "waker-fn" version = "1.1.1" @@ -1171,6 +1343,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "wasm-bindgen" version = "0.2.88" @@ -1352,3 +1530,23 @@ name = "windows_x86_64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "zerocopy" +version = "0.7.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cd369a67c0edfef15010f980c3cbe45d7f651deac2cd67ce097cd801de16557" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2f140bda219a26ccc0cdb03dba58af72590c53b22642577d88a927bc5c87d6b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/v2/robotmk/Cargo.toml b/v2/robotmk/Cargo.toml index 30103f03..2a58c5e0 100644 --- a/v2/robotmk/Cargo.toml +++ b/v2/robotmk/Cargo.toml @@ -22,6 +22,8 @@ serde = { version = "1.0.188", features = ["derive"] } serde_json = "*" sysinfo = "*" tempfile = "3.8.0" +tokio = { version = "1.33.0", features = ["full"] } +tokio-util = { version = "0.7.10", features = ["full"] } walkdir = "2.4.0" [[bin]] From 1888260152579f376102da04a4fa5f4a40cb25ab Mon Sep 17 00:00:00 2001 From: Solomon Jacobs Date: Tue, 7 Nov 2023 19:46:26 +0100 Subject: [PATCH 2/7] II: remove async-std: simplify control flow We want to remove async-std, since it is no longer maintained. --- .../bin/scheduler/child_process_supervisor.rs | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/v2/robotmk/src/bin/scheduler/child_process_supervisor.rs b/v2/robotmk/src/bin/scheduler/child_process_supervisor.rs index a5b6d162..3afe8da9 100644 --- a/v2/robotmk/src/bin/scheduler/child_process_supervisor.rs +++ b/v2/robotmk/src/bin/scheduler/child_process_supervisor.rs @@ -79,20 +79,17 @@ impl ChildProcessSupervisor<'_> { return Ok(ChildProcessOutcome::Terminated); } - if let Some(exit_status) = child - .try_wait() - .context(format!( - "Failed to query exit status of process {}, killing", - child.id() - )) - .map_err(|err| { + match child.try_wait() { + Ok(Some(exit_status)) => return Ok(ChildProcessOutcome::Exited(exit_status)), + Ok(None) => {} + e => { kill_child_tree(child); - err - })? - { - return Ok(ChildProcessOutcome::Exited(exit_status)); + e.context(format!( + "Failed to query exit status of process {}, killing", + child.id() + ))?; + } } - sleep(Duration::from_millis(250)).await } } From d733304348b547e869e196467a7ae4072a582919 Mon Sep 17 00:00:00 2001 From: Solomon Jacobs Date: Wed, 8 Nov 2023 09:14:50 +0100 Subject: [PATCH 3/7] III: remove async-std: move `wait_for_child_exit` to tokio We want to remove async-std, since it is no longer maintained. --- .../bin/scheduler/child_process_supervisor.rs | 49 ++++++++++--------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/v2/robotmk/src/bin/scheduler/child_process_supervisor.rs b/v2/robotmk/src/bin/scheduler/child_process_supervisor.rs index 3afe8da9..8bc38477 100644 --- a/v2/robotmk/src/bin/scheduler/child_process_supervisor.rs +++ b/v2/robotmk/src/bin/scheduler/child_process_supervisor.rs @@ -23,6 +23,31 @@ pub struct StdioPaths { pub stderr: Utf8PathBuf, } +async fn wait_for_child_exit( + flag: &TerminationFlag, + child: &mut Child, +) -> Result { + loop { + if flag.should_terminate() { + kill_child_tree(child); + return Ok(ChildProcessOutcome::Terminated); + } + + match child.try_wait() { + Ok(Some(exit_status)) => return Ok(ChildProcessOutcome::Exited(exit_status)), + Ok(None) => {} + e => { + kill_child_tree(child); + e.context(format!( + "Failed to query exit status of process {}, killing", + child.id() + ))?; + } + } + sleep(Duration::from_millis(250)).await + } +} + impl ChildProcessSupervisor<'_> { pub fn run(&self) -> Result { let mut command: Command = self.build_command()?; @@ -43,7 +68,7 @@ impl ChildProcessSupervisor<'_> { let mut child = command.spawn().context("Failed to spawn subprocess")?; match executor::block_on(timeout( Duration::from_secs(self.timeout), - self.wait_for_child_exit(&mut child), + wait_for_child_exit(self.termination_flag, &mut child), )) { Ok(child_result) => child_result, _ => { @@ -71,28 +96,6 @@ impl ChildProcessSupervisor<'_> { } Ok(command) } - - async fn wait_for_child_exit(&self, child: &mut Child) -> Result { - loop { - if self.termination_flag.should_terminate() { - kill_child_tree(child); - return Ok(ChildProcessOutcome::Terminated); - } - - match child.try_wait() { - Ok(Some(exit_status)) => return Ok(ChildProcessOutcome::Exited(exit_status)), - Ok(None) => {} - e => { - kill_child_tree(child); - e.context(format!( - "Failed to query exit status of process {}, killing", - child.id() - ))?; - } - } - sleep(Duration::from_millis(250)).await - } - } } pub enum ChildProcessOutcome { From b20bbeac8a6197467d0500e3e261054c54683bd8 Mon Sep 17 00:00:00 2001 From: Solomon Jacobs Date: Wed, 8 Nov 2023 09:21:47 +0100 Subject: [PATCH 4/7] IV: remove async-std: move `wait_for_child_exit` to tokio We want to remove async-std, since it is no longer maintained. --- .../bin/scheduler/child_process_supervisor.rs | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/v2/robotmk/src/bin/scheduler/child_process_supervisor.rs b/v2/robotmk/src/bin/scheduler/child_process_supervisor.rs index 8bc38477..b78a4970 100644 --- a/v2/robotmk/src/bin/scheduler/child_process_supervisor.rs +++ b/v2/robotmk/src/bin/scheduler/child_process_supervisor.rs @@ -3,13 +3,12 @@ use super::termination::kill_process_tree; use robotmk::termination::TerminationFlag; use anyhow::{Context, Result}; -use async_std::{future::timeout, task::sleep}; use camino::Utf8PathBuf; -use futures::executor; use log::{debug, error}; use std::process::{Child, Command, ExitStatus, Stdio}; use std::time::Duration; use sysinfo::{Pid, PidExt}; +use tokio::time::{timeout, sleep}; pub struct ChildProcessSupervisor<'a> { pub command_spec: &'a CommandSpec, @@ -48,6 +47,23 @@ async fn wait_for_child_exit( } } +#[tokio::main] +async fn wait_with_timeout( + duration: Duration, + flag: &TerminationFlag, + child: &mut Child, +) -> Result { + let child_future = wait_for_child_exit(flag, &mut child); + match timeout(duration, child_future).await { + Ok(child_result) => child_result, + _ => { + error!("Timed out"); + kill_child_tree(&child); + Ok(ChildProcessOutcome::TimedOut) + } + } +} + impl ChildProcessSupervisor<'_> { pub fn run(&self) -> Result { let mut command: Command = self.build_command()?; @@ -66,17 +82,7 @@ impl ChildProcessSupervisor<'_> { ); let mut child = command.spawn().context("Failed to spawn subprocess")?; - match executor::block_on(timeout( - Duration::from_secs(self.timeout), - wait_for_child_exit(self.termination_flag, &mut child), - )) { - Ok(child_result) => child_result, - _ => { - error!("Timed out"); - kill_child_tree(&child); - Ok(ChildProcessOutcome::TimedOut) - } - } + wait_with_timeout(Duration::from_secs(self.timeout), self.termination_flag, &mut child) } fn build_command(&self) -> Result { From 9b1e2334f4fb296990807792226d87651078503e Mon Sep 17 00:00:00 2001 From: Solomon Jacobs Date: Wed, 8 Nov 2023 19:09:02 +0100 Subject: [PATCH 5/7] V: remove async-std: create generic wait We want to remove async-std, since it is no longer maintained. --- .../bin/scheduler/child_process_supervisor.rs | 64 ++++++++----------- v2/robotmk/src/bin/scheduler/command_spec.rs | 8 +++ v2/robotmk/src/termination.rs | 27 ++++++++ 3 files changed, 60 insertions(+), 39 deletions(-) diff --git a/v2/robotmk/src/bin/scheduler/child_process_supervisor.rs b/v2/robotmk/src/bin/scheduler/child_process_supervisor.rs index b78a4970..a276fa1d 100644 --- a/v2/robotmk/src/bin/scheduler/child_process_supervisor.rs +++ b/v2/robotmk/src/bin/scheduler/child_process_supervisor.rs @@ -1,14 +1,14 @@ use super::command_spec::CommandSpec; use super::termination::kill_process_tree; -use robotmk::termination::TerminationFlag; +use robotmk::termination::{waited, Outcome, TerminationFlag}; use anyhow::{Context, Result}; use camino::Utf8PathBuf; use log::{debug, error}; -use std::process::{Child, Command, ExitStatus, Stdio}; +use std::process::{ExitStatus, Stdio}; use std::time::Duration; use sysinfo::{Pid, PidExt}; -use tokio::time::{timeout, sleep}; +use tokio::process::{Child, Command}; pub struct ChildProcessSupervisor<'a> { pub command_spec: &'a CommandSpec, @@ -22,45 +22,26 @@ pub struct StdioPaths { pub stderr: Utf8PathBuf, } -async fn wait_for_child_exit( - flag: &TerminationFlag, - child: &mut Child, -) -> Result { - loop { - if flag.should_terminate() { - kill_child_tree(child); - return Ok(ChildProcessOutcome::Terminated); - } - - match child.try_wait() { - Ok(Some(exit_status)) => return Ok(ChildProcessOutcome::Exited(exit_status)), - Ok(None) => {} - e => { - kill_child_tree(child); - e.context(format!( - "Failed to query exit status of process {}, killing", - child.id() - ))?; - } - } - sleep(Duration::from_millis(250)).await - } -} - -#[tokio::main] -async fn wait_with_timeout( +fn wait_for_child( duration: Duration, flag: &TerminationFlag, child: &mut Child, ) -> Result { - let child_future = wait_for_child_exit(flag, &mut child); - match timeout(duration, child_future).await { - Ok(child_result) => child_result, - _ => { + match waited(duration, flag, child.wait()) { + Outcome::Timeout => { error!("Timed out"); - kill_child_tree(&child); + kill_child_tree(child); Ok(ChildProcessOutcome::TimedOut) } + Outcome::Cancel => { + kill_child_tree(child); + Ok(ChildProcessOutcome::Terminated) + } + Outcome::Completed(Err(e)) => { + kill_child_tree(child); + Err(e.into()) + } + Outcome::Completed(Ok(o)) => Ok(ChildProcessOutcome::Exited(o)), } } @@ -81,8 +62,11 @@ impl ChildProcessSupervisor<'_> { self.command_spec, ); - let mut child = command.spawn().context("Failed to spawn subprocess")?; - wait_with_timeout(Duration::from_secs(self.timeout), self.termination_flag, &mut child) + wait_for_child( + Duration::from_secs(self.timeout), + self.termination_flag, + &mut command.spawn().context("Failed to spawn subprocess")?, + ) } fn build_command(&self) -> Result { @@ -110,6 +94,8 @@ pub enum ChildProcessOutcome { Terminated, } -fn kill_child_tree(child: &Child) { - kill_process_tree(&Pid::from_u32(child.id())) +fn kill_child_tree(child: &tokio::process::Child) { + if let Some(id) = child.id() { + kill_process_tree(&Pid::from_u32(id)) + } } diff --git a/v2/robotmk/src/bin/scheduler/command_spec.rs b/v2/robotmk/src/bin/scheduler/command_spec.rs index 1b30adab..89b6bf22 100644 --- a/v2/robotmk/src/bin/scheduler/command_spec.rs +++ b/v2/robotmk/src/bin/scheduler/command_spec.rs @@ -22,6 +22,14 @@ impl From<&CommandSpec> for Command { } } +impl From<&CommandSpec> for tokio::process::Command { + fn from(command_spec: &CommandSpec) -> Self { + let mut command = Self::new(&command_spec.executable); + command.args(&command_spec.arguments); + command + } +} + impl CommandSpec { pub fn new(executable: impl AsRef) -> Self { Self { diff --git a/v2/robotmk/src/termination.rs b/v2/robotmk/src/termination.rs index 3c97f1e6..38c92989 100644 --- a/v2/robotmk/src/termination.rs +++ b/v2/robotmk/src/termination.rs @@ -1,5 +1,8 @@ +use std::future::Future; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; +use std::time::Duration; +use tokio::{task::yield_now, time::sleep}; #[derive(Clone)] pub struct TerminationFlag(Arc); @@ -13,3 +16,27 @@ impl TerminationFlag { self.0.load(Ordering::Relaxed) } } + +pub enum Outcome { + Cancel, + Timeout, + Completed(T), +} + +#[tokio::main] +pub async fn waited(duration: Duration, flag: &TerminationFlag, future: F) -> Outcome +where + F: Future, +{ + async fn cancelled(flag: &TerminationFlag) { + while !flag.should_terminate() { + yield_now().await + } + } + + tokio::select! { + outcome = future => { Outcome::Completed(outcome) }, + _ = cancelled(flag) => { Outcome::Cancel }, + _ = sleep(duration) => { Outcome::Timeout }, + } +} From 33682eff9ac897e08c5f6a3b6cafd2867ae0c557 Mon Sep 17 00:00:00 2001 From: Solomon Jacobs Date: Thu, 9 Nov 2023 10:14:03 +0100 Subject: [PATCH 6/7] VI: remove async-std: `wait_for_task_exit` to tokio We want to remove async-std, since it is no longer maintained. --- .../src/bin/scheduler/sessions/schtasks.rs | 76 +++++++++---------- 1 file changed, 36 insertions(+), 40 deletions(-) diff --git a/v2/robotmk/src/bin/scheduler/sessions/schtasks.rs b/v2/robotmk/src/bin/scheduler/sessions/schtasks.rs index 637a47fc..028ff4af 100644 --- a/v2/robotmk/src/bin/scheduler/sessions/schtasks.rs +++ b/v2/robotmk/src/bin/scheduler/sessions/schtasks.rs @@ -2,13 +2,11 @@ use super::session::RunOutcome; use crate::command_spec::CommandSpec; use crate::logging::log_and_return_error; use crate::termination::kill_process_tree; -use robotmk::termination::TerminationFlag; +use robotmk::termination::{waited, Outcome, TerminationFlag}; use anyhow::{bail, Context, Result}; -use async_std::{future::timeout, task::sleep as async_sleep}; use camino::{Utf8Path, Utf8PathBuf}; use chrono::{Duration as ChronoDuration, Local}; -use futures::executor; use log::{debug, error}; use std::fs::{read_to_string, write}; use std::process::Command; @@ -16,6 +14,32 @@ use std::str::FromStr; use std::thread::sleep; use std::time::Duration; use sysinfo::Pid; +use tokio::task::yield_now; + +fn wait_for_task_exit(task: &TaskSpec, paths: &Paths) -> Result { + let duration = Duration::from_secs(task.timeout); + let queried = query(task.task_name, &paths.exit_code); + match waited(duration, task.termination_flag, queried) { + Outcome::Cancel => { + kill_and_delete_task(task.task_name, &paths.pid); + Ok(RunOutcome::TimedOut) + } + Outcome::Timeout => { + error!("Timeout"); + kill_and_delete_task(task.task_name, &paths.pid); + Ok(RunOutcome::Terminated) + } + Outcome::Completed(Err(e)) => { + kill_and_delete_task(task.task_name, &paths.pid); + Err(e) + } + Outcome::Completed(Ok(code)) => { + debug!("Task {} completed", task.task_name); + delete_task(task.task_name); + Ok(RunOutcome::Exited(Some(code))) + } + } +} pub fn run_task(task_spec: &TaskSpec) -> Result { debug!( @@ -31,32 +55,7 @@ pub fn run_task(task_spec: &TaskSpec) -> Result { run_schtasks(["/run", "/tn", task_spec.task_name]) .context(format!("Failed to start task {}", task_spec.task_name))?; - if let Some(run_outcome) = match executor::block_on(timeout( - Duration::from_secs(task_spec.timeout), - wait_for_task_exit(task_spec.task_name, task_spec.termination_flag, &paths.pid), - )) { - Ok(task_wait_result) => task_wait_result.map_err(|err| { - kill_and_delete_task(task_spec.task_name, &paths.pid); - err - })?, - _ => { - error!("Timed out"); - kill_and_delete_task(task_spec.task_name, &paths.pid); - return Ok(RunOutcome::TimedOut); - } - } { - return Ok(run_outcome); - }; - debug!("Task {} completed", task_spec.task_name); - - delete_task(task_spec.task_name); - - let raw_exit_code = read_until_first_whitespace(&paths.exit_code)?; - Ok(RunOutcome::Exited(Some( - raw_exit_code - .parse::() - .context(format!("Failed to parse {} as i32", raw_exit_code))?, - ))) + wait_for_task_exit(task_spec, &paths) } pub struct TaskSpec<'a> { @@ -162,22 +161,19 @@ where )) } -async fn wait_for_task_exit( - task_name: &str, - termination_flag: &TerminationFlag, - path_pid: &Utf8Path, -) -> Result> { +async fn query(task_name: &str, exit_path: &Utf8Path) -> Result { debug!("Waiting for task {} to complete", task_name); while query_if_task_is_running(task_name) .context(format!("Failed to query if task {task_name} is running"))? { - if termination_flag.should_terminate() { - kill_and_delete_task(task_name, path_pid); - return Ok(Some(RunOutcome::Terminated)); - } - async_sleep(Duration::from_millis(250)).await + yield_now().await } - Ok(None) + + let raw_exit_code = read_until_first_whitespace(exit_path)?; + let exit_code: i32 = raw_exit_code + .parse() + .context(format!("Failed to parse {} as i32", raw_exit_code))?; + Ok(exit_code) } fn query_if_task_is_running(task_name: &str) -> Result { From 82e6635a8e02a0bb8fd3e4242f1f2310d33583ba Mon Sep 17 00:00:00 2001 From: Solomon Jacobs Date: Thu, 9 Nov 2023 10:16:59 +0100 Subject: [PATCH 7/7] VII: remove `async-std` & `futures` `futures` is no longer needed, because of the addition of tokio. We want to remove async-std, since it is no longer maintained. --- v2/robotmk/Cargo.lock | 357 +----------------------------------------- v2/robotmk/Cargo.toml | 2 - 2 files changed, 6 insertions(+), 353 deletions(-) diff --git a/v2/robotmk/Cargo.lock b/v2/robotmk/Cargo.lock index b5e389df..357de382 100644 --- a/v2/robotmk/Cargo.lock +++ b/v2/robotmk/Cargo.lock @@ -116,113 +116,6 @@ dependencies = [ "backtrace", ] -[[package]] -name = "async-channel" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81953c529336010edd6d8e358f886d9581267795c61b19475b71314bffa46d35" -dependencies = [ - "concurrent-queue", - "event-listener", - "futures-core", -] - -[[package]] -name = "async-executor" -version = "1.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b0c4a4f319e45986f347ee47fef8bf5e81c9abc3f6f58dc2391439f30df65f0" -dependencies = [ - "async-lock", - "async-task", - "concurrent-queue", - "fastrand 2.0.1", - "futures-lite", - "slab", -] - -[[package]] -name = "async-global-executor" -version = "2.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1b6f5d7df27bd294849f8eec66ecfc63d11814df7a4f5d74168a2394467b776" -dependencies = [ - "async-channel", - "async-executor", - "async-io", - "async-lock", - "blocking", - "futures-lite", - "once_cell", -] - -[[package]] -name = "async-io" -version = "1.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fc5b45d93ef0529756f812ca52e44c221b35341892d3dcc34132ac02f3dd2af" -dependencies = [ - "async-lock", - "autocfg", - "cfg-if", - "concurrent-queue", - "futures-lite", - "log", - "parking", - "polling", - "rustix 0.37.27", - "slab", - "socket2 0.4.10", - "waker-fn", -] - -[[package]] -name = "async-lock" -version = "2.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "287272293e9d8c41773cec55e365490fe034813a2f172f502d6ddcf75b2f582b" -dependencies = [ - "event-listener", -] - -[[package]] -name = "async-std" -version = "1.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62565bb4402e926b29953c785397c6dc0391b7b446e45008b0049eb43cec6f5d" -dependencies = [ - "async-channel", - "async-global-executor", - "async-io", - "async-lock", - "crossbeam-utils", - "futures-channel", - "futures-core", - "futures-io", - "futures-lite", - "gloo-timers", - "kv-log-macro", - "log", - "memchr", - "once_cell", - "pin-project-lite", - "pin-utils", - "slab", - "wasm-bindgen-futures", -] - -[[package]] -name = "async-task" -version = "4.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4eb2cdb97421e01129ccb49169d8279ed21e829929144f4a22a6e54ac549ca1" - -[[package]] -name = "atomic-waker" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" - [[package]] name = "autocfg" version = "1.1.0" @@ -262,22 +155,6 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" -[[package]] -name = "blocking" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c36a4d0d48574b3dd360b4b7d95cc651d2b6557b6402848a27d4b228a473e2a" -dependencies = [ - "async-channel", - "async-lock", - "async-task", - "fastrand 2.0.1", - "futures-io", - "futures-lite", - "piper", - "tracing", -] - [[package]] name = "bumpalo" version = "3.14.0" @@ -383,15 +260,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" -[[package]] -name = "concurrent-queue" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f057a694a54f12365049b0958a1685bb52d567f5593b355fbf685838e873d400" -dependencies = [ - "crossbeam-utils", -] - [[package]] name = "core-foundation-sys" version = "0.8.4" @@ -457,21 +325,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "event-listener" -version = "2.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" - -[[package]] -name = "fastrand" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be" -dependencies = [ - "instant", -] - [[package]] name = "fastrand" version = "2.0.1" @@ -500,73 +353,22 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29f9df8a11882c4e3335eb2d18a0137c505d9ca927470b0cac9c6f0ae07d28f7" dependencies = [ - "rustix 0.38.21", + "rustix", "windows-sys", ] -[[package]] -name = "futures" -version = "0.3.29" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da0290714b38af9b4a7b094b8a37086d1b4e61f2df9122c3cad2577669145335" -dependencies = [ - "futures-channel", - "futures-core", - "futures-executor", - "futures-io", - "futures-sink", - "futures-task", - "futures-util", -] - -[[package]] -name = "futures-channel" -version = "0.3.29" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff4dd66668b557604244583e3e1e1eada8c5c2e96a6d0d6653ede395b78bbacb" -dependencies = [ - "futures-core", - "futures-sink", -] - [[package]] name = "futures-core" version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c" -[[package]] -name = "futures-executor" -version = "0.3.29" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f4fb8693db0cf099eadcca0efe2a5a22e4550f98ed16aba6c48700da29597bc" -dependencies = [ - "futures-core", - "futures-task", - "futures-util", -] - [[package]] name = "futures-io" version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa" -[[package]] -name = "futures-lite" -version = "1.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49a9d51ce47660b1e808d3c990b4709f2f415d928835a17dfd16991515c46bce" -dependencies = [ - "fastrand 1.9.0", - "futures-core", - "futures-io", - "memchr", - "parking", - "pin-project-lite", - "waker-fn", -] - [[package]] name = "futures-macro" version = "0.3.29" @@ -596,13 +398,9 @@ version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a19526d624e703a3179b3d322efec918b6246ea0fa51d41124525f00f1cc8104" dependencies = [ - "futures-channel", "futures-core", - "futures-io", "futures-macro", - "futures-sink", "futures-task", - "memchr", "pin-project-lite", "pin-utils", "slab", @@ -620,18 +418,6 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" -[[package]] -name = "gloo-timers" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b995a66bb87bebce9a0f4a95aed01daca4872c050bfcb21653361c03bc35e5c" -dependencies = [ - "futures-channel", - "futures-core", - "js-sys", - "wasm-bindgen", -] - [[package]] name = "hashbrown" version = "0.14.2" @@ -677,26 +463,6 @@ dependencies = [ "cc", ] -[[package]] -name = "instant" -version = "0.1.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "io-lifetimes" -version = "1.0.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2" -dependencies = [ - "hermit-abi", - "libc", - "windows-sys", -] - [[package]] name = "is-terminal" version = "0.4.9" @@ -704,7 +470,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" dependencies = [ "hermit-abi", - "rustix 0.38.21", + "rustix", "windows-sys", ] @@ -723,15 +489,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "kv-log-macro" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f" -dependencies = [ - "log", -] - [[package]] name = "lazy_static" version = "1.4.0" @@ -744,12 +501,6 @@ version = "0.2.149" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" -[[package]] -name = "linux-raw-sys" -version = "0.3.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" - [[package]] name = "linux-raw-sys" version = "0.4.10" @@ -771,9 +522,6 @@ name = "log" version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" -dependencies = [ - "value-bag", -] [[package]] name = "memchr" @@ -873,12 +621,6 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" -[[package]] -name = "parking" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb813b8af86854136c6922af0598d719255ecb2179515e6e7730d468f05c9cae" - [[package]] name = "parking_lot" version = "0.12.1" @@ -914,33 +656,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" -[[package]] -name = "piper" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "668d31b1c4eba19242f2088b2bf3316b82ca31082a8335764db4e083db7485d4" -dependencies = [ - "atomic-waker", - "fastrand 2.0.1", - "futures-io", -] - -[[package]] -name = "polling" -version = "2.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b2d323e8ca7996b3e23126511a523f7e62924d93ecd5ae73b333815b0eb3dce" -dependencies = [ - "autocfg", - "bitflags 1.3.2", - "cfg-if", - "concurrent-queue", - "libc", - "log", - "pin-project-lite", - "windows-sys", -] - [[package]] name = "proc-macro2" version = "1.0.69" @@ -1022,7 +737,6 @@ name = "robotmk" version = "0.1.0" dependencies = [ "anyhow", - "async-std", "base64", "camino", "chrono", @@ -1031,7 +745,6 @@ dependencies = [ "ctrlc", "flexi_logger", "fs4", - "futures", "log", "serde", "serde_json", @@ -1048,20 +761,6 @@ version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" -[[package]] -name = "rustix" -version = "0.37.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fea8ca367a3a01fe35e6943c400addf443c0f57670e6ec51196f71a4b8762dd2" -dependencies = [ - "bitflags 1.3.2", - "errno", - "io-lifetimes", - "libc", - "linux-raw-sys 0.3.8", - "windows-sys", -] - [[package]] name = "rustix" version = "0.38.21" @@ -1071,7 +770,7 @@ dependencies = [ "bitflags 2.4.1", "errno", "libc", - "linux-raw-sys 0.4.10", + "linux-raw-sys", "windows-sys", ] @@ -1151,16 +850,6 @@ version = "1.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a" -[[package]] -name = "socket2" -version = "0.4.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f7916fc008ca5542385b89a3d3ce689953c143e9304a9bf8beec1de48994c0d" -dependencies = [ - "libc", - "winapi", -] - [[package]] name = "socket2" version = "0.5.5" @@ -1210,9 +899,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ef1adac450ad7f4b3c28589471ade84f25f731a7a0fe30d71dfa9f60fd808e5" dependencies = [ "cfg-if", - "fastrand 2.0.1", + "fastrand", "redox_syscall", - "rustix 0.38.21", + "rustix", "windows-sys", ] @@ -1250,7 +939,7 @@ dependencies = [ "parking_lot", "pin-project-lite", "signal-hook-registry", - "socket2 0.5.5", + "socket2", "tokio-macros", "windows-sys", ] @@ -1315,24 +1004,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" -[[package]] -name = "value-bag" -version = "1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a72e1902dde2bd6441347de2b70b7f5d59bf157c6c62f0c44572607a1d55bbe" - [[package]] name = "version_check" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" -[[package]] -name = "waker-fn" -version = "1.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3c4517f54858c779bbcbf228f4fca63d121bf85fbecb2dc578cdf4a39395690" - [[package]] name = "walkdir" version = "2.4.0" @@ -1374,18 +1051,6 @@ dependencies = [ "wasm-bindgen-shared", ] -[[package]] -name = "wasm-bindgen-futures" -version = "0.4.38" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9afec9963e3d0994cac82455b2b3502b81a7f40f9a0d32181f7528d9f4b43e02" -dependencies = [ - "cfg-if", - "js-sys", - "wasm-bindgen", - "web-sys", -] - [[package]] name = "wasm-bindgen-macro" version = "0.2.88" @@ -1415,16 +1080,6 @@ version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d046c5d029ba91a1ed14da14dca44b68bf2f124cfbaf741c54151fdb3e0750b" -[[package]] -name = "web-sys" -version = "0.3.65" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5db499c5f66323272151db0e666cd34f78617522fb0c1604d31a27c50c206a85" -dependencies = [ - "js-sys", - "wasm-bindgen", -] - [[package]] name = "winapi" version = "0.3.9" diff --git a/v2/robotmk/Cargo.toml b/v2/robotmk/Cargo.toml index 2a58c5e0..72c798e3 100644 --- a/v2/robotmk/Cargo.toml +++ b/v2/robotmk/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" [dependencies] anyhow = { version = "*", features = ["backtrace"] } -async-std = "*" base64 = "*" camino = { version = "1.1.6", features = ["serde1"] } chrono = "0.4.31" @@ -16,7 +15,6 @@ clokwerk = "*" ctrlc = "*" flexi_logger = "*" fs4 = "0.7.0" -futures = "*" log = "*" serde = { version = "1.0.188", features = ["derive"] } serde_json = "*"