From 3009b9ac54197be7fb234bc4ec09b0ed81488ff8 Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Thu, 30 Jul 2026 02:14:23 +0800 Subject: [PATCH] fix(updater): clean runtime resources before restart --- crates/dbx-core/src/connection.rs | 47 ++++++++++- crates/dbx-core/src/db/http_tunnel.rs | 7 ++ crates/dbx-core/src/db/proxy_tunnel.rs | 7 ++ crates/dbx-core/src/db/ssh_tunnel.rs | 10 +++ crates/dbx-web/src/main.rs | 2 +- src-tauri/src/commands/redis_pubsub_server.rs | 84 +++++++++++++++---- src-tauri/src/lib.rs | 12 ++- 7 files changed, 149 insertions(+), 20 deletions(-) diff --git a/crates/dbx-core/src/connection.rs b/crates/dbx-core/src/connection.rs index 6a6e62cb27..379273be40 100644 --- a/crates/dbx-core/src/connection.rs +++ b/crates/dbx-core/src/connection.rs @@ -1109,9 +1109,24 @@ impl AppState { } } - pub async fn shutdown_background_tasks(&self, deadline: Duration) { + pub async fn shutdown(&self, deadline: Duration) { self.running_queries.cancel_all(); - self.task_supervisor.shutdown(deadline).await; + let removed_pools = self.drain_all_connection_pools().await; + self.transaction_sessions.write().await.clear(); + + let shutdown = async { + tokio::join!( + self.task_supervisor.shutdown(deadline), + close_removed_pools(removed_pools), + self.tunnels.stop_all_tunnels(), + self.proxy_tunnels.stop_all_tunnels(), + self.http_tunnels.stop_all_tunnels(), + self.agent_manager.stop_daemons(), + ); + }; + if tokio::time::timeout(deadline, shutdown).await.is_err() { + log::warn!("Timed out shutting down DBX runtime resources after {}ms", deadline.as_millis()); + } } #[cfg(test)] @@ -3044,6 +3059,15 @@ impl AppState { close_removed_pools_in_background(&self.task_supervisor, removed); } + async fn drain_all_connection_pools(&self) -> Vec<(String, PoolKind)> { + let pool_keys = self.connections.read().await.keys().cloned().collect::>(); + self.stop_keepalive_tasks(&pool_keys).await; + self.pool_activity.write().await.clear(); + self.postgres_cancel_contexts.write().await.clear(); + self.draining_pools.lock().unwrap_or_else(|error| error.into_inner()).clear(); + self.connections.write().await.drain().collect() + } + #[cfg(feature = "duckdb-sidecar")] async fn remove_duckdb_pools_detached(&self) { let removed = self.drain_duckdb_pools().await; @@ -4263,6 +4287,25 @@ mod tests { ))) } + #[tokio::test] + async fn shutdown_releases_connection_pools_and_agent_daemons() { + let (state, dir) = test_app_state().await; + state.connections.write().await.insert("conn".to_string(), agent_pool_stub()); + state + .agent_manager + .daemons + .lock() + .await + .insert("dameng".to_string(), crate::db::agent_driver::AgentDriverClient::test_stub()); + + state.shutdown(Duration::from_secs(1)).await; + + assert!(state.connections.read().await.is_empty()); + assert!(state.agent_manager.active_daemon_keys().await.is_empty()); + + let _ = std::fs::remove_dir_all(dir); + } + #[tokio::test] async fn agent_update_blockers_only_include_open_connections() { let (state, dir) = test_app_state().await; diff --git a/crates/dbx-core/src/db/http_tunnel.rs b/crates/dbx-core/src/db/http_tunnel.rs index 8a4b51bbc1..8d8d83df77 100644 --- a/crates/dbx-core/src/db/http_tunnel.rs +++ b/crates/dbx-core/src/db/http_tunnel.rs @@ -75,6 +75,13 @@ impl HttpTunnelManager { } } } + + pub async fn stop_all_tunnels(&self) { + let tunnels = std::mem::take(&mut *self.tunnels.lock().await); + for (handle, _) in tunnels.into_values() { + handle.abort(); + } + } } struct HttpTunnelEndpoint { diff --git a/crates/dbx-core/src/db/proxy_tunnel.rs b/crates/dbx-core/src/db/proxy_tunnel.rs index b7ad090581..5c2dc550ba 100644 --- a/crates/dbx-core/src/db/proxy_tunnel.rs +++ b/crates/dbx-core/src/db/proxy_tunnel.rs @@ -83,6 +83,13 @@ impl ProxyTunnelManager { } } } + + pub async fn stop_all_tunnels(&self) { + let tunnels = std::mem::take(&mut *self.tunnels.lock().await); + for (handle, _) in tunnels.into_values() { + handle.abort(); + } + } } #[derive(Clone)] diff --git a/crates/dbx-core/src/db/ssh_tunnel.rs b/crates/dbx-core/src/db/ssh_tunnel.rs index 643c0c2e73..48810fcfc2 100644 --- a/crates/dbx-core/src/db/ssh_tunnel.rs +++ b/crates/dbx-core/src/db/ssh_tunnel.rs @@ -978,6 +978,16 @@ impl TunnelManager { self.stop_tunnel(&key).await; } } + + pub async fn stop_all_tunnels(&self) { + let tunnels = std::mem::take(&mut *self.tunnels.lock().await); + for entry in tunnels.into_values() { + for handle in entry.handles { + handle.abort(); + } + } + self.start_locks.lock().await.clear(); + } } #[allow(clippy::too_many_arguments)] diff --git a/crates/dbx-web/src/main.rs b/crates/dbx-web/src/main.rs index 2f9d5819ea..0a6895e5f0 100644 --- a/crates/dbx-web/src/main.rs +++ b/crates/dbx-web/src/main.rs @@ -800,7 +800,7 @@ async fn main() { }) .await .expect("Server error"); - shutdown_state.shutdown_background_tasks(std::time::Duration::from_secs(3)).await; + shutdown_state.shutdown(std::time::Duration::from_secs(3)).await; } #[cfg(test)] diff --git a/src-tauri/src/commands/redis_pubsub_server.rs b/src-tauri/src/commands/redis_pubsub_server.rs index 56195ea04e..295ee84bd0 100644 --- a/src-tauri/src/commands/redis_pubsub_server.rs +++ b/src-tauri/src/commands/redis_pubsub_server.rs @@ -1,4 +1,6 @@ use std::sync::Arc; +use std::sync::Mutex; +use std::time::Duration; use std::{net::Ipv4Addr, net::TcpListener}; use axum::extract::ws::{Message, WebSocket}; @@ -8,20 +10,45 @@ use axum::routing::get; use axum::Router; use futures::{SinkExt, StreamExt}; use serde::Deserialize; +use tauri::async_runtime::JoinHandle; +use tokio_util::sync::CancellationToken; use dbx_core::connection::AppState; const DEFAULT_PUBSUB_PORT: u16 = 4224; -pub struct PubSubServerPort(Option); +pub struct PubSubServerState { + port: Option, + shutdown: CancellationToken, + task: Mutex>>, +} -impl PubSubServerPort { - fn new(port: Option) -> Self { - Self(port) +impl PubSubServerState { + fn unavailable() -> Self { + Self { port: None, shutdown: CancellationToken::new(), task: Mutex::new(None) } } fn get(&self) -> Result { - self.0.ok_or_else(|| "Redis PubSub server is unavailable".to_string()) + self.port.ok_or_else(|| "Redis PubSub server is unavailable".to_string()) + } + + pub async fn shutdown(&self, deadline: Duration) { + self.shutdown.cancel(); + let task = self.task.lock().unwrap_or_else(|error| error.into_inner()).take(); + let Some(mut task) = task else { return }; + if tokio::time::timeout(deadline, &mut task).await.is_err() { + task.abort(); + let _ = task.await; + } + } +} + +impl Drop for PubSubServerState { + fn drop(&mut self) { + self.shutdown.cancel(); + if let Some(task) = self.task.get_mut().unwrap_or_else(|error| error.into_inner()).take() { + task.abort(); + } } } @@ -40,8 +67,8 @@ fn pubsub_server_port() -> u16 { } #[tauri::command] -pub fn redis_pubsub_server_port(port: tauri::State<'_, PubSubServerPort>) -> Result { - port.get() +pub fn redis_pubsub_server_port(state: tauri::State<'_, PubSubServerState>) -> Result { + state.get() } fn bind_pubsub_listener(preferred_port: u16) -> Result { @@ -173,28 +200,38 @@ async fn handle_command(sink: &mut redis::aio::PubSubSink, text: &str) -> Result /// Start the embedded web server for PubSub WebSocket support. /// Runs on a background task using the shared AppState. -pub fn start_pubsub_server(state: Arc) -> PubSubServerPort { +pub fn start_pubsub_server(state: Arc) -> PubSubServerState { let router = build_pubsub_router(state); let listener = match bind_pubsub_listener(pubsub_server_port()) { Ok(listener) => listener, Err(error) => { log::warn!("{error}"); - return PubSubServerPort::new(None); + return PubSubServerState::unavailable(); } }; let addr = match listener.local_addr() { Ok(addr) => addr, Err(error) => { log::warn!("Failed to read PubSub server address: {error}"); - return PubSubServerPort::new(None); + return PubSubServerState::unavailable(); } }; if let Err(error) = listener.set_nonblocking(true) { log::warn!("Failed to configure PubSub server listener: {error}"); - return PubSubServerPort::new(None); + return PubSubServerState::unavailable(); } - tauri::async_runtime::spawn(async move { + start_pubsub_server_with_listener(listener, addr, router) +} + +fn start_pubsub_server_with_listener( + listener: TcpListener, + addr: std::net::SocketAddr, + router: Router, +) -> PubSubServerState { + let shutdown = CancellationToken::new(); + let shutdown_signal = shutdown.clone(); + let task = tauri::async_runtime::spawn(async move { let listener = match tokio::net::TcpListener::from_std(listener) { Ok(listener) => listener, Err(error) => { @@ -203,18 +240,22 @@ pub fn start_pubsub_server(state: Arc) -> PubSubServerPort { } }; log::info!("PubSub WebSocket server listening on {addr}"); - if let Err(error) = axum::serve(listener, router).await { + if let Err(error) = + axum::serve(listener, router).with_graceful_shutdown(shutdown_signal.cancelled_owned()).await + { log::warn!("PubSub server stopped with error: {error}"); } }); - PubSubServerPort::new(Some(addr.port())) + PubSubServerState { port: Some(addr.port()), shutdown, task: Mutex::new(Some(task)) } } #[cfg(test)] mod tests { - use super::bind_pubsub_listener; + use super::{bind_pubsub_listener, start_pubsub_server_with_listener}; + use axum::Router; use std::net::{Ipv4Addr, TcpListener}; + use std::time::Duration; #[test] fn falls_back_to_an_available_local_port_when_the_preferred_port_is_in_use() { @@ -226,4 +267,17 @@ mod tests { assert_eq!(listener.local_addr().unwrap().ip(), Ipv4Addr::LOCALHOST); assert_ne!(listener.local_addr().unwrap().port(), preferred_port); } + + #[tokio::test] + async fn shutdown_releases_listener_before_process_exit() { + let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); + let addr = listener.local_addr().unwrap(); + listener.set_nonblocking(true).unwrap(); + let state = start_pubsub_server_with_listener(listener, addr, Router::new()); + + state.shutdown(Duration::from_secs(1)).await; + + let rebound = TcpListener::bind(addr).unwrap(); + assert_eq!(rebound.local_addr().unwrap(), addr); + } } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 4a99446169..235c08a146 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1900,8 +1900,16 @@ pub fn run() { if should_confirm_app_exit_request(std::env::consts::OS, *code, confirmed_exit) { api.prevent_exit(); request_app_close(app_handle, "quit"); - } else if let Some(state) = app_handle.try_state::>() { - tauri::async_runtime::block_on(state.shutdown_background_tasks(Duration::from_secs(3))); + } else { + tauri::async_runtime::block_on(async { + if let Some(server) = app_handle.try_state::() + { + server.shutdown(Duration::from_secs(1)).await; + } + if let Some(state) = app_handle.try_state::>() { + state.shutdown(Duration::from_secs(3)).await; + } + }); } }