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

Stop NetworkService when NetworkController is dropped #4175

Closed
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
21 changes: 21 additions & 0 deletions network/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1165,6 +1178,7 @@ impl NetworkService {
network_state,
p2p_control,
ping_controller,
server_canceller,
})
}
}
Expand All @@ -1176,6 +1190,13 @@ pub struct NetworkController {
network_state: Arc<NetworkState>,
p2p_control: ServiceControl,
ping_controller: Option<Sender<()>>,
server_canceller: CancellationToken,
}

impl Drop for NetworkController {
fn drop(&mut self) {
self.server_canceller.cancel();
}
}

impl NetworkController {
Expand Down
4 changes: 2 additions & 2 deletions util/stop-handler/src/stop_register.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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")
}
Expand Down
Loading