Skip to content

Commit

Permalink
React to more shutdown signals (#77)
Browse files Browse the repository at this point in the history
* React to more shutdown signals on windows
  • Loading branch information
Finomnis authored Dec 2, 2023
1 parent 8645740 commit 3ac73fe
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ jobs:
run: cargo minimal-versions test -- --test-threads 1

min-versions-msrv:
name: Minimal Depencency Versions (MSRV)
name: Minimal Dependency Versions (MSRV)
runs-on: ubuntu-latest
needs: [lints, docs]
env:
Expand Down
24 changes: 18 additions & 6 deletions src/signal_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
async fn wait_for_signal_impl() {
use tokio::signal::unix::{signal, SignalKind};

// Infos here:
// https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html
let mut signal_terminate = signal(SignalKind::terminate()).unwrap();
let mut signal_interrupt = signal(SignalKind::interrupt()).unwrap();

Expand All @@ -15,15 +17,25 @@ async fn wait_for_signal_impl() {
/// Waits for a signal that requests a graceful shutdown, Ctrl-C (SIGINT).
#[cfg(windows)]
async fn wait_for_signal_impl() {
use tokio::signal::ctrl_c;
use tokio::signal::windows;

ctrl_c().await.unwrap();
tracing::debug!("Received SIGINT.");
// Infos here:
// https://learn.microsoft.com/en-us/windows/console/handlerroutine
let mut signal_c = windows::ctrl_c().unwrap();
let mut signal_break = windows::ctrl_break().unwrap();
let mut signal_close = windows::ctrl_close().unwrap();
let mut signal_shutdown = windows::ctrl_shutdown().unwrap();

tokio::select! {
_ = signal_c.recv() => tracing::debug!("Received CTRL_C."),
_ = signal_break.recv() => tracing::debug!("Received CTRL_BREAK."),
_ = signal_close.recv() => tracing::debug!("Received CTRL_CLOSE."),
_ = signal_shutdown.recv() => tracing::debug!("Received CTRL_SHUTDOWN."),
};
}

/// Registers Ctrl+C and SIGTERM handlers to cause a program shutdown.
/// Further, registers a custom panic handler to also initiate a shutdown.
/// Otherwise, a multi-threaded system would deadlock on panik.
/// Registers signal handlers and waits for a signal that
/// indicates a shutdown request.
pub(crate) async fn wait_for_signal() {
wait_for_signal_impl().await
}
8 changes: 6 additions & 2 deletions src/toplevel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,14 @@ impl<ErrType: ErrTypeTraits> Toplevel<ErrType> {
/// The following signals will be handled:
///
/// - On Windows:
/// - Ctrl+C (SIGINT)
/// - `CTRL_C`
/// - `CTRL_BREAK`
/// - `CTRL_CLOSE`
/// - `CTRL_SHUTDOWN`
///
/// - On Unix:
/// - SIGINT and SIGTERM
/// - `SIGINT`
/// - `SIGTERM`
///
/// # Caveats
///
Expand Down

0 comments on commit 3ac73fe

Please sign in to comment.