shutdown-handler 0.1.1

A shutdown handler that allows all parts of an application to trigger a shutdown.
Documentation
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! A graceful shutdown handler that allows all parts of an application to trigger a shutdown.
//!
//! # Why?
//!
//! An application I was maintaining was in charge of 3 different services.
//! * A RabbitMQ processing service
//! * A gRPC Server
//! * An HTTP metrics server.
//!
//! Our RabbitMQ node was restarted, so our connections dropped and our service went into shutdown mode.
//! However, due to a bug in our application layer, we didn't acknowledge the failure immediately and
//! continued handling the gRPC and HTTP traffic. Thankfully our alerts triggered that the queue was backing up
//! and we manually restarted the application without any real impact.
//!
//! Understandably, I wanted a way to not have this happen ever again. We fixed the bug in the application, and then
//! tackled the root cause: **Other services were oblivious that a shutdown happened**.
//!
//! Using this library, we've enforced that all service libraries take in a `ShutdownHandler` instance and use it to gracefully
//! shutdown. If any of them are about to crash, they will immediately raise a shutdown signal. The other services
//! will then see that signal, finish whatever work they had started, then shutdown.
//!
//! # Example
//!
//! ```
//! use std::pin::pin;
//! use std::sync::Arc;
//! use shutdown_handler::{ShutdownHandler, SignalOrComplete};
//!
//! # #[tokio::main] async fn main() {
//! // Create the shutdown handler
//! let shutdown = Arc::new(ShutdownHandler::new());
//!
//! // Shutdown on SIGTERM
//! shutdown.spawn_sigterm_handler().unwrap();
//!
//! // Spawn a few service workers
//! let mut workers = tokio::task::JoinSet::new();
//! for port in 0..4 {
//!     workers.spawn(service(Arc::clone(&shutdown), port));
//! }
//!
//! // await all workers and collect the errors
//! let mut errors = vec![];
//! while let Some(result) = workers.join_next().await {
//!     // unwrap any JoinErrors that happen if the tokio task panicked
//!     let result = result.unwrap();
//!
//!     // did our service error?
//!     if let Err(e) = result {
//!         errors.push(e);
//!     }
//! }
//!
//! assert_eq!(errors, ["port closed"]);
//! # }
//!
//! // Define our services to loop on work and shutdown gracefully
//!
//! async fn service(shutdown: Arc<ShutdownHandler>, port: u16) -> Result<(), &'static str> {
//!     // a work loop that handles events
//!     for request in 0.. {
//!         let handle = pin!(handle_request(port, request));
//!
//!         match shutdown.wait_for_signal_or_future(handle).await {
//!             // We finished handling the request without any interuptions. Continue
//!             SignalOrComplete::Completed(Ok(_)) => {}
//!
//!             // There was an error handling the request, let's shutdown
//!             SignalOrComplete::Completed(Err(e)) => {
//!                 shutdown.shutdown();
//!                 return Err(e);
//!             }
//!
//!             // There was a shutdown signal raised while handling this request
//!             SignalOrComplete::ShutdownSignal(handle) => {
//!                 // We will finish handling the request but then exit
//!                 return handle.await;
//!             }
//!         }
//!     }
//!     Ok(())
//! }
//!
//! async fn handle_request(port: u16, request: usize) -> Result<(), &'static str> {
//!     // simulate some work being done
//!     tokio::time::sleep(std::time::Duration::from_millis(10)).await;
//!     
//!     // simulate an error
//!     if port == 3 && request > 12 {
//!         Err("port closed")
//!     } else {
//!         Ok(())
//!     }
//! }
//! ```

use pin_project_lite::pin_project;
use std::{
    future::Future,
    pin::{pin, Pin},
    sync::{atomic::AtomicBool, Arc},
    task::Poll,
};
use tokio::{
    signal::unix::{signal, SignalKind},
    sync::{futures::Notified, Notify},
};

/// A graceful shutdown handler that allows all parts of an application to trigger a shutdown.
///
/// # Example
/// ```
/// use std::pin::pin;
/// use std::sync::Arc;
/// use shutdown_handler::{ShutdownHandler, SignalOrComplete};
///
/// # #[tokio::main] async fn main() {
/// // Create the shutdown handler
/// let shutdown = Arc::new(ShutdownHandler::new());
///
/// // Shutdown on SIGTERM
/// shutdown.spawn_sigterm_handler().unwrap();
///
/// // Spawn a few service workers
/// let mut workers = tokio::task::JoinSet::new();
/// for port in 0..4 {
///     workers.spawn(service(Arc::clone(&shutdown), port));
/// }
///
/// // await all workers and collect the errors
/// let mut errors = vec![];
/// while let Some(result) = workers.join_next().await {
///     // unwrap any JoinErrors that happen if the tokio task panicked
///     let result = result.unwrap();
///
///     // did our service error?
///     if let Err(e) = result {
///         errors.push(e);
///     }
/// }
///
/// assert_eq!(errors, ["port closed"]);
/// # }
///
/// // Define our services to loop on work and shutdown gracefully
///
/// async fn service(shutdown: Arc<ShutdownHandler>, port: u16) -> Result<(), &'static str> {
///     // a work loop that handles events
///     for request in 0.. {
///         let handle = pin!(handle_request(port, request));
///
///         match shutdown.wait_for_signal_or_future(handle).await {
///             // We finished handling the request without any interuptions. Continue
///             SignalOrComplete::Completed(Ok(_)) => {}
///
///             // There was an error handling the request, let's shutdown
///             SignalOrComplete::Completed(Err(e)) => {
///                 shutdown.shutdown();
///                 return Err(e);
///             }
///
///             // There was a shutdown signal raised while handling this request
///             SignalOrComplete::ShutdownSignal(handle) => {
///                 // We will finish handling the request but then exit
///                 return handle.await;
///             }
///         }
///     }
///     Ok(())
/// }
///
/// async fn handle_request(port: u16, request: usize) -> Result<(), &'static str> {
///     // simulate some work being done
///     tokio::time::sleep(std::time::Duration::from_millis(10)).await;
///     
///     // simulate an error
///     if port == 3 && request > 12 {
///         Err("port closed")
///     } else {
///         Ok(())
///     }
/// }
/// ```
#[derive(Debug, Default)]
pub struct ShutdownHandler {
    notifier: Notify,
    shutdown: AtomicBool,
}

impl ShutdownHandler {
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a new `ShutdownHandler` and registers the sigterm handler
    pub fn sigterm() -> std::io::Result<Arc<Self>> {
        let this = Arc::new(Self::new());
        this.spawn_sigterm_handler()?;
        Ok(this)
    }

    /// Registers the signal event `SIGTERM` to trigger an application shutdown
    pub fn spawn_sigterm_handler(self: &Arc<Self>) -> std::io::Result<()> {
        self.spawn_signal_handler(SignalKind::terminate())
    }

    /// Registers a signal event to trigger an application shutdown
    pub fn spawn_signal_handler(self: &Arc<Self>, signal_kind: SignalKind) -> std::io::Result<()> {
        let mut signal = signal(signal_kind)?;

        let shutdown = self.clone();
        tokio::spawn(async move {
            signal.recv().await;
            shutdown.shutdown();
        });
        Ok(())
    }

    /// Sends the shutdown signal to all the current and future waiters
    pub fn shutdown(&self) {
        self.shutdown
            .store(true, std::sync::atomic::Ordering::Release);
        self.notifier.notify_waiters();
    }

    /// Returns a future that waits for the shutdown signal.
    ///
    /// You can use this like an async function.
    pub fn wait_for_signal(&self) -> ShutdownSignal<'_> {
        ShutdownSignal {
            shutdown: &self.shutdown,
            notified: self.notifier.notified(),
        }
    }

    /// This method will try to complete the given future, but will give up if the shutdown signal is raised.
    /// The unfinished future is returned in case it is not cancel safe and you need to complete it
    ///
    /// ```
    /// use std::sync::Arc;
    /// use std::pin::pin;
    /// use shutdown_handler::{ShutdownHandler, SignalOrComplete};
    ///
    /// # #[tokio::main] async fn main() {
    /// async fn important_work() -> i32 {
    ///     tokio::time::sleep(std::time::Duration::from_secs(2)).await;
    ///     42
    /// }
    ///
    /// let shutdown = Arc::new(ShutdownHandler::new());
    ///
    /// // another part of the application signals a shutdown
    /// let shutdown2 = Arc::clone(&shutdown);
    /// let handle = tokio::spawn(async move {
    ///     tokio::time::sleep(std::time::Duration::from_secs(1)).await;
    ///     shutdown2.shutdown();
    /// });
    ///
    /// let work = pin!(important_work());
    ///
    /// match shutdown.wait_for_signal_or_future(work).await {
    ///     SignalOrComplete::Completed(res) => println!("important work completed without interuption: {res}"),
    ///     SignalOrComplete::ShutdownSignal(work) => {
    ///         println!("shutdown signal recieved");
    ///         let res = work.await;
    ///         println!("important work completed: {res}");
    ///     },
    /// }
    /// # }
    /// ```
    pub async fn wait_for_signal_or_future<F: Future + Unpin>(&self, f: F) -> SignalOrComplete<F> {
        let mut handle = pin!(self.wait_for_signal());
        let mut f = Some(f);

        std::future::poll_fn(|cx| {
            if let Poll::Ready(_signal) = handle.as_mut().poll(cx) {
                return Poll::Ready(SignalOrComplete::ShutdownSignal(f.take().unwrap()));
            }

            if let Poll::Ready(res) = Pin::new(f.as_mut().unwrap()).poll(cx) {
                return Poll::Ready(SignalOrComplete::Completed(res));
            }

            Poll::Pending
        })
        .await
    }
}

#[derive(Debug)]
/// Reports whether a future managed to complete without interuption, or if there was a shutdown signal
pub enum SignalOrComplete<F: Future> {
    ShutdownSignal(F),
    Completed(F::Output),
}

pin_project!(
    /// A Future that waits for a shutdown signal. Returned by [`ShutdownHandler::shutdown`]
    pub struct ShutdownSignal<'a> {
        shutdown: &'a AtomicBool,
        #[pin]
        notified: Notified<'a>,
    }
);

impl std::future::Future for ShutdownSignal<'_> {
    type Output = ();

    fn poll(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Self::Output> {
        let this = self.project();
        if this.shutdown.load(std::sync::atomic::Ordering::Acquire) {
            std::task::Poll::Ready(())
        } else {
            this.notified.poll(cx)
        }
    }
}

#[cfg(test)]
mod test {
    use std::{sync::Arc, time::Duration};

    use nix::sys::signal::{raise, Signal};
    use tokio::{signal::unix::SignalKind, sync::oneshot, time::timeout};

    use crate::ShutdownHandler;

    #[tokio::test]
    async fn shutdown_sigterm() {
        let shutdown = Arc::new(ShutdownHandler::new());
        shutdown.spawn_sigterm_handler().unwrap();

        let (tx, rx) = oneshot::channel();
        tokio::spawn(async move {
            shutdown.wait_for_signal().await;
            tx.send(true).unwrap();
        });

        raise(Signal::SIGTERM).unwrap();

        assert!(
            (timeout(Duration::from_secs(1), rx).await).is_ok(),
            "Shutdown handler took longer than 1 second!"
        );
    }

    #[tokio::test]
    async fn shutdown_custom_signal() {
        let shutdown = Arc::new(ShutdownHandler::new());
        shutdown.spawn_signal_handler(SignalKind::hangup()).unwrap();

        let (tx, rx) = oneshot::channel();
        tokio::spawn(async move {
            shutdown.wait_for_signal().await;
            tx.send(true).unwrap();
        });

        raise(Signal::SIGHUP).unwrap();

        assert!(
            (timeout(Duration::from_secs(1), rx).await).is_ok(),
            "Shutdown handler took longer than 1 second!"
        );
    }

    #[tokio::test]
    async fn shutdown() {
        let shutdown = Arc::new(ShutdownHandler::new());

        let (tx, rx) = oneshot::channel();
        let channel_shutdown = shutdown.clone();
        tokio::spawn(async move {
            channel_shutdown.wait_for_signal().await;
            tx.send(true).unwrap();
        });

        tokio::spawn(async move {
            shutdown.shutdown();
        });

        assert!(
            (timeout(Duration::from_secs(1), rx).await).is_ok(),
            "Shutdown handler took longer than 1 second!"
        );
    }

    #[tokio::test]
    async fn no_notification() {
        let shutdown = Arc::new(ShutdownHandler::new());

        let (tx, rx) = oneshot::channel();
        tokio::spawn(async move {
            shutdown.wait_for_signal().await;
            tx.send(true).unwrap();
        });

        assert!(
            (timeout(Duration::from_secs(1), rx).await).is_err(),
            "Shutdown handler ran without a signal!"
        );
    }
}