Skip to content

Commit

Permalink
Fix Windows server shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Grvzard committed Oct 12, 2024
1 parent 52fd6d4 commit 63019c0
Showing 1 changed file with 23 additions and 3 deletions.
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;

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

0 comments on commit 63019c0

Please sign in to comment.