[model-gateway] fix graceful shutdown for TLS/Non-TLS server#15491
[model-gateway] fix graceful shutdown for TLS/Non-TLS server#15491
Conversation
The issue: axum_server::Handle::graceful_shutdown(None) doesn't wait for connections - it initiates shutdown immediately. The fix: Use axum_server for both TLS and non-TLS paths with graceful_shutdown(Some(duration)) to wait up to the configured grace period for existing connections (including streaming) to close. Changes: - Use axum_server::bind for non-TLS (instead of axum::serve) - Use axum_server::bind_rustls for TLS - Both paths now use graceful_shutdown(Some(grace_period)) - Add --shutdown-grace-period-secs CLI flag (default: 180s) - Python CLI: Added --shutdown-grace-period-secs argument - Python bindings: Added shutdown_grace_period_secs parameter K8s users: Ensure terminationGracePeriodSeconds >= shutdown_grace_period_secs
Summary of ChangesHello @slin1237, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the robustness of the model gateway's shutdown process. By adopting Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly implements graceful shutdown for both TLS and non-TLS servers by switching to axum_server and using its graceful_shutdown feature with a configurable grace period. The changes are well-implemented across the Python bindings and the Rust core. I've added a couple of suggestions to improve code clarity and maintainability: one to fix an incorrect comment about the default grace period, and another to refactor duplicated code for setting up the graceful shutdown logic.
| pub request_timeout_secs: u64, | ||
| pub request_id_headers: Option<Vec<String>>, | ||
| /// Grace period in seconds to wait for in-flight requests during shutdown. | ||
| /// Default is 30 seconds. |
There was a problem hiding this comment.
| let addr: std::net::SocketAddr = bind_addr | ||
| .parse() | ||
| .map_err(|e| format!("Invalid address: {}", e))?; | ||
|
|
||
| let handle = axum_server::Handle::new(); | ||
| let handle_clone = handle.clone(); | ||
| let grace_period = Duration::from_secs(config.shutdown_grace_period_secs); | ||
| spawn(async move { | ||
| shutdown_signal().await; | ||
| handle_clone.graceful_shutdown(Some(grace_period)); | ||
| }); |
There was a problem hiding this comment.
This logic for parsing the address and setting up the graceful shutdown is duplicated from the TLS branch. To improve maintainability and reduce redundancy, consider extracting this common code to before the if/else block.
For example, you could refactor the if/else block to look like this:
let addr: std::net::SocketAddr = bind_addr
.parse()
.map_err(|e| format!("Invalid address: {}", e))?;
let handle = axum_server::Handle::new();
let grace_period = Duration::from_secs(config.shutdown_grace_period_secs);
// Spawn a task to gracefully shutdown the server.
let handle_clone = handle.clone();
spawn(async move {
shutdown_signal().await;
handle_clone.graceful_shutdown(Some(grace_period));
});
if let (Some(cert), Some(key)) = (
&config.router_config.server_cert,
&config.router_config.server_key,
) {
info!("TLS enabled");
ring::default_provider()
.install_default()
.map_err(|e| format!("Failed to install rustls ring provider: {:?}", e))?;
let tls_config = axum_server::tls_rustls::RustlsConfig::from_pem(cert.clone(), key.clone())
.await
.map_err(|e| format!("Failed to create TLS config: {}", e))?;
axum_server::bind_rustls(addr, tls_config)
.handle(handle)
.serve(app.into_make_service())
.await
.map_err(|e| Box::new(e) as Box<dyn std::error::Error>)?;
} else {
axum_server::bind(addr)
.handle(handle)
.serve(app.into_make_service())
.await
.map_err(|e| Box::new(e) as Box<dyn std::error::Error>)?;
}
The fix: Use axum_server for both TLS and non-TLS paths with graceful_shutdown(Some(duration)) to wait up to the configured grace period for existing connections (including streaming) to close.
Changes:
K8s users: Ensure terminationGracePeriodSeconds >= shutdown_grace_period_secs
Checklist