Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(web): ensure prompt shutdown on Ctrl+C and SIGTERM

Luis Araya6a2bcdaf65d7
diff --git a/crates/dbx-web/src/main.rs b/crates/dbx-web/src/main.rs
index dbe8f57695..a67852f9e8 100644
--- a/crates/dbx-web/src/main.rs
+++ b/crates/dbx-web/src/main.rs
@@ -1437,22 +1437,80 @@ async fn serve() {
let listener = tokio::net::TcpListener::bind(addr).await.expect("Failed to bind address");
let shutdown_state = web_state.app.clone();
- axum::serve(listener, app)
- .with_graceful_shutdown(async move {
- #[cfg(unix)]
- {
- let mut terminate = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
- .expect("Failed to listen for SIGTERM");
- tokio::select! { _ = tokio::signal::ctrl_c() => {}, _ = terminate.recv() => {} }
+ let server_shutdown = tokio_util::sync::CancellationToken::new();
+ let server_shutdown_trigger = server_shutdown.clone();
+ tokio::spawn(async move {
+ #[cfg(unix)]
+ {
+ let mut terminate: Option<tokio::signal::unix::Signal> =
+ match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
+ Ok(s) => Some(s),
+ Err(e) => {
+ tracing::error!("Failed to install SIGTERM handler: {e}; continuing with Ctrl+C only");
+ None
+ }
+ };
+ tokio::select! {
+ res = tokio::signal::ctrl_c() => {
+ if let Err(e) = res {
+ tracing::error!("Failed to listen for Ctrl+C: {e}");
+ } else {
+ tracing::info!("Shutdown signal received (Ctrl+C)");
+ }
+ }
+ _ = async {
+ match terminate.as_mut() {
+ Some(signal) => {
+ signal.recv().await;
+ }
+ None => std::future::pending::<()>().await,
+ }
+ } => {
+ tracing::info!("Shutdown signal received (SIGTERM)");
+ }
+ }
+ }
+ #[cfg(not(unix))]
+ {
+ if let Err(e) = tokio::signal::ctrl_c().await {
+ tracing::error!("Failed to listen for Ctrl+C: {e}");
+ } else {
+ tracing::info!("Shutdown signal received (Ctrl+C)");
+ }
+ }
+ backup_stop.cancel();
+ server_shutdown_trigger.cancel();
+ });
+
+ let shutdown_wait = server_shutdown.clone();
+ let serve_future = axum::serve(listener, app).with_graceful_shutdown(async move {
+ shutdown_wait.cancelled().await;
+ });
+
+ // If graceful shutdown of HTTP connections takes longer than 5 seconds, abort to ensure prompt termination on Ctrl+C.
+ // Non-zero exit codes keep systemd's Restart=on-failure meaningful: a serve error or an
+ // undrained backup worker must not look like a clean stop.
+ let mut exit_code = 0i32;
+ tokio::select! {
+ res = serve_future => {
+ if let Err(e) = res {
+ tracing::error!("Server error: {e}");
+ exit_code = 1;
}
- #[cfg(not(unix))]
- let _ = tokio::signal::ctrl_c().await;
- backup_stop.cancel();
- })
- .await
- .expect("Server error");
- let _ = tokio::time::timeout(std::time::Duration::from_secs(60), backup_worker).await;
+ }
+ _ = async {
+ server_shutdown.cancelled().await;
+ tokio::time::sleep(std::time::Duration::from_secs(5)).await;
+ } => {
+ tracing::warn!("Graceful HTTP shutdown timed out after 5s; proceeding with teardown");
+ }
+ }
+ if tokio::time::timeout(std::time::Duration::from_secs(5), backup_worker).await.is_err() {
+ tracing::warn!("Scheduled backup worker did not drain within 5s; exiting with failure status");
+ exit_code = 1;
+ }
shutdown_state.shutdown(std::time::Duration::from_secs(3)).await;
+ std::process::exit(exit_code);
}
#[cfg(test)]