Skip to main content

zebrad/components/
tokio.rs

1//! A component owning the Tokio runtime.
2//!
3//! The tokio runtime is used for:
4//! - non-blocking async tasks, via [`Future`]s and
5//! - blocking network and file tasks, via [`spawn_blocking`](tokio::task::spawn_blocking).
6//!
7//! The rayon thread pool is used for:
8//! - long-running CPU-bound tasks like cryptography, via [`rayon::spawn_fifo`].
9
10#![allow(non_local_definitions)]
11
12use std::{future::Future, time::Duration};
13
14use abscissa_core::{Component, FrameworkError, Shutdown};
15use color_eyre::Report;
16use tokio::runtime::Runtime;
17
18use crate::prelude::*;
19
20/// When Zebra is shutting down, wait this long for tokio tasks to finish.
21const TOKIO_SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(20);
22
23/// An Abscissa component which owns a Tokio runtime.
24///
25/// The runtime is stored as an `Option` so that when it's time to enter an async
26/// context by calling `block_on` with a "root future", the runtime can be taken
27/// independently of Abscissa's component locking system. Otherwise whatever
28/// calls `block_on` holds an application lock for the entire lifetime of the
29/// async context.
30#[derive(Component, Debug)]
31pub struct TokioComponent {
32    pub rt: Option<Runtime>,
33}
34
35impl TokioComponent {
36    #[allow(clippy::unwrap_in_result)]
37    pub fn new() -> Result<Self, FrameworkError> {
38        Ok(Self {
39            rt: Some(
40                tokio::runtime::Builder::new_multi_thread()
41                    .enable_all()
42                    .build()
43                    .expect("runtime building should not fail"),
44            ),
45        })
46    }
47}
48
49/// Zebrad's graceful shutdown function, blocks until one of the supported
50/// shutdown signals is received.
51async fn shutdown() {
52    imp::shutdown().await;
53}
54
55/// Extension trait to centralize entry point for runnable subcommands that
56/// depend on tokio
57pub(crate) trait RuntimeRun {
58    fn run(self, fut: impl Future<Output = Result<(), Report>>);
59}
60
61impl RuntimeRun for Runtime {
62    fn run(self, fut: impl Future<Output = Result<(), Report>>) {
63        let result = self.block_on(async move {
64            // Always poll the shutdown future first.
65            //
66            // Otherwise, a busy Zebra instance could starve the shutdown future,
67            // and delay shutting down.
68            tokio::select! {
69                biased;
70                _ = shutdown() => Ok(()),
71                result = fut => result,
72            }
73        });
74
75        // Don't wait for long blocking tasks before shutting down
76        info!(
77            ?TOKIO_SHUTDOWN_TIMEOUT,
78            "waiting for async tokio tasks to shut down"
79        );
80        self.shutdown_timeout(TOKIO_SHUTDOWN_TIMEOUT);
81
82        // On SIGINT/SIGTERM the select above drops `fut` — including the
83        // zcashd-compat supervisor task — without running its graceful
84        // shutdown path, and on errors `APPLICATION.shutdown` below never
85        // returns. Terminate any abandoned supervised zcashd synchronously,
86        // now that the runtime has shut down. This is a no-op when no
87        // supervised child is running.
88        {
89            let config = APPLICATION.config();
90            if config.zcashd_compat.enabled && config.zcashd_compat.manage_zcashd {
91                crate::components::zcashd_compat::terminate_abandoned_zcashd(
92                    config.zcashd_compat.shutdown_grace_period,
93                );
94            }
95        }
96
97        match result {
98            Ok(()) => {
99                info!("shutting down Zebra");
100            }
101            Err(report) => {
102                // Recording the error as an error, not as text, lets Sentry build exceptions
103                // from its chain and give each cause its own issue.
104                let error: &(dyn std::error::Error + 'static) = report.as_ref();
105                error!(error, ?report, "shutting down Zebra due to an error");
106                APPLICATION.shutdown(Shutdown::Forced);
107            }
108        }
109    }
110}
111
112#[cfg(unix)]
113mod imp {
114    use tokio::signal::unix::{signal, SignalKind};
115
116    pub(super) async fn shutdown() {
117        // If both signals are received, select! chooses one of them at random.
118        tokio::select! {
119            // SIGINT  - Terminal interrupt signal. Typically generated by shells in response to Ctrl-C.
120            _ = sig(SignalKind::interrupt(), "SIGINT") => {}
121            // SIGTERM - Standard shutdown signal used by process launchers.
122            _ = sig(SignalKind::terminate(), "SIGTERM") => {}
123        };
124    }
125
126    #[instrument]
127    async fn sig(kind: SignalKind, name: &'static str) {
128        // Create a Future that completes the first
129        // time the process receives 'sig'.
130        signal(kind)
131            .expect("Failed to register signal handler")
132            .recv()
133            .await;
134
135        zebra_chain::shutdown::set_shutting_down();
136
137        #[cfg(feature = "progress-bar")]
138        howudoin::disable();
139
140        info!(
141            // use target to remove 'imp' from output
142            target: "zebrad::signal",
143            "received {}, starting shutdown",
144            name,
145        );
146    }
147}
148
149#[cfg(not(unix))]
150mod imp {
151
152    pub(super) async fn shutdown() {
153        //  Wait for Ctrl-C in Windows terminals.
154        // (Zebra doesn't support NT Service control messages. Use a service wrapper for long-running instances.)
155        tokio::signal::ctrl_c()
156            .await
157            .expect("listening for ctrl-c signal should never fail");
158
159        zebra_chain::shutdown::set_shutting_down();
160
161        #[cfg(feature = "progress-bar")]
162        howudoin::disable();
163
164        info!(
165            // use target to remove 'imp' from output
166            target: "zebrad::signal",
167            "received Ctrl-C, starting shutdown",
168        );
169    }
170}