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:
1 parent
32164d5aea
commit
171c7cd8d7
15 files changed
+111
-16
No files matched your search
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user