fix(cli): close direct query resources after command execution

Fix #745
This commit is contained in:
Scars authored and GitHub committed 2026-06-05 14:56:55 +08:00
1 parent 249a1ed649
commit 6016b293ce
4 files changed
+92 -3

No files matched your search

+6 -1
View File
@@ -24,6 +24,7 @@ export interface CliResult {
interface RunOptions {
backend?: Backend;
backendFactory?: (env?: NodeJS.ProcessEnv) => Promise<Backend>;
env?: NodeJS.ProcessEnv;
diagnostics?: () => Promise<DbxDiagnostics>;
}
@@ -56,6 +57,7 @@ class CliError extends Error {
export async function runCli(argv: string[], options: RunOptions = {}): Promise<CliResult> {
const env = options.env ?? process.env;
let ownedBackend: Backend | undefined;
try {
const flags = parseFlags(argv);
@@ -69,7 +71,8 @@ export async function runCli(argv: string[], options: RunOptions = {}): Promise<
return ok(`${usage()}\n`);
}
const backend = options.backend ?? (await createBackend(env));
const backendFactory = options.backendFactory ?? createBackend;
const backend = options.backend ?? (ownedBackend = await backendFactory(env));
if (args[0] === "doctor") {
ensureArgCount(args, 1, "dbx doctor");
@@ -242,6 +245,8 @@ export async function runCli(argv: string[], options: RunOptions = {}): Promise<
: "ERROR";
const wantsJson = argv.includes("--json");
return fail(code, message, wantsJson);
} finally {
await ownedBackend?.close?.().catch(() => {});
}
}