Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: also gracefully shutdown on SIGTERM #17802

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/utils/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod panic_hook;
pub use panic_hook::*;
mod prof;
use prof::*;
use tokio::signal::unix::SignalKind;

/// Start RisingWave components with configs from environment variable.
///
Expand Down Expand Up @@ -100,20 +101,22 @@ where
let shutdown = CancellationToken::new();
let mut fut = pin!(f(shutdown.clone()));

let mut sigint = tokio::signal::unix::signal(SignalKind::interrupt()).unwrap();
let mut sigterm = tokio::signal::unix::signal(SignalKind::terminate()).unwrap();

tokio::select! {
biased;
result = tokio::signal::ctrl_c() => {
result.expect("failed to receive ctrl-c signal");
tracing::info!("received ctrl-c, shutting down... (press ctrl-c again to force shutdown)");

// Send shutdown signal.
// Watch SIGINT, typically originating from user pressing ctrl-c.
// Attempt to shutdown gracefully and force shutdown on the next signal.
_ = sigint.recv() => {
tracing::info!("received ctrl-c, shutting down... (press ctrl-c again to force shutdown)");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

shutdown.cancel();

// While waiting for the future to finish, listen for the second ctrl-c signal.
tokio::select! {
biased;
result = tokio::signal::ctrl_c() => {
result.expect("failed to receive ctrl-c signal");
_ = sigint.recv() => {
tracing::warn!("forced shutdown");

// Directly exit the process **here** instead of returning from the future, since
Expand All @@ -123,6 +126,16 @@ where
_ = &mut fut => {},
}
}

// Watch SIGTERM, typically originating from Kubernetes.
// Attempt to shutdown gracefully. No need to force shutdown since it will send SIGKILL after a timeout.
_ = sigterm.recv() => {
tracing::info!("received SIGTERM, shutting down...");
shutdown.cancel();
fut.await;
}

// Proceed with the future.
_ = &mut fut => {},
}
};
Expand Down
Loading