From 7b5864329db6010dd774a9f234de7500b0647d4a Mon Sep 17 00:00:00 2001 From: joshieDo <93316087+joshieDo@users.noreply.github.com> Date: Wed, 12 Jun 2024 20:49:05 +0200 Subject: [PATCH] fix: add a 5 second timeout for `tokio_runtime` shutdown (#8771) Co-authored-by: Matthias Seitz --- crates/cli/runner/src/lib.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/crates/cli/runner/src/lib.rs b/crates/cli/runner/src/lib.rs index 9d5fea6a1..a848ad0b2 100644 --- a/crates/cli/runner/src/lib.rs +++ b/crates/cli/runner/src/lib.rs @@ -11,7 +11,7 @@ //! Entrypoint for running commands. use reth_tasks::{TaskExecutor, TaskManager}; -use std::{future::Future, pin::pin}; +use std::{future::Future, pin::pin, sync::mpsc, time::Duration}; use tracing::{debug, error, trace}; /// Executes CLI commands. @@ -52,17 +52,26 @@ impl CliRunner { // 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)); + task_manager.graceful_shutdown_with_timeout(Duration::from_secs(5)); } - // 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 - // the current thread but we want to exit right away. + // `drop(tokio_runtime)` would block the current thread until its pools + // (including blocking pool) are shutdown. Since we want to exit as soon as possible, drop + // it on a separate thread and wait for up to 5 seconds for this operation to + // complete. + let (tx, rx) = mpsc::channel(); std::thread::Builder::new() .name("tokio-runtime-shutdown".to_string()) - .spawn(move || drop(tokio_runtime)) + .spawn(move || { + drop(tokio_runtime); + let _ = tx.send(()); + }) .unwrap(); + let _ = rx.recv_timeout(Duration::from_secs(5)).inspect_err(|err| { + debug!(target: "reth::cli", %err, "tokio runtime shutdown timed out"); + }); + command_res }