From 6b8b2a1009d515864de6b4fffc89083f96395f45 Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Tue, 2 Jun 2026 10:39:24 +0800 Subject: [PATCH] fix(connection): keep SSH tunnels alive --- apps/desktop/src/i18n/locales/en.ts | 2 +- apps/desktop/src/i18n/locales/es.ts | 2 +- apps/desktop/src/i18n/locales/zh-CN.ts | 2 +- apps/desktop/src/i18n/locales/zh-TW.ts | 2 +- crates/dbx-core/src/db/ssh_tunnel.rs | 38 +++++++++++++++++++++----- crates/dbx-core/src/query.rs | 2 ++ 6 files changed, 37 insertions(+), 11 deletions(-) diff --git a/apps/desktop/src/i18n/locales/en.ts b/apps/desktop/src/i18n/locales/en.ts index 842a06b6cb..05d89f6157 100644 --- a/apps/desktop/src/i18n/locales/en.ts +++ b/apps/desktop/src/i18n/locales/en.ts @@ -1572,7 +1572,7 @@ export default { showColumnCommentsInHeaderDescription: "Display table column comments directly below grid column names.", compactColumnHeaderActions: "Compact column header tools", compactColumnHeaderActionsDescription: - "Move sort, formatter, and local filter tools into a more menu so column names get priority.", + "Move formatter and local filter tools into a more menu so column names get priority.", sidebarActivation: "Sidebar activation", sidebarActivationSingle: "Single click", sidebarActivationSingleDescription: "Open actionable sidebar items with one click.", diff --git a/apps/desktop/src/i18n/locales/es.ts b/apps/desktop/src/i18n/locales/es.ts index 597601a472..c888fbeec8 100644 --- a/apps/desktop/src/i18n/locales/es.ts +++ b/apps/desktop/src/i18n/locales/es.ts @@ -1464,7 +1464,7 @@ export default { "Muestra los comentarios de columnas directamente debajo del nombre de la columna.", compactColumnHeaderActions: "Compactar herramientas del encabezado", compactColumnHeaderActionsDescription: - "Mueve ordenar, formatear y filtrar a un menú de más acciones para priorizar el nombre de columna.", + "Mueve formatear y filtrar a un menú de más acciones para priorizar el nombre de columna.", sidebarActivation: "Activación de la barra lateral", sidebarActivationSingle: "Un clic", sidebarActivationSingleDescription: "Abrir elementos accionables de la barra lateral con un clic.", diff --git a/apps/desktop/src/i18n/locales/zh-CN.ts b/apps/desktop/src/i18n/locales/zh-CN.ts index 548c1fc26d..35df4625bf 100644 --- a/apps/desktop/src/i18n/locales/zh-CN.ts +++ b/apps/desktop/src/i18n/locales/zh-CN.ts @@ -1542,7 +1542,7 @@ export default { showColumnCommentsInHeader: "在字段名下方显示注释", showColumnCommentsInHeaderDescription: "把表字段注释直接显示在结果表头字段名下方。", compactColumnHeaderActions: "收起字段表头工具", - compactColumnHeaderActionsDescription: "将排序、格式化、本地筛选收进更多菜单,优先显示字段名称。", + compactColumnHeaderActionsDescription: "将格式化、本地筛选收进更多菜单,优先显示字段名称。", sidebarActivation: "侧边栏打开方式", sidebarActivationSingle: "单击打开", sidebarActivationSingleDescription: "单击即可打开侧边栏中的可操作项目。", diff --git a/apps/desktop/src/i18n/locales/zh-TW.ts b/apps/desktop/src/i18n/locales/zh-TW.ts index 7521354a29..1885e13d4b 100644 --- a/apps/desktop/src/i18n/locales/zh-TW.ts +++ b/apps/desktop/src/i18n/locales/zh-TW.ts @@ -1541,7 +1541,7 @@ export default { showColumnCommentsInHeader: "在欄位名稱下方顯示註解", showColumnCommentsInHeaderDescription: "直接在資料表格欄位名稱下方顯示資料表欄位註解。", compactColumnHeaderActions: "收起欄位表頭工具", - compactColumnHeaderActionsDescription: "將排序、格式設定和本機篩選工具移到更多選單,讓欄位名稱優先顯示。", + compactColumnHeaderActionsDescription: "將格式設定和本機篩選工具移到更多選單,讓欄位名稱優先顯示。", sidebarActivation: "側邊欄開啟方式", sidebarActivationSingle: "單擊開啟", sidebarActivationSingleDescription: "單擊即可開啟側邊欄中的可操作項目。", diff --git a/crates/dbx-core/src/db/ssh_tunnel.rs b/crates/dbx-core/src/db/ssh_tunnel.rs index 1309bd3872..30746f0e04 100644 --- a/crates/dbx-core/src/db/ssh_tunnel.rs +++ b/crates/dbx-core/src/db/ssh_tunnel.rs @@ -8,7 +8,7 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::TcpListener; use tokio::sync::Mutex; use tokio::task::JoinHandle; -use tokio::time::Duration; +use tokio::time::{Duration, MissedTickBehavior}; use crate::models::connection::SshTunnelConfig; @@ -20,6 +20,10 @@ const INITIAL_RECONNECT_DELAY: Duration = Duration::from_secs(5); const MAX_RECONNECT_DELAY: Duration = Duration::from_secs(60); /// Maximum number of consecutive reconnect attempts before giving up. const MAX_RECONNECT_ATTEMPTS: u32 = 10; +/// How often an idle local listener verifies that the SSH session still answers. +const IDLE_SESSION_CHECK_INTERVAL: Duration = Duration::from_secs(30); +/// Maximum time to wait for an explicit SSH ping response. +const IDLE_SESSION_PING_TIMEOUT: Duration = Duration::from_secs(10); struct SshClient; @@ -93,8 +97,32 @@ async fn connect_and_authenticate( /// Accept connections on the local listener and forward them through the SSH session. /// Returns when the SSH session dies (listener error or session.is_closed()). async fn forward_loop(session: &Handle, listener: &TcpListener, remote_host: &str, remote_port: u16) { + let mut idle_check = tokio::time::interval(IDLE_SESSION_CHECK_INTERVAL); + idle_check.set_missed_tick_behavior(MissedTickBehavior::Delay); + loop { - let (mut stream, peer_addr) = match listener.accept().await { + let accepted = tokio::select! { + result = listener.accept() => result, + _ = idle_check.tick() => { + if session.is_closed() { + log::warn!("SSH session closed while tunnel was idle"); + break; + } + match tokio::time::timeout(IDLE_SESSION_PING_TIMEOUT, session.send_ping()).await { + Ok(Ok(())) => continue, + Ok(Err(e)) => { + log::warn!("SSH idle ping failed: {e}"); + break; + } + Err(_) => { + log::warn!("SSH idle ping timed out"); + break; + } + } + } + }; + + let (mut stream, peer_addr) = match accepted { Ok(v) => v, Err(e) => { log::error!("SSH tunnel listener error: {e}"); @@ -120,11 +148,7 @@ async fn forward_loop(session: &Handle, listener: &TcpListener, remot Ok(c) => c, Err(e) => { log::error!("SSH direct-tcpip failed: {e}"); - if session.is_closed() { - log::warn!("SSH session closed after channel open failure"); - break; - } - continue; + break; } }; diff --git a/crates/dbx-core/src/query.rs b/crates/dbx-core/src/query.rs index 2a7ca4201f..eb864f3d69 100644 --- a/crates/dbx-core/src/query.rs +++ b/crates/dbx-core/src/query.rs @@ -359,6 +359,7 @@ pub fn is_connection_error(err: &str) -> bool { || lower.contains("not connected") || lower.contains("end-of-file") || lower.contains("idle") + || lower.contains("communicating with the server") || is_os_connection_error(&lower) } @@ -1321,6 +1322,7 @@ mod tests { assert!(is_connection_error("Connection timed out")); assert!(is_connection_error("socket closed")); assert!(is_connection_error("unexpected eof")); + assert!(is_connection_error("Error occurred while creating a new object: error communicating with the server")); } #[test]