Skip to content

Commit ab73645

Browse files
committed
wasm fixes and made 'run_until_shutdown' take reference instead of ownership
1 parent 6a11500 commit ab73645

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

common/task/src/cancellation/manager.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ impl ShutdownManager {
480480
/// - all tracked tasks have terminated
481481
/// - timeout has been reached
482482
/// - shutdown has been forced (by sending SIGINT)
483-
async fn finish_shutdown(mut self) {
483+
async fn finish_shutdown(&mut self) {
484484
let mut wait_futures = FuturesUnordered::<Pin<Box<dyn Future<Output = ()> + Send>>>::new();
485485

486486
// force shutdown via ctrl-c
@@ -496,14 +496,16 @@ impl ShutdownManager {
496496
}));
497497

498498
// timeout
499+
let max_shutdown = self.max_shutdown_duration;
499500
wait_futures.push(Box::pin(async move {
500-
sleep(self.max_shutdown_duration).await;
501+
sleep(max_shutdown).await;
501502
info!("timeout reached - forcing shutdown");
502503
}));
503504

504505
// graceful
506+
let tracker = self.tracker.clone();
505507
wait_futures.push(Box::pin(async move {
506-
self.wait_for_tracker().await;
508+
tracker.wait_for_tracker().await;
507509
info!("all tracked tasks successfully shutdown");
508510
if let Some(legacy) = self.legacy_task_manager.as_mut() {
509511
legacy.wait_for_graceful_shutdown().await;
@@ -555,15 +557,15 @@ impl ShutdownManager {
555557
/// - all tracked tasks have terminated
556558
/// - timeout has been reached
557559
/// - shutdown has been forced (by sending SIGINT)
558-
pub async fn perform_shutdown(self) {
560+
pub async fn perform_shutdown(&mut self) {
559561
self.send_cancellation();
560562

561563
info!("waiting for tasks to finish... (press ctrl-c to force)");
562564
self.finish_shutdown().await;
563565
}
564566

565567
/// Wait until a shutdown signal has been received and trigger system shutdown.
566-
pub async fn run_until_shutdown(mut self) {
568+
pub async fn run_until_shutdown(&mut self) {
567569
self.close_tracker();
568570
self.wait_for_shutdown_signal().await;
569571

@@ -580,11 +582,11 @@ mod tests {
580582

581583
#[tokio::test]
582584
async fn shutdown_with_no_tracked_tasks_and_signals() -> anyhow::Result<()> {
583-
let manager = ShutdownManager::new_without_signals();
585+
let mut manager = ShutdownManager::new_without_signals();
584586
let res = manager.run_until_shutdown().timeboxed().await;
585587
assert!(res.has_elapsed());
586588

587-
let manager = ShutdownManager::new_without_signals();
589+
let mut manager = ShutdownManager::new_without_signals();
588590
let shutdown = manager.clone_shutdown_token();
589591
shutdown.cancel();
590592
let res = manager.run_until_shutdown().timeboxed().await;
@@ -596,7 +598,7 @@ mod tests {
596598
#[tokio::test]
597599
async fn shutdown_signal() -> anyhow::Result<()> {
598600
let timeout_shutdown = sleep(Duration::from_millis(100));
599-
let manager = ShutdownManager::new_without_signals().with_shutdown(timeout_shutdown);
601+
let mut manager = ShutdownManager::new_without_signals().with_shutdown(timeout_shutdown);
600602

601603
// execution finishes after the sleep gets finishes
602604
let res = manager
@@ -610,7 +612,7 @@ mod tests {
610612

611613
#[tokio::test]
612614
async fn panic_hook() -> anyhow::Result<()> {
613-
let manager = ShutdownManager::new_without_signals().with_cancel_on_panic();
615+
let mut manager = ShutdownManager::new_without_signals().with_cancel_on_panic();
614616
manager.spawn_with_shutdown(async move {
615617
sleep(Duration::from_millis(10000)).await;
616618
});
@@ -632,7 +634,7 @@ mod tests {
632634
#[tokio::test]
633635
async fn task_cancellation() -> anyhow::Result<()> {
634636
let timeout_shutdown = sleep(Duration::from_millis(100));
635-
let manager = ShutdownManager::new_without_signals().with_shutdown(timeout_shutdown);
637+
let mut manager = ShutdownManager::new_without_signals().with_shutdown(timeout_shutdown);
636638

637639
let cancelled1 = Arc::new(AtomicBool::new(false));
638640
let cancelled1_clone = cancelled1.clone();
@@ -664,7 +666,7 @@ mod tests {
664666

665667
#[tokio::test]
666668
async fn cancellation_within_task() -> anyhow::Result<()> {
667-
let manager = ShutdownManager::new_without_signals();
669+
let mut manager = ShutdownManager::new_without_signals();
668670

669671
let cancelled1 = Arc::new(AtomicBool::new(false));
670672
let cancelled1_clone = cancelled1.clone();
@@ -694,7 +696,7 @@ mod tests {
694696
#[tokio::test]
695697
async fn shutdown_timeout() -> anyhow::Result<()> {
696698
let timeout_shutdown = sleep(Duration::from_millis(50));
697-
let manager = ShutdownManager::new_without_signals()
699+
let mut manager = ShutdownManager::new_without_signals()
698700
.with_shutdown(timeout_shutdown)
699701
.with_shutdown_duration(Duration::from_millis(1000));
700702

@@ -711,7 +713,7 @@ mod tests {
711713
assert!(res.has_elapsed());
712714

713715
let timeout_shutdown = sleep(Duration::from_millis(50));
714-
let manager = ShutdownManager::new_without_signals()
716+
let mut manager = ShutdownManager::new_without_signals()
715717
.with_shutdown(timeout_shutdown)
716718
.with_shutdown_duration(Duration::from_millis(100));
717719

wasm/client/src/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use serde::{Deserialize, Serialize};
1717
use std::sync::Arc;
1818
use tsify::Tsify;
1919
use wasm_bindgen::prelude::*;
20-
use wasm_bindgen_futures::{future_to_promise, spawn_local};
20+
use wasm_bindgen_futures::future_to_promise;
2121
use wasm_client_core::client::base_client::storage::GatewaysDetailsStore;
2222
use wasm_client_core::client::{
2323
base_client::{BaseClientBuilder, ClientInput, ClientOutput, ClientState},

wasm/mix-fetch/src/client.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use wasm_client_core::client::base_client::{BaseClientBuilder, ClientInput, Clie
2020
use wasm_client_core::client::inbound_messages::InputMessage;
2121
use wasm_client_core::helpers::{add_gateway, generate_new_client_keys};
2222
use wasm_client_core::nym_task::connections::TransmissionLane;
23-
use wasm_client_core::nym_task::TaskManager;
23+
use wasm_client_core::nym_task::ShutdownManager;
2424
use wasm_client_core::storage::core_client_traits::FullWasmClientStorage;
2525
use wasm_client_core::storage::wasm_client_traits::WasmClientStorage;
2626
use wasm_client_core::storage::ClientStorage;
@@ -41,7 +41,7 @@ pub struct MixFetchClient {
4141
requests: ActiveRequests,
4242

4343
// this has to be guarded by a mutex to be able to disconnect with an immutable reference
44-
_task_manager: Mutex<TaskManager>,
44+
_shutdown_manager: Mutex<ShutdownManager>,
4545
}
4646

4747
#[wasm_bindgen]
@@ -187,12 +187,11 @@ impl MixFetchClientBuilder {
187187
self_address,
188188
client_input,
189189
requests: active_requests,
190-
// this cannot failed as we haven't passed an external task manager
191-
_task_manager: Mutex::new(
190+
// this cannot fail as we haven't passed an external task manager
191+
_shutdown_manager: Mutex::new(
192192
started_client
193193
.shutdown_handle
194-
.try_into_task_manager()
195-
.unwrap(),
194+
.expect("shutdown manager missing"),
196195
),
197196
})
198197
}
@@ -234,11 +233,11 @@ impl MixFetchClient {
234233
self.invalidated.store(true, Ordering::Relaxed);
235234

236235
console_log!("sending shutdown signal");
237-
let mut shutdown_guard = self._task_manager.lock().await;
238-
shutdown_guard.signal_shutdown().ok();
236+
let mut shutdown_guard = self._shutdown_manager.lock().await;
237+
shutdown_guard.send_cancellation();
239238

240239
console_log!("waiting for shutdown to complete");
241-
shutdown_guard.wait_for_shutdown().await;
240+
shutdown_guard.run_until_shutdown().await;
242241

243242
self.requests.invalidate_all().await;
244243

0 commit comments

Comments
 (0)