diff --git a/apps/desktop/src/components/connection/ConnectionDialog.vue b/apps/desktop/src/components/connection/ConnectionDialog.vue index fe5615f4c0..c5501ee423 100644 --- a/apps/desktop/src/components/connection/ConnectionDialog.vue +++ b/apps/desktop/src/components/connection/ConnectionDialog.vue @@ -112,7 +112,7 @@ const defaultForm = (): ConnectionForm => ({ database: undefined, color: "", transport_layers: [], - connect_timeout_secs: 5, + connect_timeout_secs: 10, query_timeout_secs: 30, idle_timeout_secs: 60, keepalive_interval_secs: 0, @@ -720,7 +720,7 @@ watch( database: config.database, color: config.color || "", transport_layers: transportLayersForConfig(legacyConfig), - connect_timeout_secs: config.connect_timeout_secs || 5, + connect_timeout_secs: config.connect_timeout_secs || 10, query_timeout_secs: config.query_timeout_secs ?? 30, idle_timeout_secs: config.idle_timeout_secs ?? 60, keepalive_interval_secs: config.keepalive_interval_secs ?? 0, @@ -1256,7 +1256,7 @@ function connectionConfigForSubmit(id: string): ConnectionConfig { }); validateTransportLayers(config); const connectTimeout = Number(config.connect_timeout_secs); - config.connect_timeout_secs = Number.isFinite(connectTimeout) && connectTimeout > 0 ? connectTimeout : 5; + config.connect_timeout_secs = Number.isFinite(connectTimeout) && connectTimeout > 0 ? connectTimeout : 10; const queryTimeout = Number(config.query_timeout_secs); config.query_timeout_secs = Number.isFinite(queryTimeout) && queryTimeout >= 0 ? queryTimeout : 30; const idleTimeout = Number(config.idle_timeout_secs); diff --git a/apps/desktop/src/lib/__tests__/connectionAttemptTimeout.spec.ts b/apps/desktop/src/lib/__tests__/connectionAttemptTimeout.spec.ts index ebce369cfd..caa1667e00 100644 --- a/apps/desktop/src/lib/__tests__/connectionAttemptTimeout.spec.ts +++ b/apps/desktop/src/lib/__tests__/connectionAttemptTimeout.spec.ts @@ -1,7 +1,15 @@ import { describe, expect, it } from "vitest"; -import { ACCESS_AGENT_MIN_CONNECT_TIMEOUT_SECS, AGENT_DRIVER_MIN_CONNECT_TIMEOUT_SECS, connectionAttemptTimeoutMs, CONNECTION_ATTEMPT_TIMEOUT_BUFFER_MS } from "@/lib/connectionAttemptTimeout"; +import { ACCESS_AGENT_MIN_CONNECT_TIMEOUT_SECS, AGENT_DRIVER_MIN_CONNECT_TIMEOUT_SECS, connectionAttemptOriginalErrorMessage, connectionAttemptTimeoutMessage, connectionAttemptTimeoutMs, CONNECTION_ATTEMPT_TIMEOUT_BUFFER_MS } from "@/lib/connectionAttemptTimeout"; describe("connectionAttemptTimeout", () => { + it("uses a 10s default for regular database connections", () => { + expect(connectionAttemptTimeoutMs({ db_type: "mysql", connect_timeout_secs: undefined, transport_layers: [] })).toBe(12_000); + }); + + it("keeps explicit regular database timeout values", () => { + expect(connectionAttemptTimeoutMs({ db_type: "mysql", connect_timeout_secs: 5, transport_layers: [] })).toBe(7_000); + }); + it("uses a 30s agent startup floor", () => { expect(connectionAttemptTimeoutMs({ db_type: "oracle", connect_timeout_secs: 5, transport_layers: [] })).toBe(AGENT_DRIVER_MIN_CONNECT_TIMEOUT_SECS * 1000 + CONNECTION_ATTEMPT_TIMEOUT_BUFFER_MS); }); @@ -13,4 +21,10 @@ describe("connectionAttemptTimeout", () => { it("respects a user configured Access timeout above the default floor", () => { expect(connectionAttemptTimeoutMs({ db_type: "access", connect_timeout_secs: 45, transport_layers: [] })).toBe(47_000); }); + + it("adds late original database errors to timeout messages", () => { + const timeoutMessage = connectionAttemptTimeoutMessage(7_000); + + expect(connectionAttemptOriginalErrorMessage(timeoutMessage, "MySQL connection failed: os error 10060")).toContain("MySQL connection failed: os error 10060"); + }); }); diff --git a/apps/desktop/src/lib/connectionAttemptTimeout.ts b/apps/desktop/src/lib/connectionAttemptTimeout.ts index 4e8030df10..1b447f94a4 100644 --- a/apps/desktop/src/lib/connectionAttemptTimeout.ts +++ b/apps/desktop/src/lib/connectionAttemptTimeout.ts @@ -4,7 +4,7 @@ export const CONNECTION_ATTEMPT_TIMEOUT_BUFFER_MS = 2_000; export const MONGO_LEGACY_FALLBACK_TIMEOUT_BUFFER_MS = 30_000; export const AGENT_DRIVER_MIN_CONNECT_TIMEOUT_SECS = 30; export const ACCESS_AGENT_MIN_CONNECT_TIMEOUT_SECS = 30; -const DEFAULT_CONNECT_TIMEOUT_SECS = 5; +const DEFAULT_CONNECT_TIMEOUT_SECS = 10; const AGENT_DRIVER_TYPES = new Set([ "dameng", @@ -61,3 +61,9 @@ export function connectionAttemptTimeoutMs(config: Pick { async function withConnectionAttemptTimeout(promise: Promise, config: ConnectionConfig): Promise { const timeoutMs = connectionAttemptTimeoutMs(config); + const timeoutMessage = connectionAttemptTimeoutMessage(timeoutMs); + let timedOut = false; let timer: ReturnType | undefined; + void promise.catch((error) => { + if (!timedOut) return; + const current = connectionErrors.value[config.id]; + if (current !== timeoutMessage) return; + setConnectionError(config.id, connectionAttemptOriginalErrorMessage(timeoutMessage, connectionErrorMessage(error))); + }); try { return await Promise.race([ promise, new Promise((_, reject) => { - timer = setTimeout(() => reject(new Error(connectionAttemptTimeoutMessage(timeoutMs))), timeoutMs); + timer = setTimeout(() => { + timedOut = true; + reject(new Error(timeoutMessage)); + }, timeoutMs); }), ]); } finally { @@ -366,7 +377,7 @@ export const useConnectionStore = defineStore("connection", () => { url_params: config.url_params || "", attached_databases: Array.isArray(config.attached_databases) ? config.attached_databases.filter((database) => database.name?.trim() && database.path?.trim()) : [], transport_layers: Array.isArray(config.transport_layers) ? config.transport_layers : [], - connect_timeout_secs: config.connect_timeout_secs || 5, + connect_timeout_secs: config.connect_timeout_secs || 10, query_timeout_secs: config.query_timeout_secs ?? 30, idle_timeout_secs: config.idle_timeout_secs ?? 60, keepalive_interval_secs: config.keepalive_interval_secs ?? 0, diff --git a/crates/dbx-core/src/models/connection.rs b/crates/dbx-core/src/models/connection.rs index c697bf9391..c78362dc88 100644 --- a/crates/dbx-core/src/models/connection.rs +++ b/crates/dbx-core/src/models/connection.rs @@ -194,7 +194,7 @@ pub fn default_ssh_connect_timeout_secs() -> u64 { } pub fn default_connect_timeout_secs() -> u64 { - 5 + 10 } pub fn default_query_timeout_secs() -> u64 { @@ -1581,6 +1581,24 @@ mod tests { assert!(hops[0].expose_lan); } + #[test] + fn missing_connection_timeout_defaults_to_ten_seconds() { + let config: ConnectionConfig = serde_json::from_value(serde_json::json!({ + "id": "id", + "name": "name", + "db_type": "mysql", + "host": "10.1.2.3", + "port": 3306, + "username": "root", + "password": "", + "database": null + })) + .unwrap(); + + assert_eq!(config.connect_timeout_secs, 10); + assert_eq!(config.effective_connect_timeout_secs(), 10); + } + #[test] fn legacy_ssh_tunnels_migrate_to_ordered_transport_layers() { let config: ConnectionConfig = serde_json::from_value(serde_json::json!({ diff --git a/packages/app-tests/connectionStoreErrorState.test.ts b/packages/app-tests/connectionStoreErrorState.test.ts index 09ad8e5b26..c515aac4b6 100644 --- a/packages/app-tests/connectionStoreErrorState.test.ts +++ b/packages/app-tests/connectionStoreErrorState.test.ts @@ -1,6 +1,6 @@ import assert from "node:assert/strict"; import { createPinia, setActivePinia } from "pinia"; -import { test } from "vitest"; +import { test, vi } from "vitest"; import { useConnectionStore } from "../../apps/desktop/src/stores/connectionStore.ts"; import type { ConnectionConfig } from "../../apps/desktop/src/types/database.ts"; @@ -153,3 +153,39 @@ test("explicit lost-connection marker clears state without relying on error text restoreStorage(); } }); + +test("late original connect errors replace the generic timeout detail", async () => { + vi.useFakeTimers(); + const restoreStorage = installMemoryStorage(); + const originalFetch = globalThis.fetch; + globalThis.fetch = (async (input) => { + if (String(input) === "/api/connection/connect") { + return new Promise((_, reject) => { + setTimeout(() => reject(new Error("MySQL connection failed: raw socket timeout")), 3500); + }); + } + return new Response("unexpected request", { status: 500 }); + }) as typeof fetch; + + try { + setActivePinia(createPinia()); + const store = useConnectionStore(); + const config = { ...conn("conn-1"), connect_timeout_secs: 1 }; + const connectPromise = store.connect(config); + const timeoutRejection = assert.rejects(() => connectPromise, /Connection attempt timed out after 3s/); + + await vi.advanceTimersByTimeAsync(3000); + await timeoutRejection; + assert.equal(store.connectionErrors["conn-1"], "Connection attempt timed out after 3s. Please check the network or VPN and try again."); + + await vi.advanceTimersByTimeAsync(500); + await Promise.resolve(); + + assert.match(store.connectionErrors["conn-1"], /Original database error returned after the UI timeout/); + assert.match(store.connectionErrors["conn-1"], /MySQL connection failed: raw socket timeout/); + } finally { + vi.useRealTimers(); + globalThis.fetch = originalFetch; + restoreStorage(); + } +});