File size: 7,363 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use std::{
    future::Future,
    pin::Pin,
    sync::{Arc, Mutex, OnceLock},
};

use anyhow::Result;
use tokio::{select, sync::mpsc, task::JoinSet};

/// A guard for the exit handler. When dropped, the exit guard will be dropped.
/// It might also be dropped on Ctrl-C.
pub struct ExitGuard<T>(Arc<Mutex<Option<T>>>);

impl<T> Drop for ExitGuard<T> {
    fn drop(&mut self) {
        drop(self.0.lock().unwrap().take())
    }
}

impl<T: Send + 'static> ExitGuard<T> {
    /// Drop a guard when Ctrl-C is pressed or the [ExitGuard] is dropped.
    pub fn new(guard: T) -> Result<Self> {
        let guard = Arc::new(Mutex::new(Some(guard)));
        {
            let guard = guard.clone();
            tokio::spawn(async move {
                tokio::signal::ctrl_c().await.unwrap();
                drop(guard.lock().unwrap().take());
                std::process::exit(0);
            });
        }
        Ok(ExitGuard(guard))
    }
}

type BoxExitFuture = Pin<Box<dyn Future<Output = ()> + Send + 'static>>;

/// The singular global ExitHandler. This is primarily used to ensure
/// `ExitHandler::listen` is only called once.
///
/// The global handler is intentionally not exposed, so that APIs that depend on
/// exit behavior are required to take the `ExitHandler`. This ensures that the
/// `ExitHandler` is configured before these APIs are run, and that these
/// consumers can be used with a callback (e.g. a mock) instead.
static GLOBAL_EXIT_HANDLER: OnceLock<Arc<ExitHandler>> = OnceLock::new();

pub struct ExitHandler {
    tx: mpsc::UnboundedSender<BoxExitFuture>,
}

impl ExitHandler {
    /// Waits for `SIGINT` using [`tokio::signal::ctrl_c`], and exits the
    /// process with exit code `0` after running any futures scheduled with
    /// [`ExitHandler::on_exit`].
    ///
    /// As this uses global process signals, this must only be called once, and
    /// will panic if called multiple times. Use this when you own the
    /// process (e.g. `turbopack-cli`).
    ///
    /// If you don't own the process (e.g. you're called as a library, such as
    /// in `next-swc`), use [`ExitHandler::new_trigger`] instead.
    ///
    /// This may listen for other signals, like `SIGTERM` or `SIGPIPE` in the
    /// future.
    pub fn listen() -> &'static Arc<ExitHandler> {
        let (handler, receiver) = Self::new_receiver();
        if GLOBAL_EXIT_HANDLER.set(handler).is_err() {
            panic!("ExitHandler::listen must only be called once");
        }
        tokio::spawn(async move {
            tokio::signal::ctrl_c()
                .await
                .expect("failed to set ctrl_c handler");
            receiver.run_exit_handler().await;
            std::process::exit(0);
        });
        GLOBAL_EXIT_HANDLER.get().expect("value is set")
    }

    /// Creates an [`ExitHandler`] that can be manually controlled with an
    /// [`ExitReceiver`].
    ///
    /// This does not actually exit the process or listen for any signals. If
    /// you'd like that behavior, use [`ExitHandler::listen`].
    ///
    /// Because this API has no global side-effects and can be called many times
    /// within the same process, it is possible to use it to provide a mock
    /// [`ExitHandler`] inside unit tests.
    pub fn new_receiver() -> (Arc<ExitHandler>, ExitReceiver) {
        let (tx, rx) = mpsc::unbounded_channel();
        (Arc::new(ExitHandler { tx }), ExitReceiver { rx })
    }

    /// Register this given [`Future`] to run upon process exit.
    ///
    /// As there are many ways for a process be killed that are outside of a
    /// process's own control (e.g. `SIGKILL` or `SIGSEGV`), this API is
    /// provided on a best-effort basis.
    pub fn on_exit(&self, fut: impl Future<Output = ()> + Send + 'static) {
        // realistically, this error case can only happen with the `new_receiver` API.
        self.tx
            .send(Box::pin(fut))
            .expect("cannot send future after process exit");
    }
}

/// Provides a way to run futures scheduled with an [`ExitHandler`].
pub struct ExitReceiver {
    rx: mpsc::UnboundedReceiver<BoxExitFuture>,
}

impl ExitReceiver {
    /// Call this when the process exits to run the futures scheduled via
    /// [`ExitHandler::on_exit`].
    ///
    /// As this is intended to be used in a library context, this does not exit
    /// the process. It is expected that the process will not exit until
    /// this async method finishes executing.
    ///
    /// Additional work can be scheduled using [`ExitHandler::on_exit`] even
    /// while this is running, and it will execute before this function
    /// finishes. Work attempted to be scheduled after this finishes will panic.
    pub async fn run_exit_handler(mut self) {
        let mut set = JoinSet::new();
        while let Ok(fut) = self.rx.try_recv() {
            set.spawn(fut);
        }
        loop {
            select! {
                biased;
                Some(fut) = self.rx.recv() => {
                    set.spawn(fut);
                },
                val = set.join_next() => {
                    match val {
                        Some(Ok(())) => {}
                        Some(Err(_)) => panic!("ExitHandler future panicked!"),
                        None => return,
                    }
                },
            }
        }
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this
    use std::{
        future::Future,
        pin::Pin,
        sync::{
            Arc,
            atomic::{AtomicBool, AtomicU32, Ordering},
        },
    };

    use super::ExitHandler;

    #[tokio::test]
    async fn test_on_exit() {
        let (handler, receiver) = ExitHandler::new_receiver();

        let called = Arc::new(AtomicBool::new(false));
        handler.on_exit({
            let called = Arc::clone(&called);
            async move {
                tokio::task::yield_now().await;
                called.store(true, Ordering::SeqCst);
            }
        });

        receiver.run_exit_handler().await;
        assert!(called.load(Ordering::SeqCst));
    }

    #[tokio::test]
    async fn test_queue_while_exiting() {
        let (handler, receiver) = ExitHandler::new_receiver();
        let call_count = Arc::new(AtomicU32::new(0));

        type BoxExitFuture = Pin<Box<dyn Future<Output = ()> + Send + 'static>>;

        // this struct is needed to construct the recursive closure type
        #[derive(Clone)]
        struct GetFut {
            handler: Arc<ExitHandler>,
            call_count: Arc<AtomicU32>,
        }

        impl GetFut {
            fn get(self) -> BoxExitFuture {
                Box::pin(async move {
                    tokio::task::yield_now().await;
                    if self.call_count.fetch_add(1, Ordering::SeqCst) < 99 {
                        // queue more work while the exit handler is running
                        Arc::clone(&self.handler).on_exit(self.get())
                    }
                })
            }
        }

        handler.on_exit(
            GetFut {
                handler: Arc::clone(&handler),
                call_count: Arc::clone(&call_count),
            }
            .get(),
        );
        receiver.run_exit_handler().await;
        assert_eq!(call_count.load(Ordering::SeqCst), 100);
    }
}