From 53e9f0378c256158326961b3732ae9ef04ba83cb Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Thu, 5 Oct 2023 18:58:21 +0800 Subject: [PATCH 1/2] Don't print error message when ckb received stop signal multiple times --- util/stop-handler/src/stop_register.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/stop-handler/src/stop_register.rs b/util/stop-handler/src/stop_register.rs index d7f2a7f560..d242fc6034 100644 --- a/util/stop-handler/src/stop_register.rs +++ b/util/stop-handler/src/stop_register.rs @@ -61,7 +61,7 @@ pub fn broadcast_exit_signals() { .iter() .for_each(|tx| match tx.try_send(()) { Ok(_) => {} - Err(TrySendError::Full(_)) => error!("send exit signal to channel failed since the channel is full, this should not happen"), + Err(TrySendError::Full(_)) => debug!("ckb process has received stop signal already"), Err(TrySendError::Disconnected(_)) => { info!("broadcast thread: channel is disconnected") } From bee38a23380e299f4c8d05bdfdb5025f0c54887c Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Sat, 7 Oct 2023 10:30:59 +0800 Subject: [PATCH 2/2] Add drop_guard to NetworkController Signed-off-by: Eval EXEC --- network/src/network.rs | 21 +++++++++++++++++++++ util/stop-handler/src/stop_register.rs | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/network/src/network.rs b/network/src/network.rs index d7ce369897..d198450dd8 100644 --- a/network/src/network.rs +++ b/network/src/network.rs @@ -1095,8 +1095,12 @@ impl NetworkService { .unzip(); let receiver: CancellationToken = new_tokio_exit_rx(); + + let server_canceller: CancellationToken = CancellationToken::new(); + let (start_sender, start_receiver) = mpsc::channel(); { + let server_canceller_clone = server_canceller.clone(); let network_state = Arc::clone(&network_state); let p2p_control: ServiceAsyncControl = p2p_control.clone().into(); handle.spawn_task(async move { @@ -1134,6 +1138,15 @@ impl NetworkService { break; }, + _ = server_canceller_clone.cancelled() => { + + debug!("NetworkController dropped, start shutdown..."); + let _ = p2p_control.shutdown().await; + // Drop senders to stop all corresponding background task + drop(bg_signals); + + break; + }, else => { let _ = p2p_control.shutdown().await; // Drop senders to stop all corresponding background task @@ -1165,6 +1178,7 @@ impl NetworkService { network_state, p2p_control, ping_controller, + server_canceller, }) } } @@ -1176,6 +1190,13 @@ pub struct NetworkController { network_state: Arc, p2p_control: ServiceControl, ping_controller: Option>, + server_canceller: CancellationToken, +} + +impl Drop for NetworkController { + fn drop(&mut self) { + self.server_canceller.cancel(); + } } impl NetworkController { diff --git a/util/stop-handler/src/stop_register.rs b/util/stop-handler/src/stop_register.rs index d242fc6034..e122f4b8e0 100644 --- a/util/stop-handler/src/stop_register.rs +++ b/util/stop-handler/src/stop_register.rs @@ -1,5 +1,5 @@ use ckb_channel::TrySendError; -use ckb_logger::{debug, error, info, trace, warn}; +use ckb_logger::{debug, info, trace, warn}; use ckb_util::Mutex; use tokio_util::sync::CancellationToken;