Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use task manager graceful shutdown #5591

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions bin/reth/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,9 +728,10 @@ impl<Ext: RethCliExt> NodeCommand<Ext> {
task_executor.spawn_critical("p2p eth request handler", eth);

let known_peers_file = self.network.persistent_peers_file(default_peers_path);
task_executor.spawn_critical_with_shutdown_signal("p2p network task", |shutdown| {
run_network_until_shutdown(shutdown, network, known_peers_file)
});
task_executor
.spawn_critical_with_graceful_shutdown_signal("p2p network task", |shutdown| {
run_network_until_shutdown(shutdown, network, known_peers_file)
});

handle
}
Expand Down Expand Up @@ -1029,17 +1030,20 @@ impl<Ext: RethCliExt> NodeCommand<Ext> {
/// Drives the [NetworkManager] future until a [Shutdown](reth_tasks::shutdown::Shutdown) signal is
/// received. If configured, this writes known peers to `persistent_peers_file` afterwards.
async fn run_network_until_shutdown<C>(
shutdown: reth_tasks::shutdown::Shutdown,
shutdown: reth_tasks::shutdown::GracefulShutdown,
network: NetworkManager<C>,
persistent_peers_file: Option<PathBuf>,
) where
C: BlockReader + HeaderProvider + Clone + Unpin + 'static,
{
pin_mut!(network, shutdown);

let mut graceful_guard = None;
tokio::select! {
_ = &mut network => {},
_ = shutdown => {},
guard = shutdown => {
graceful_guard = Some(guard);
},
}

if let Some(file_path) = persistent_peers_file {
Expand All @@ -1057,6 +1061,8 @@ async fn run_network_until_shutdown<C>(
}
}
}

drop(graceful_guard)
}

#[cfg(test)]
Expand Down
8 changes: 5 additions & 3 deletions bin/reth/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ impl CliRunner {
task_manager,
run_until_ctrl_c(command(context)),
))?;
// after the command has finished or exit signal was received we drop the task manager which
// fires the shutdown signal to all tasks spawned via the task executor
drop(task_manager);

// after the command has finished or exit signal was received we shutdown the task manager
// which fires the shutdown signal to all tasks spawned via the task executor and
// awaiting on tasks spawned with graceful shutdown
task_manager.graceful_shutdown_with_timeout(std::time::Duration::from_secs(10));

// drop the tokio runtime on a separate thread because drop blocks until its pools
// (including blocking pool) are shutdown. In other words `drop(tokio_runtime)` would block
Expand Down
Loading