Skip to content

Commit

Permalink
Handle Root Future Termination in Shutdown Scenarios
Browse files Browse the repository at this point in the history
This is crucial because when the `start` method is executed
in a runtime that is not the main one, the lifecycle of futures
spawned within the `start` method continues, even after the `jdc`
process terminates.The earlier approach of simply exiting the
loop worked because the `start` method was executed on the main
blocking thread, where the Tokio runtime was defined. However,
in the case of integration tests, the runtime runs on a different
blocking thread, necessitating proper handling of the root
future termination.
  • Loading branch information
Shourya742 committed Jan 24, 2025
1 parent 643d16f commit ec0cdc2
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions roles/jd-client/src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,18 @@ impl JobDeclaratorClient {
let task_collector = task_collector.clone();
let tx_status = tx_status.clone();
let proxy_config = proxy_config.clone();
let root_handler;
if let Some(upstream) = proxy_config.upstreams.get(upstream_index) {
let tx_status = tx_status.clone();
let task_collector = task_collector.clone();
let upstream = upstream.clone();
tokio::spawn(async move {
root_handler = tokio::spawn(async move {
Self::initialize_jd(proxy_config, tx_status, task_collector, upstream).await;
});
} else {
let tx_status = tx_status.clone();
let task_collector = task_collector.clone();
tokio::spawn(async move {
root_handler = tokio::spawn(async move {
Self::initialize_jd_as_solo_miner(
proxy_config,
tx_status.clone(),
Expand Down Expand Up @@ -165,11 +166,27 @@ impl JobDeclaratorClient {
}
} else {
info!("Received unknown task. Shutting down.");
task_collector
.safe_lock(|s| {
for handle in s {
handle.abort();
}
})
.unwrap();
root_handler.abort();
break 'outer;
}
},
_ = self.shutdown.notified().fuse() => {
info!("Shutting down gracefully...");
task_collector
.safe_lock(|s| {
for handle in s {
handle.abort();
}
})
.unwrap();
root_handler.abort();
break 'outer;
}
};
Expand Down

0 comments on commit ec0cdc2

Please sign in to comment.