feat(connection): add connection attempt cancellation

This commit is contained in:
Moe. authored and GitHub committed 2026-07-04 23:47:53 +08:00
1 parent 454457f191
commit 8a1a791927
16 files changed
+681 -51

No files matched your search

@@ -63,7 +63,7 @@ describe("connectionStore timeout recovery", () => {
await ensure;
expect(checkConnectionHealth).toHaveBeenCalledWith(connection.id);
expect(connectDb).toHaveBeenCalledWith(connection);
expect(connectDb).toHaveBeenCalledWith(connection, expect.any(Number));
expect(store.connectedIds.has(connection.id)).toBe(true);
}, 10_000);
@@ -117,4 +117,217 @@ describe("connectionStore timeout recovery", () => {
expect(error).toBeInstanceOf(Error);
expect(node.isLoading).toBe(false);
}, 10_000);
it("cancels an in-flight connection without leaving connected or loading state", async () => {
const connectDb = vi.fn(() => new Promise(() => undefined));
const disconnectDb = vi.fn().mockResolvedValue(undefined);
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
vi.doMock("@/lib/backend/api", () => ({
connectDb,
deleteSchemaCachePrefix: vi.fn().mockResolvedValue(undefined),
disconnectDb,
saveConnections: vi.fn().mockResolvedValue(undefined),
saveSidebarLayout: vi.fn().mockResolvedValue(undefined),
}));
const { CONNECTION_ATTEMPT_CANCELLED_MESSAGE, useConnectionStore } = await import("@/stores/connectionStore");
const store = useConnectionStore();
const connection = postgresConnection({ connect_timeout_secs: 1 });
const node: TreeNode = {
id: connection.id,
label: connection.name,
type: "connection",
connectionId: connection.id,
isLoading: false,
children: [],
};
store.connections = [connection];
store.treeNodes = [node];
const ensure = store.ensureConnected(connection.id).catch((error) => error);
await vi.advanceTimersByTimeAsync(1);
expect(store.connectingIds.has(connection.id)).toBe(true);
expect(node.isLoading).toBe(true);
await expect(store.cancelConnecting(connection.id)).resolves.toBe(true);
expect(disconnectDb).toHaveBeenCalledWith(connection.id, expect.any(Number));
expect(store.connectingIds.has(connection.id)).toBe(false);
expect(store.connectedIds.has(connection.id)).toBe(false);
expect(store.connectionErrors[connection.id]).toBeUndefined();
expect(node.isLoading).toBe(false);
await vi.advanceTimersByTimeAsync(3001);
const error = await ensure;
expect(error).toBeInstanceOf(Error);
expect(error.message).toContain(CONNECTION_ATTEMPT_CANCELLED_MESSAGE);
expect(store.connectingIds.has(connection.id)).toBe(false);
expect(store.connectedIds.has(connection.id)).toBe(false);
expect(store.connectionErrors[connection.id]).toBeUndefined();
expect(node.isLoading).toBe(false);
}, 10_000);
it("allows reconnecting the same connection while a scoped cancel is pending", async () => {
let resolveDisconnect!: () => void;
const pendingConnect = new Promise<string>(() => undefined);
let connectCallCount = 0;
const connectDb = vi.fn(() => {
connectCallCount += 1;
return connectCallCount === 1 ? pendingConnect : Promise.resolve("pg-1");
});
const disconnectDb = vi.fn(
() =>
new Promise<void>((resolve) => {
resolveDisconnect = resolve;
}),
);
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
vi.doMock("@/lib/backend/api", () => ({
connectDb,
deleteSchemaCachePrefix: vi.fn().mockResolvedValue(undefined),
disconnectDb,
saveConnections: vi.fn().mockResolvedValue(undefined),
saveSidebarLayout: vi.fn().mockResolvedValue(undefined),
}));
const { useConnectionStore } = await import("@/stores/connectionStore");
const store = useConnectionStore();
const connection = postgresConnection({ connect_timeout_secs: 1 });
store.connections = [connection];
store.treeNodes = [
{
id: connection.id,
label: connection.name,
type: "connection",
connectionId: connection.id,
isLoading: false,
children: [],
},
];
const firstEnsure = store.ensureConnected(connection.id).catch((error) => error);
await vi.advanceTimersByTimeAsync(1);
expect(connectDb).toHaveBeenCalledTimes(1);
const firstAttempt = connectDb.mock.calls[0]?.[1];
const cancel = store.cancelConnecting(connection.id);
await vi.advanceTimersByTimeAsync(1);
expect(disconnectDb).toHaveBeenCalledWith(connection.id, firstAttempt);
const reconnect = store.ensureConnected(connection.id);
await vi.advanceTimersByTimeAsync(1);
expect(connectDb).toHaveBeenCalledTimes(2);
expect(connectDb.mock.calls[1]?.[1]).not.toBe(firstAttempt);
resolveDisconnect();
await cancel;
await reconnect;
expect(connectDb).toHaveBeenCalledTimes(2);
expect(store.connectedIds.has(connection.id)).toBe(true);
expect(store.connectionErrors[connection.id]).toBeUndefined();
await vi.advanceTimersByTimeAsync(3001);
await firstEnsure;
}, 10_000);
it("cleans up backend state when a cancelled connection later succeeds", async () => {
let resolveConnect!: (connectionId: string) => void;
const connectDb = vi.fn(
() =>
new Promise<string>((resolve) => {
resolveConnect = resolve;
}),
);
const disconnectDb = vi.fn().mockResolvedValue(undefined);
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
vi.doMock("@/lib/backend/api", () => ({
connectDb,
deleteSchemaCachePrefix: vi.fn().mockResolvedValue(undefined),
disconnectDb,
saveConnections: vi.fn().mockResolvedValue(undefined),
saveSidebarLayout: vi.fn().mockResolvedValue(undefined),
}));
const { CONNECTION_ATTEMPT_CANCELLED_MESSAGE, useConnectionStore } = await import("@/stores/connectionStore");
const store = useConnectionStore();
const connection = postgresConnection({ connect_timeout_secs: 10 });
store.connections = [connection];
const ensure = store.ensureConnected(connection.id).catch((error) => error);
await vi.advanceTimersByTimeAsync(1);
const attempt = connectDb.mock.calls[0]?.[1];
await expect(store.cancelConnecting(connection.id)).resolves.toBe(true);
expect(disconnectDb).toHaveBeenCalledTimes(1);
expect(disconnectDb).toHaveBeenCalledWith(connection.id, attempt);
resolveConnect(connection.id);
await vi.advanceTimersByTimeAsync(1);
const error = await ensure;
expect(error).toBeInstanceOf(Error);
expect(error.message).toContain(CONNECTION_ATTEMPT_CANCELLED_MESSAGE);
expect(disconnectDb).toHaveBeenCalledTimes(2);
expect(disconnectDb).toHaveBeenLastCalledWith(connection.id, attempt);
expect(store.connectedIds.has(connection.id)).toBe(false);
expect(store.connectionErrors[connection.id]).toBeUndefined();
}, 10_000);
it("keeps errors from earlier cancelled attempts hidden after a second cancel", async () => {
let rejectFirstConnect!: (error: Error) => void;
let rejectSecondConnect!: (error: Error) => void;
let connectCallCount = 0;
const connectDb = vi.fn(() => {
connectCallCount += 1;
return new Promise<string>((_, reject) => {
if (connectCallCount === 1) {
rejectFirstConnect = reject;
} else {
rejectSecondConnect = reject;
}
});
});
const disconnectDb = vi.fn().mockResolvedValue(undefined);
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
vi.doMock("@/lib/backend/api", () => ({
connectDb,
deleteSchemaCachePrefix: vi.fn().mockResolvedValue(undefined),
disconnectDb,
saveConnections: vi.fn().mockResolvedValue(undefined),
saveSidebarLayout: vi.fn().mockResolvedValue(undefined),
}));
const { CONNECTION_ATTEMPT_CANCELLED_MESSAGE, useConnectionStore } = await import("@/stores/connectionStore");
const store = useConnectionStore();
const connection = postgresConnection({ connect_timeout_secs: 10 });
store.connections = [connection];
const firstEnsure = store.ensureConnected(connection.id).catch((error) => error);
await vi.advanceTimersByTimeAsync(1);
expect(connectDb).toHaveBeenCalledTimes(1);
await expect(store.cancelConnecting(connection.id)).resolves.toBe(true);
const secondEnsure = store.ensureConnected(connection.id).catch((error) => error);
await vi.advanceTimersByTimeAsync(1);
expect(connectDb).toHaveBeenCalledTimes(2);
await expect(store.cancelConnecting(connection.id)).resolves.toBe(true);
rejectFirstConnect(new Error("first connection failed after cancel"));
const firstError = await firstEnsure;
expect(firstError).toBeInstanceOf(Error);
expect(firstError.message).toContain(CONNECTION_ATTEMPT_CANCELLED_MESSAGE);
expect(store.connectionErrors[connection.id]).toBeUndefined();
rejectSecondConnect(new Error("second connection failed after cancel"));
const secondError = await secondEnsure;
expect(secondError).toBeInstanceOf(Error);
expect(secondError.message).toContain(CONNECTION_ATTEMPT_CANCELLED_MESSAGE);
expect(store.connectionErrors[connection.id]).toBeUndefined();
}, 10_000);
});
+203 -8
View File
@@ -174,6 +174,8 @@ interface PersistedTreeChildrenLoadResult {
type BeforeConnectHandler = (config: ConnectionConfig) => Promise<void>;
export const CONNECTION_ATTEMPT_CANCELLED_MESSAGE = "Connection attempt was cancelled";
function redisDbLabel(db: number, _loadedKeyCount?: number, totalKeyCount?: number): string {
if (totalKeyCount == null) return `db${db}`;
return `db${db} (${totalKeyCount})`;
@@ -202,6 +204,7 @@ export const useConnectionStore = defineStore("connection", () => {
let agentDriversRefreshPromise: Promise<void> | null = null;
const loadedTreeNodeChildrenIds = ref<Set<string>>(new Set());
const connectionErrors = ref<Record<string, string>>({});
const connectingIds = ref<Set<string>>(new Set());
const editingConnectionId = ref<string | null>(null);
const newConnectionGroupId = ref<string | null>(null);
const completionTablesCache = ref<Record<string, SqlCompletionTable[]>>({});
@@ -274,6 +277,11 @@ export const useConnectionStore = defineStore("connection", () => {
let layoutPersistTimer: ReturnType<typeof setTimeout> | null = null;
const staleTreeRefreshIds = new Set<string>();
const connectInFlight = new Map<string, Promise<void>>();
const disconnectInFlight = new Map<string, Promise<void>>();
const cancelDisconnectInFlight = new Map<string, Promise<void>>();
const activeLocalConnectionAttempts = new Map<string, number>();
const cancelledLocalConnectionAttempts = new Map<string, Set<number>>();
let nextLocalConnectionAttempt = 0;
let beforeConnectHandler: BeforeConnectHandler | null = null;
let initFromDiskPromise: Promise<void> | null = null;
@@ -309,6 +317,146 @@ export const useConnectionStore = defineStore("connection", () => {
return connectionErrorMessage(error).includes(SUPERSEDED_CONNECTION_ATTEMPT_MESSAGE);
}
function isCancelledConnectionAttempt(error: unknown): boolean {
return connectionErrorMessage(error).includes(CONNECTION_ATTEMPT_CANCELLED_MESSAGE);
}
function beginLocalConnectionAttempt(connectionId: string): number {
const attempt = ++nextLocalConnectionAttempt;
activeLocalConnectionAttempts.set(connectionId, attempt);
connectingIds.value.add(connectionId);
const node = findNode(treeNodes.value, connectionId);
if (node) node.isLoading = true;
return attempt;
}
function isCurrentLocalConnectionAttempt(connectionId: string, attempt: number): boolean {
return activeLocalConnectionAttempts.get(connectionId) === attempt;
}
function isCancelledLocalConnectionAttempt(connectionId: string, attempt: number): boolean {
return cancelledLocalConnectionAttempts.get(connectionId)?.has(attempt) === true;
}
function getLocalConnectionAttempt(connectionId: string): number | undefined {
return activeLocalConnectionAttempts.get(connectionId);
}
function finishLocalConnectionAttempt(connectionId: string, attempt: number) {
if (isCancelledLocalConnectionAttempt(connectionId, attempt)) {
const attempts = cancelledLocalConnectionAttempts.get(connectionId);
attempts?.delete(attempt);
if (attempts?.size === 0) {
cancelledLocalConnectionAttempts.delete(connectionId);
}
}
if (!isCurrentLocalConnectionAttempt(connectionId, attempt)) return;
activeLocalConnectionAttempts.delete(connectionId);
connectingIds.value.delete(connectionId);
clearConnectionNodeLoading(connectionId);
}
function cancelLocalConnectionAttempt(connectionId: string): boolean {
const attempt = activeLocalConnectionAttempts.get(connectionId);
if (attempt == null) return false;
const attempts = cancelledLocalConnectionAttempts.get(connectionId) ?? new Set<number>();
attempts.add(attempt);
cancelledLocalConnectionAttempts.set(connectionId, attempts);
activeLocalConnectionAttempts.delete(connectionId);
connectingIds.value.delete(connectionId);
clearConnectionNodeLoading(connectionId);
connectInFlight.delete(connectionId);
return true;
}
function getDisconnectInFlight(connectionId: string): Promise<void> | undefined {
return disconnectInFlight.get(connectionId);
}
async function waitForDisconnectInFlight(connectionId: string): Promise<void> {
const pending = getDisconnectInFlight(connectionId);
if (pending) await pending;
}
function trackDisconnectRequest(connectionId: string, request: Promise<void>): Promise<void> {
const tracked = request
.catch((error) => {
console.warn("[DBX][connection:disconnect-error]", { connectionId, error });
})
.finally(() => {
if (disconnectInFlight.get(connectionId) === tracked) {
disconnectInFlight.delete(connectionId);
}
});
disconnectInFlight.set(connectionId, tracked);
return withDisconnectRequestTimeout(connectionId, request);
}
function startDisconnectRequest(connectionId: string): Promise<void> {
let request: Promise<void>;
try {
request = api.disconnectDb(connectionId);
} catch (error) {
request = Promise.reject(error);
}
return trackDisconnectRequest(connectionId, request);
}
function cancelDisconnectKey(connectionId: string, attempt: number): string {
return `${connectionId}:${attempt}`;
}
function startCancelDisconnectRequest(connectionId: string, attempt: number): Promise<void> {
const key = cancelDisconnectKey(connectionId, attempt);
const existing = cancelDisconnectInFlight.get(key);
if (existing) return existing;
let request: Promise<void>;
try {
request = api.disconnectDb(connectionId, attempt);
} catch (error) {
request = Promise.reject(error);
}
const tracked = withDisconnectRequestTimeout(connectionId, request)
.catch((error) => {
console.warn("[DBX][connection:cancel-disconnect-error]", { connectionId, attempt, error });
throw error;
})
.finally(() => {
if (cancelDisconnectInFlight.get(key) === tracked) {
cancelDisconnectInFlight.delete(key);
}
});
cancelDisconnectInFlight.set(key, tracked);
return tracked;
}
async function cleanupResolvedCancelledConnectionAttempt(connectionId: string, attempt: number) {
try {
// A cancel request can reach the backend before connect_db registers the
// attempt, so clean again if that cancelled connect later returns a pool.
await withDisconnectRequestTimeout(connectionId, api.disconnectDb(connectionId, attempt));
} catch (error) {
console.warn("[DBX][connection:cancel-result-cleanup-error]", { connectionId, attempt, error });
}
}
async function ensureLocalConnectionAttemptActiveAfterConnectResult(connectionId: string, attempt: number, cleanupConnectionId: string) {
if (isCancelledLocalConnectionAttempt(connectionId, attempt)) {
await cleanupResolvedCancelledConnectionAttempt(cleanupConnectionId, attempt);
throw new Error(CONNECTION_ATTEMPT_CANCELLED_MESSAGE);
}
ensureLocalConnectionAttemptActive(connectionId, attempt);
}
function ensureLocalConnectionAttemptActive(connectionId: string, attempt: number) {
if (isCancelledLocalConnectionAttempt(connectionId, attempt)) {
throw new Error(CONNECTION_ATTEMPT_CANCELLED_MESSAGE);
}
if (!isCurrentLocalConnectionAttempt(connectionId, attempt)) {
throw new Error(SUPERSEDED_CONNECTION_ATTEMPT_MESSAGE);
}
}
function setConnectionError(connectionId: string, message: string) {
connectionErrors.value[connectionId] = message;
}
@@ -442,6 +590,10 @@ export const useConnectionStore = defineStore("connection", () => {
function recordConnectionError(connectionId: string, error: unknown): string {
const message = connectionErrorMessage(error);
if (isCancelledConnectionAttempt(message)) {
clearConnectionError(connectionId);
return "";
}
setConnectionError(connectionId, message);
maybeAppendAgentDriverUpdateHint(connectionId, message);
return message;
@@ -1318,12 +1470,15 @@ export const useConnectionStore = defineStore("connection", () => {
async function connect(config: ConnectionConfig) {
config = normalizeConnection(config);
const pendingNode = findNode(treeNodes.value, config.id);
if (pendingNode) pendingNode.isLoading = true;
if (getDisconnectInFlight(config.id)) await waitForDisconnectInFlight(config.id);
const localAttempt = beginLocalConnectionAttempt(config.id);
try {
await beforeConnectHandler?.(config);
const id = await withConnectionAttemptTimeout(api.connectDb(config), config);
ensureLocalConnectionAttemptActive(config.id, localAttempt);
const id = await withConnectionAttemptTimeout(api.connectDb(config, localAttempt), config);
await ensureLocalConnectionAttemptActiveAfterConnectResult(config.id, localAttempt, id);
await syncMongoLegacyDriverFallback(id, config);
await ensureLocalConnectionAttemptActiveAfterConnectResult(config.id, localAttempt, id);
activeConnectionId.value = id;
connectedIds.value.add(id);
markConnectionHealthChecked(id);
@@ -1348,17 +1503,41 @@ export const useConnectionStore = defineStore("connection", () => {
}
return id;
} catch (e) {
recordConnectionError(config.id, e);
if (isCancelledLocalConnectionAttempt(config.id, localAttempt)) {
clearConnectionError(config.id);
throw new Error(CONNECTION_ATTEMPT_CANCELLED_MESSAGE);
}
if (isCancelledConnectionAttempt(e) || isSupersededConnectionAttempt(e)) {
clearConnectionError(config.id);
} else {
recordConnectionError(config.id, e);
}
throw e;
} finally {
const node = findNode(treeNodes.value, config.id);
if (node) node.isLoading = false;
finishLocalConnectionAttempt(config.id, localAttempt);
}
}
async function cancelConnecting(connectionId: string): Promise<boolean> {
const localAttempt = getLocalConnectionAttempt(connectionId);
if (localAttempt == null) return false;
const disconnectRequest = startCancelDisconnectRequest(connectionId, localAttempt);
const cancelled = cancelLocalConnectionAttempt(connectionId);
if (!cancelled) return false;
clearConnectionError(connectionId);
connectedIds.value.delete(connectionId);
clearConnectionHealthCheck(connectionId);
if (activeConnectionId.value === connectionId) activeConnectionId.value = null;
invalidateCompletionCache(connectionId);
await disconnectRequest;
return true;
}
async function disconnect(connectionId: string) {
const disconnectRequest = startDisconnectRequest(connectionId);
cancelLocalConnectionAttempt(connectionId);
const shouldRemoveOneTimeConnection = getConfig(connectionId)?.one_time === true;
await withDisconnectRequestTimeout(connectionId, api.disconnectDb(connectionId));
await disconnectRequest;
clearConnectionError(connectionId);
const { useQueryStore } = await import("@/stores/queryStore");
const queryStore = useQueryStore();
@@ -1440,15 +1619,20 @@ export const useConnectionStore = defineStore("connection", () => {
recordConnectionError(connectionId, error);
throw error;
}
if (getDisconnectInFlight(connectionId)) await waitForDisconnectInFlight(connectionId);
const existingConnect = connectInFlight.get(connectionId);
if (existingConnect) {
await existingConnect;
return;
}
const localAttempt = beginLocalConnectionAttempt(connectionId);
const connectPromise = (async () => {
await beforeConnectHandler?.(config);
await withConnectionAttemptTimeout(api.connectDb(config), config);
ensureLocalConnectionAttemptActive(connectionId, localAttempt);
const id = await withConnectionAttemptTimeout(api.connectDb(config, localAttempt), config);
await ensureLocalConnectionAttemptActiveAfterConnectResult(connectionId, localAttempt, id);
await syncMongoLegacyDriverFallback(connectionId, config);
await ensureLocalConnectionAttemptActiveAfterConnectResult(connectionId, localAttempt, id);
connectedIds.value.add(connectionId);
markConnectionHealthChecked(connectionId);
activeConnectionId.value = connectionId;
@@ -1458,6 +1642,14 @@ export const useConnectionStore = defineStore("connection", () => {
try {
await connectPromise;
} catch (e) {
if (isCancelledLocalConnectionAttempt(connectionId, localAttempt)) {
clearConnectionError(connectionId);
throw new Error(CONNECTION_ATTEMPT_CANCELLED_MESSAGE);
}
if (isCancelledConnectionAttempt(e)) {
clearConnectionError(connectionId);
throw e;
}
if (isSupersededConnectionAttempt(e) && connectedIds.value.has(connectionId)) {
clearConnectionError(connectionId);
return;
@@ -1469,6 +1661,7 @@ export const useConnectionStore = defineStore("connection", () => {
if (connectInFlight.get(connectionId) === connectPromise) {
connectInFlight.delete(connectionId);
}
finishLocalConnectionAttempt(connectionId, localAttempt);
}
}
@@ -4015,6 +4208,7 @@ export const useConnectionStore = defineStore("connection", () => {
refreshDatabaseTreeNode,
refreshObjectListTreeNode,
connectedIds,
connectingIds,
connectionErrors,
setConnectionError,
clearConnectionError,
@@ -4046,6 +4240,7 @@ export const useConnectionStore = defineStore("connection", () => {
startCreatingConnectionInGroup,
stopCreatingConnectionInGroup,
connect,
cancelConnecting,
disconnect,
closeDatabaseConnection,
ensureConnected,