fix(ssh): prevent duplicate SSH tunnels from concurrent pool creation (#891)

start_tunnel() and start_chain() had a check-then-act race condition:
multiple concurrent callers could all see an empty cache and each
spawn a new SSH tunnel. The tunnel that lost the insert race was
orphaned, causing queries routed through it to fail.

Use double-check locking with stale entry eviction: hold the lock for
the cache lookup, release it during the slow SSH handshake, then
re-check under the lock before inserting. If another caller already
created the tunnel, abort the duplicate and return the existing port.

Also check handle liveness on cache hits so that callers don't get a
dead port back when the background tunnel task has exited.

Add a matching re-check in the MongoDB connection pool creation path.

Add idle_timeout_secs connection option (default 60s) to preempt
server-side connection idle timeouts that cause "unexpected end of
file" errors on pooled MongoDB connections.

Reject MongoDB queries in the generic SQL execution path before any
pool or session-key creation. Previously, a typo in a MongoDB shell
command would fall through to executeMulti / execute_sql_statement,
which called get_or_create_pool_for_session and leaked a session-scoped
MongoDB Client (and SSH tunnel resources) before eventually returning
"Use MongoDB-specific commands".

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
James-LeongandClaude Opus 4.7 authored and t8y2 committed 2026-06-09 10:29:42 +08:00
1 parent 32164d5aea
commit 171c7cd8d7
15 files changed
+111 -16

No files matched your search

+8 -2
View File
@@ -324,6 +324,7 @@ impl AppState {
probe_connection_endpoint(&db_config, &host, port).await?;
let url = connection_url_for_endpoint(&db_config, &host, port);
let connect_timeout = std::time::Duration::from_secs(db_config.effective_connect_timeout_secs());
let idle_timeout = std::time::Duration::from_secs(db_config.idle_timeout_secs);
let mysql_pool_max_connections = if normalize_client_session_id(client_session_id).is_some() { 1 } else { 3 };
let pool = match db_config.db_type {
DatabaseType::Mysql => {
@@ -401,7 +402,7 @@ impl AppState {
PoolKind::DuckDb(con)
}
DatabaseType::MongoDb => {
let native_err = match db::mongo_driver::connect(&url, connect_timeout).await {
let native_err = match db::mongo_driver::connect(&url, connect_timeout, idle_timeout).await {
Ok(client) => match db::mongo_driver::test_connection(
&client,
connect_timeout,
@@ -410,7 +411,12 @@ impl AppState {
.await
{
Ok(()) => {
self.connections.write().await.insert(pool_key.clone(), PoolKind::MongoDb(client));
let mut conns = self.connections.write().await;
// Re-check: another task may have created the pool while we were connecting.
if conns.contains_key(&pool_key) {
return Ok(pool_key);
}
conns.insert(pool_key.clone(), PoolKind::MongoDb(client));
return Ok(pool_key);
}
Err(e) => e,
+7 -1
View File
@@ -16,7 +16,7 @@ pub struct MongoDocumentResult {
pub total: u64,
}
pub async fn connect(url: &str, timeout: Duration) -> Result<Client, String> {
pub async fn connect(url: &str, timeout: Duration, idle_timeout: Duration) -> Result<Client, String> {
let is_multi_host = is_multi_host_mongo_uri(url);
let parse_timeout = if is_multi_host { std::cmp::max(timeout * 2, Duration::from_secs(10)) } else { timeout };
@@ -25,6 +25,12 @@ pub async fn connect(url: &str, timeout: Duration) -> Result<Client, String> {
options.connect_timeout = Some(timeout);
options.server_selection_timeout =
if is_multi_host { Some(std::cmp::max(timeout * 2, Duration::from_secs(10))) } else { Some(timeout) };
// Close idle connections before the server-side timeout drops them,
// preventing "Broken pipe" (os error 32) or "unexpected end of file".
// 0 means no idle timeout (keep connections alive indefinitely).
if idle_timeout.as_secs() > 0 {
options.max_idle_time = Some(idle_timeout);
}
Client::with_options(options).map_err(|e| format!("MongoDB connection failed: {e}"))
})
.await
+42 -11
View File
@@ -309,9 +309,15 @@ impl TunnelManager {
remote_port: u16,
expose_to_lan: bool,
) -> Result<u16, String> {
if let Some(local_port) = self.local_port(connection_id).await {
return Ok(local_port);
// Check cache under lock to avoid race with concurrent callers.
// Also evict stale entries whose background task has exited.
{
let mut tunnels = self.tunnels.lock().await;
if let Some(port) = Self::get_active_port(&mut tunnels, connection_id) {
return Ok(port);
}
}
// Slow SSH connection — do this outside the lock.
let (handle, local_port) = spawn_tunnel(
ssh_host,
ssh_port,
@@ -326,11 +332,28 @@ impl TunnelManager {
)
.await?;
self.tunnels.lock().await.insert(connection_id.to_string(), TunnelEntry { handles: vec![handle], local_port });
// Re-check under lock: another caller may have beaten us.
let mut tunnels = self.tunnels.lock().await;
if let Some(port) = Self::get_active_port(&mut tunnels, connection_id) {
// Another task already created a live tunnel; abort ours.
handle.abort();
return Ok(port);
}
tunnels.insert(connection_id.to_string(), TunnelEntry { handles: vec![handle], local_port });
Ok(local_port)
}
/// Returns the local port for a cached tunnel entry, or `None` if the entry
/// is stale (all background handles have exited).
fn get_active_port(tunnels: &mut HashMap<String, TunnelEntry>, connection_id: &str) -> Option<u16> {
let entry = tunnels.get(connection_id)?;
if entry.handles.iter().all(|h| h.is_finished()) {
tunnels.remove(connection_id);
return None;
}
Some(entry.local_port)
}
pub async fn start_chain(
&self,
connection_id: &str,
@@ -341,8 +364,12 @@ impl TunnelManager {
if hops.is_empty() {
return Err("No SSH tunnel hops configured".to_string());
}
if let Some(local_port) = self.local_port(connection_id).await {
return Ok(local_port);
// Check cache under lock; evict stale entries.
{
let mut tunnels = self.tunnels.lock().await;
if let Some(port) = Self::get_active_port(&mut tunnels, connection_id) {
return Ok(port);
}
}
let mut handles = Vec::new();
@@ -379,11 +406,15 @@ impl TunnelManager {
next_connect_endpoint = Some(("127.0.0.1".to_string(), local_port));
}
self.tunnels
.lock()
.await
.insert(connection_id.to_string(), TunnelEntry { handles, local_port: final_local_port });
// Re-check under lock: another caller may have beaten us.
let mut tunnels = self.tunnels.lock().await;
if let Some(port) = Self::get_active_port(&mut tunnels, connection_id) {
for handle in handles {
handle.abort();
}
return Ok(port);
}
tunnels.insert(connection_id.to_string(), TunnelEntry { handles, local_port: final_local_port });
Ok(final_local_port)
}
+9
View File
@@ -30,6 +30,8 @@ pub struct ConnectionConfig {
pub connect_timeout_secs: u64,
#[serde(default = "default_query_timeout_secs")]
pub query_timeout_secs: u64,
#[serde(default = "default_idle_timeout_secs")]
pub idle_timeout_secs: u64,
#[serde(default)]
pub ssl: bool,
#[serde(default, skip_serializing_if = "String::is_empty")]
@@ -180,6 +182,10 @@ pub fn default_query_timeout_secs() -> u64 {
30
}
pub fn default_idle_timeout_secs() -> u64 {
60
}
fn default_proxy_port() -> u16 {
1080
}
@@ -295,6 +301,8 @@ struct ConnectionConfigData {
pub connect_timeout_secs: u64,
#[serde(default = "default_query_timeout_secs")]
pub query_timeout_secs: u64,
#[serde(default = "default_idle_timeout_secs")]
pub idle_timeout_secs: u64,
#[serde(default)]
pub ssl: bool,
#[serde(default)]
@@ -355,6 +363,7 @@ impl From<ConnectionConfigData> for ConnectionConfig {
transport_layers: data.transport_layers,
connect_timeout_secs: data.connect_timeout_secs,
query_timeout_secs: data.query_timeout_secs,
idle_timeout_secs: data.idle_timeout_secs,
ssl: data.ssl,
ca_cert_path: data.ca_cert_path,
client_cert_path: data.client_cert_path,
+18
View File
@@ -15,6 +15,11 @@ pub const QUERY_TIMEOUT: Duration = Duration::from_secs(30);
pub const MAX_ROWS: usize = 10000;
pub const QUERY_CANCELED: &str = "Query canceled";
async fn connection_is_mongodb(state: &AppState, connection_id: &str) -> bool {
let configs = state.configs.read().await;
configs.get(connection_id).is_some_and(|config| config.db_type == DatabaseType::MongoDb)
}
async fn connection_database_type(state: &AppState, connection_id: &str) -> Option<DatabaseType> {
let configs = state.configs.read().await;
configs.get(connection_id).map(|config| config.db_type)
@@ -804,6 +809,14 @@ pub async fn execute_sql_statement_with_options(
cancel_token: Option<CancellationToken>,
options: QueryExecutionOptions,
) -> Result<db::QueryResult, String> {
// MongoDB connections use shell-style commands dispatched through the
// frontend parser. Queries that fall through to the generic SQL executor
// (e.g. typos) must be rejected before any pool/key creation so that
// session-scoped pools do not leak MongoDB Clients and SSH tunnels.
if connection_is_mongodb(state, connection_id).await {
return Err("Use MongoDB-specific commands".to_string());
}
// When a query tab has a client session, keep even database-less execution
// on that tab-scoped pool so connection-level state (for example MySQL @vars)
// survives across runs.
@@ -893,6 +906,11 @@ pub async fn execute_multi_core_with_options(
cancel_token: Option<CancellationToken>,
options: QueryExecutionOptions,
) -> Result<Vec<db::QueryResult>, String> {
// Reject MongoDB queries that fall through to the generic executor.
if connection_is_mongodb(state, connection_id).await {
return Err("Use MongoDB-specific commands".to_string());
}
let pool_key = if database.is_empty() {
state.get_or_create_pool_for_session(connection_id, None, options.client_session_id.as_deref()).await?
} else {