Skip to content

Commit 02566ef

Browse files
committed
Add a timeout to graceful termination
1 parent 2c51b87 commit 02566ef

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

plane/src/admin.rs

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use crate::{
1212
use chrono::Duration;
1313
use clap::{Parser, Subcommand};
1414
use colored::Colorize;
15-
use futures_util::{Stream, StreamExt};
1615
use std::path::PathBuf;
1716
use url::Url;
1817

plane/src/controller/mod.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ mod forward_auth;
5858
mod proxy;
5959
mod terminate;
6060

61+
/// How long to wait for the server to terminate gracefully before forcing it to shut down.
62+
/// We want to keep this just high enough to serve short requests. Long-lived requests
63+
/// will continue blocking indefinitely, which is why we need a timeout.
64+
const TERMINATE_TIMEOUT_DURATION: std::time::Duration = std::time::Duration::from_secs(2);
65+
6166
#[derive(Serialize, Deserialize, Debug)]
6267
pub struct StatusResponse {
6368
pub status: String,
@@ -295,6 +300,7 @@ impl ControllerServer {
295300
self.heartbeat_handle.terminate().await;
296301

297302
// Begin graceful shutdown of server.
303+
tracing::info!("Initiating graceful shutdown of server");
298304
let Some(graceful_terminate_sender) = self.graceful_terminate_sender.take() else {
299305
return;
300306
};
@@ -307,16 +313,19 @@ impl ControllerServer {
307313
return;
308314
};
309315

310-
match server_handle.await {
311-
Ok(Ok(())) => {
316+
match tokio::time::timeout(TERMINATE_TIMEOUT_DURATION, server_handle).await {
317+
Ok(Ok(Ok(()))) => {
312318
tracing::info!("Server gracefully terminated");
313319
}
314-
Ok(Err(err)) => {
320+
Ok(Ok(Err(err))) => {
315321
tracing::error!(?err, "Server error");
316322
}
317-
Err(err) => {
323+
Ok(Err(err)) => {
318324
tracing::error!(?err, "Server error");
319325
}
326+
Err(_) => {
327+
tracing::warn!("Server did not terminate gracefully in time, forcing shutdown");
328+
}
320329
}
321330
}
322331
}

0 commit comments

Comments
 (0)