From a16c5667199a17de809f972eb48da47db0df4c06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Mon, 5 Dec 2022 14:44:44 +0100 Subject: [PATCH] common/task: extract out spawn_with_report_error (#1837) --- Cargo.lock | 2 ++ clients/socks5/src/client/mod.rs | 33 +++++++++++++------------------- common/task/Cargo.toml | 6 ++++++ common/task/src/lib.rs | 3 +++ common/task/src/spawn.rs | 33 ++++++++++++++++++++++++++++++++ nym-connect/Cargo.lock | 2 ++ 6 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 common/task/src/spawn.rs diff --git a/Cargo.lock b/Cargo.lock index f3c4c24118..2442e93ce5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5700,6 +5700,8 @@ dependencies = [ "log", "thiserror", "tokio", + "wasm-bindgen", + "wasm-bindgen-futures", ] [[package]] diff --git a/clients/socks5/src/client/mod.rs b/clients/socks5/src/client/mod.rs index 0784666af8..07a06e08d0 100644 --- a/clients/socks5/src/client/mod.rs +++ b/clients/socks5/src/client/mod.rs @@ -80,7 +80,7 @@ impl NymClient { client_input: ClientInput, client_output: ClientOutput, self_address: Recipient, - mut shutdown: ShutdownListener, + shutdown: ShutdownListener, ) { info!("Starting socks5 listener..."); let auth_methods = vec![AuthenticationMethods::NoAuth as u8]; @@ -103,25 +103,18 @@ impl NymClient { shared_lane_queue_lengths, shutdown.clone(), ); - tokio::spawn(async move { - // Ideally we should have a fully fledged task manager to check for errors in all - // tasks. - // However, pragmatically, we start out by at least reporting errors for some of the - // tasks that interact with the outside world and can fail in normal operation, such as - // network issues. - // TODO: replace this by a generic solution, such as a task manager that stores all - // JoinHandles of all spawned tasks. - if let Err(res) = sphinx_socks - .serve( - input_sender, - received_buffer_request_sender, - connection_command_sender, - ) - .await - { - shutdown.send_we_stopped(Box::new(res)); - } - }); + task::spawn_with_report_error( + async move { + sphinx_socks + .serve( + input_sender, + received_buffer_request_sender, + connection_command_sender, + ) + .await + }, + shutdown, + ); } /// blocking version of `start` method. Will run forever (or until SIGINT is sent) diff --git a/common/task/Cargo.toml b/common/task/Cargo.toml index 8240207575..ee83423f5f 100644 --- a/common/task/Cargo.toml +++ b/common/task/Cargo.toml @@ -15,5 +15,11 @@ tokio = { version = "1.21.2", features = ["macros", "sync"] } version = "1.21.2" features = ["signal", "time"] +[target."cfg(target_arch = \"wasm32\")".dependencies.wasm-bindgen-futures] +version = "0.4" + +[target."cfg(target_arch = \"wasm32\")".dependencies.wasm-bindgen] +version = "0.2.83" + [dev-dependencies] tokio = { version = "1.21.2", features = ["rt-multi-thread", "net", "signal", "test-util", "macros"] } diff --git a/common/task/src/lib.rs b/common/task/src/lib.rs index e2b5f623df..667936a830 100644 --- a/common/task/src/lib.rs +++ b/common/task/src/lib.rs @@ -4,7 +4,10 @@ pub mod shutdown; #[cfg(not(target_arch = "wasm32"))] pub mod signal; +pub mod spawn; pub use shutdown::{ShutdownListener, ShutdownNotifier}; #[cfg(not(target_arch = "wasm32"))] pub use signal::{wait_for_signal, wait_for_signal_and_error}; + +pub use spawn::spawn_with_report_error; diff --git a/common/task/src/spawn.rs b/common/task/src/spawn.rs new file mode 100644 index 0000000000..1286abf6dc --- /dev/null +++ b/common/task/src/spawn.rs @@ -0,0 +1,33 @@ +use crate::ShutdownListener; +use std::future::Future; + +#[cfg(target_arch = "wasm32")] +pub(crate) fn spawn(future: F) +where + F: Future + 'static, +{ + wasm_bindgen_futures::spawn_local(future); +} + +#[cfg(not(target_arch = "wasm32"))] +pub(crate) fn spawn(future: F) +where + F: Future + Send + 'static, + F::Output: Send + 'static, +{ + tokio::spawn(future); +} + +pub fn spawn_with_report_error(future: F, mut shutdown: ShutdownListener) +where + F: Future> + Send + 'static, + T: 'static, + E: std::error::Error + Send + 'static, +{ + let future_that_sends = async move { + if let Err(err) = future.await { + shutdown.send_we_stopped(Box::new(err)); + } + }; + spawn(future_that_sends); +} diff --git a/nym-connect/Cargo.lock b/nym-connect/Cargo.lock index 8d2d5d1ceb..298996b5f3 100644 --- a/nym-connect/Cargo.lock +++ b/nym-connect/Cargo.lock @@ -5464,6 +5464,8 @@ dependencies = [ "log", "thiserror", "tokio", + "wasm-bindgen", + "wasm-bindgen-futures", ] [[package]]