Skip to content

Commit 93f67fc

Browse files
committed
wrap token in a handle and expand service error handling
1 parent d8b27a2 commit 93f67fc

13 files changed

Lines changed: 158 additions & 122 deletions

File tree

binaries/cuprated/src/blockchain/manager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub async fn init_blockchain_manager(
7272
Arc::clone(&stop_current_block_downloader),
7373
block_downloader_config,
7474
Arc::clone(&synced_notify),
75-
task.cancellation_token.clone(),
75+
task.shutdown_handle.token(),
7676
);
7777
task.spawn_critical(
7878
async move {
@@ -95,7 +95,7 @@ pub async fn init_blockchain_manager(
9595
broadcast_svc: clearnet_interface.broadcast_svc(),
9696
};
9797

98-
let shutdown_token = task.cancellation_token.clone();
98+
let shutdown_token = task.shutdown_handle.token();
9999
task.spawn_critical(manager.run(batch_rx, command_rx, shutdown_token), || {
100100
tracing::info!("Blockchain manager shut down.");
101101
});

binaries/cuprated/src/commands.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::{io, thread::sleep, time::Duration};
55

66
use clap::{builder::TypedValueParser, Parser, ValueEnum};
77
use tokio::sync::mpsc;
8-
use tokio_util::sync::CancellationToken;
98
use tower::{Service, ServiceExt};
109
use tracing::level_filters::LevelFilter;
1110

@@ -17,6 +16,7 @@ use cuprate_helper::time::secs_to_hms;
1716
use crate::{
1817
logging::{self, CupratedTracingFilter},
1918
statics,
19+
supervisor::ShutdownHandle,
2020
};
2121

2222
/// A command received from [`io::stdin`].
@@ -102,12 +102,12 @@ pub fn command_listener(incoming_commands: mpsc::Sender<Command>) {
102102
pub async fn io_loop(
103103
mut incoming_commands: mpsc::Receiver<Command>,
104104
mut context_service: BlockchainContextService,
105-
shutdown_token: CancellationToken,
105+
shutdown_handle: ShutdownHandle,
106106
) {
107107
loop {
108108
let command = tokio::select! {
109109
biased;
110-
() = shutdown_token.cancelled() => break,
110+
() = shutdown_handle.cancelled() => break,
111111
cmd = incoming_commands.recv() => {
112112
let Some(cmd) = cmd else { break };
113113
cmd
@@ -157,7 +157,7 @@ pub async fn io_loop(
157157
}
158158
}
159159
Command::Exit => {
160-
crate::supervisor::trigger_shutdown(&shutdown_token);
160+
shutdown_handle.trigger_shutdown();
161161
break;
162162
}
163163
}

binaries/cuprated/src/main.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ fn main() {
9898

9999
rt.block_on(async move {
100100
// Create the supervisor and task handle, spawn signal handlers.
101-
let (mut supervisor, task) = supervisor::new();
102-
supervisor::spawn_signal_handler(supervisor.cancellation_token.clone());
101+
let (supervisor, task) = supervisor::new();
102+
supervisor::spawn_signal_handler(supervisor.shutdown_handle.clone());
103103

104104
// TODO: Add an argument/option for keeping alt blocks between restart.
105105
blockchain_write_handle
@@ -135,7 +135,7 @@ fn main() {
135135
blockchain_read_handle.clone(),
136136
txpool_read_handle.clone(),
137137
&tor_context,
138-
task.cancellation_token.clone(),
138+
task.shutdown_handle.clone(),
139139
)
140140
.await;
141141

@@ -190,13 +190,13 @@ fn main() {
190190
if tor_enabled {
191191
info!("Tor P2P zone will start after sync.");
192192
let context_svc = context_svc.clone();
193-
let cancellation_token = task.cancellation_token.clone();
193+
let shutdown_handle = task.shutdown_handle.clone();
194194

195195
task.task_tracker.spawn(async move {
196196
// Wait for the node to synchronize with the network, or shutdown.
197197
tokio::select! {
198198
() = synced_notify.notified() => {}
199-
() = cancellation_token.cancelled() => {
199+
() = shutdown_handle.cancelled() => {
200200
return;
201201
}
202202
}
@@ -208,7 +208,7 @@ fn main() {
208208
blockchain_read_handle,
209209
txpool_read_handle,
210210
tor_context,
211-
cancellation_token.clone(),
211+
shutdown_handle.clone(),
212212
)
213213
.await;
214214

@@ -236,13 +236,13 @@ fn main() {
236236
task.task_tracker.spawn(commands::io_loop(
237237
command_rx,
238238
context_svc,
239-
task.cancellation_token.clone(),
239+
task.shutdown_handle.clone(),
240240
));
241241
} else {
242242
info!("Terminal/TTY not detected, disabling STDIN commands");
243243
}
244244
// Wait for shutdown (signal, command, or critical task failure).
245-
supervisor.cancellation_token.cancelled().await;
245+
supervisor.shutdown_handle.cancelled().await;
246246
supervisor.task_tracker.close();
247247
supervisor.task_tracker.wait().await;
248248
});

binaries/cuprated/src/p2p.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use anyhow::anyhow;
88
use futures::{FutureExt, TryFutureExt};
99
use serde::{Deserialize, Serialize};
1010
use tokio::sync::oneshot::{self, Sender};
11-
use tokio_util::sync::CancellationToken;
1211
use tower::{Service, ServiceExt};
1312

1413
use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle};
@@ -24,7 +23,7 @@ use cuprate_types::blockchain::BlockchainWriteRequest;
2423
use crate::{
2524
blockchain,
2625
config::Config,
27-
constants::PANIC_CRITICAL_SERVICE_ERROR,
26+
supervisor::ShutdownHandle,
2827
tor::{transport_clearnet_daemon_config, transport_daemon_config, TorContext, TorMode},
2928
txpool::{self, IncomingTxHandler},
3029
};
@@ -100,7 +99,7 @@ pub async fn initialize_clearnet_p2p(
10099
blockchain_read_handle: BlockchainReadHandle,
101100
txpool_read_handle: TxpoolReadHandle,
102101
tor_ctx: &TorContext,
103-
shutdown_token: CancellationToken,
102+
shutdown_handle: ShutdownHandle,
104103
) -> (NetworkInterface<ClearNet>, Sender<IncomingTxHandler>) {
105104
match config.p2p.clear_net.proxy {
106105
ProxySettings::Tor => match tor_ctx.mode {
@@ -113,7 +112,7 @@ pub async fn initialize_clearnet_p2p(
113112
txpool_read_handle,
114113
config.clearnet_p2p_config(),
115114
transport_clearnet_arti_config(tor_ctx),
116-
shutdown_token,
115+
shutdown_handle,
117116
)
118117
.await
119118
.unwrap()
@@ -124,7 +123,7 @@ pub async fn initialize_clearnet_p2p(
124123
txpool_read_handle,
125124
config.clearnet_p2p_config(),
126125
transport_clearnet_daemon_config(config),
127-
shutdown_token,
126+
shutdown_handle,
128127
)
129128
.await
130129
.unwrap(),
@@ -138,7 +137,7 @@ pub async fn initialize_clearnet_p2p(
138137
txpool_read_handle,
139138
config.clearnet_p2p_config(),
140139
config.p2p.clear_net.tcp_transport_config(config.network),
141-
shutdown_token.clone(),
140+
shutdown_handle.clone(),
142141
)
143142
.await
144143
.unwrap()
@@ -152,7 +151,7 @@ pub async fn initialize_clearnet_p2p(
152151
client_config: socks_proxy_str_to_config(s).unwrap(),
153152
server_config: None,
154153
},
155-
shutdown_token.clone(),
154+
shutdown_handle.clone(),
156155
)
157156
.await
158157
.unwrap()
@@ -169,7 +168,7 @@ pub async fn start_tor_p2p(
169168
blockchain_read_handle: BlockchainReadHandle,
170169
txpool_read_handle: TxpoolReadHandle,
171170
tor_ctx: TorContext,
172-
shutdown_token: CancellationToken,
171+
shutdown_handle: ShutdownHandle,
173172
) -> (NetworkInterface<Tor>, Sender<IncomingTxHandler>) {
174173
match tor_ctx.mode {
175174
TorMode::Daemon => start_zone_p2p::<Tor, Daemon>(
@@ -178,7 +177,7 @@ pub async fn start_tor_p2p(
178177
txpool_read_handle,
179178
config.tor_p2p_config(&tor_ctx),
180179
transport_daemon_config(config),
181-
shutdown_token,
180+
shutdown_handle,
182181
)
183182
.await
184183
.unwrap(),
@@ -189,7 +188,7 @@ pub async fn start_tor_p2p(
189188
txpool_read_handle,
190189
config.tor_p2p_config(&tor_ctx),
191190
transport_arti_config(config, tor_ctx),
192-
shutdown_token,
191+
shutdown_handle,
193192
)
194193
.await
195194
.unwrap(),
@@ -207,7 +206,7 @@ pub async fn start_zone_p2p<N, T>(
207206
txpool_read_handle: TxpoolReadHandle,
208207
config: P2PConfig<N>,
209208
transport_config: TransportConfig<N, T>,
210-
shutdown_token: CancellationToken,
209+
shutdown_handle: ShutdownHandle,
211210
) -> Result<(NetworkInterface<N>, Sender<IncomingTxHandler>), tower::BoxError>
212211
where
213212
N: NetworkZone,
@@ -221,7 +220,7 @@ where
221220
blockchain_read_handle,
222221
blockchain_context_service: blockchain_context_service.clone(),
223222
txpool_read_handle,
224-
shutdown_token,
223+
shutdown_handle,
225224
incoming_tx_handler: None,
226225
incoming_tx_handler_fut: incoming_tx_handler_rx.shared(),
227226
};

0 commit comments

Comments
 (0)