Skip to content

Commit

Permalink
track shutdown signals + do not wait aimlessly if no shutdown token p…
Browse files Browse the repository at this point in the history
…resent
  • Loading branch information
bragov4ik committed Nov 19, 2024
1 parent 44c0d06 commit 97c9a64
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions libs/blockscout-service-launcher/src/launcher/launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{
use actix_web::{middleware::Condition, App, HttpServer};
use actix_web_prom::PrometheusMetrics;
use std::{net::SocketAddr, time::Duration};
use tokio::task::JoinSet;
use tokio::{task::JoinSet, time::timeout};
use tokio_util::{sync::CancellationToken, task::TaskTracker};
use tracing_actix_web::TracingLogger;

Expand Down Expand Up @@ -64,13 +64,21 @@ where
Ok(())
});
}
if let Some(ref shutdown) = shutdown {
let shutdown = shutdown.clone();
futures.spawn(tracker.track_future(async move { Ok(shutdown.cancelled().await) }));
}

let res = futures.join_next().await.expect("future set is not empty");
tracker.close();
shutdown.map(|s| s.cancel());
match tokio::time::timeout(Duration::from_secs(SHUTDOWN_TIMEOUT_SEC), tracker.wait()).await {
Ok(_) => (),
Err(_) => futures.abort_all(),
if let Some(shutdown) = shutdown {
shutdown.cancel();
if let Err(_) = timeout(Duration::from_secs(SHUTDOWN_TIMEOUT_SEC), tracker.wait()).await {
// timed out; fallback to simple task abort
futures.abort_all();
}
} else {
futures.abort_all();
}
tracker.wait().await;
futures.join_all().await;
Expand Down

0 comments on commit 97c9a64

Please sign in to comment.