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

Fix Windows server shutdown #425

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
26 changes: 23 additions & 3 deletions pingora-core/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use pingora_timeout::fast_timeout;
use sentry::ClientOptions;
use std::sync::Arc;
use std::thread;
#[cfg(windows)]
use tokio::signal;
#[cfg(unix)]
use tokio::signal::unix;
use tokio::sync::{watch, Mutex};
Expand Down Expand Up @@ -152,6 +154,27 @@ impl Server {
}
}

#[cfg(windows)]
async fn main_loop(&self) -> ShutdownType {
let mut graceful_terminate_signal = signal::windows::ctrl_c().unwrap();
tokio::select! {
_ = graceful_terminate_signal.recv() => {
// we receive a graceful terminate, all instances are instructed to stop
info!("CTRL-C received, gracefully exiting");
// graceful shutdown if there are listening sockets
info!("Broadcasting graceful shutdown");
match self.shutdown_watch.send(true) {
Ok(_) => { info!("Graceful shutdown started!"); }
Err(e) => {
error!("Graceful shutdown broadcast failed: {e}");
}
}
info!("Broadcast graceful shutdown complete");
ShutdownType::Graceful
}
}
}

fn run_service(
mut service: Box<dyn Service>,
#[cfg(unix)] fds: Option<ListenFds>,
Expand Down Expand Up @@ -350,10 +373,7 @@ impl Server {
// blocked on main loop so that it runs forever
// Only work steal runtime can use block_on()
let server_runtime = Server::create_runtime("Server", 1, true);
#[cfg(unix)]
let shutdown_type = server_runtime.get_handle().block_on(self.main_loop());
#[cfg(windows)]
let shutdown_type = ShutdownType::Graceful;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the server will shut down immediately if we don't block it.


if matches!(shutdown_type, ShutdownType::Graceful) {
let exit_timeout = self
Expand Down