From b5e80fbe4efe97d25549f2369253d1b9647de252 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 22 Aug 2023 20:59:23 +0000 Subject: [PATCH] Dont block tokio reactor waiting on user input When we moved the CLI input reading into a `tokio::select`, we ended up blocking the tokio reactor by reading from stdin in a standard tokio future. Instead, we need to let tokio know that we're running a largely-blocking task. --- src/cli.rs | 18 +++++++++++++----- src/main.rs | 32 +++++++++++++++++++------------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 10dce92..75b7967 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -60,7 +60,7 @@ impl Writeable for UserOnionMessageContents { } } -pub(crate) async fn poll_for_user_input( +pub(crate) fn poll_for_user_input( peer_manager: Arc, channel_manager: Arc, keys_manager: Arc, network_graph: Arc, onion_messenger: Arc, inbound_payments: Arc>, @@ -112,8 +112,12 @@ pub(crate) async fn poll_for_user_input( continue; } - if connect_peer_if_necessary(pubkey, peer_addr, peer_manager.clone()) - .await + if tokio::runtime::Handle::current() + .block_on(connect_peer_if_necessary( + pubkey, + peer_addr, + peer_manager.clone(), + )) .is_err() { continue; @@ -259,8 +263,12 @@ pub(crate) async fn poll_for_user_input( continue; } }; - if connect_peer_if_necessary(pubkey, peer_addr, peer_manager.clone()) - .await + if tokio::runtime::Handle::current() + .block_on(connect_peer_if_necessary( + pubkey, + peer_addr, + peer_manager.clone(), + )) .is_ok() { println!("SUCCESS: connected to peer {}", pubkey); diff --git a/src/main.rs b/src/main.rs index d438682..40240a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -943,19 +943,25 @@ async fn start_ldk() { )); // Start the CLI. - let cli_poll = tokio::spawn(cli::poll_for_user_input( - Arc::clone(&peer_manager), - Arc::clone(&channel_manager), - Arc::clone(&keys_manager), - Arc::clone(&network_graph), - Arc::clone(&onion_messenger), - inbound_payments, - outbound_payments, - ldk_data_dir, - network, - Arc::clone(&logger), - Arc::clone(&persister), - )); + let cli_channel_manager = Arc::clone(&channel_manager); + let cli_persister = Arc::clone(&persister); + let cli_logger = Arc::clone(&logger); + let cli_peer_manager = Arc::clone(&peer_manager); + let cli_poll = tokio::task::spawn_blocking(move || { + cli::poll_for_user_input( + cli_peer_manager, + cli_channel_manager, + keys_manager, + network_graph, + onion_messenger, + inbound_payments, + outbound_payments, + ldk_data_dir, + network, + cli_logger, + cli_persister, + ) + }); // Exit if either CLI polling exits or the background processor exits (which shouldn't happen // unless we fail to write to the filesystem). -- 2.39.5