Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

Fix CLI hanging

Dillon DuPont789b7264b579
diff --git a/libs/typescript/cua-cli/src/auth.ts b/libs/typescript/cua-cli/src/auth.ts
index b3659b895a..e46f4e6ba4 100644
--- a/libs/typescript/cua-cli/src/auth.ts
+++ b/libs/typescript/cua-cli/src/auth.ts
@@ -12,7 +12,6 @@ const c = {
yellow: "\x1b[33m",
};
-// openInBrowser is imported from util
export async function loginViaBrowser(): Promise<string> {
let resolveToken!: (v: string) => void;
@@ -40,9 +39,17 @@ export async function loginViaBrowser(): Promise<string> {
console.log(`${c.yellow}${c.underline}${url}${c.reset}`);
await openInBrowser(url);
- const timeout = new Promise<string>((_, reject) => setTimeout(() => reject(new Error("Timed out waiting for authorization")), 2 * 60 * 1000));
- try { return await Promise.race([tokenPromise, timeout]); }
- finally { try { server.stop(); } catch {} }
+ let timeoutId: ReturnType<typeof setTimeout> | undefined;
+ const timeout = new Promise<string>((_, reject) => {
+ timeoutId = setTimeout(() => reject(new Error("Timed out waiting for authorization")), 2 * 60 * 1000);
+ });
+ try {
+ const result = await Promise.race([tokenPromise, timeout]);
+ if (timeoutId) clearTimeout(timeoutId);
+ return result;
+ } finally {
+ try { server.stop(); } catch {}
+ }
}
export async function ensureApiKeyInteractive(): Promise<string> {
diff --git a/libs/typescript/cua-cli/src/storage.ts b/libs/typescript/cua-cli/src/storage.ts
index d2c4fa5695..15f683089d 100644
--- a/libs/typescript/cua-cli/src/storage.ts
+++ b/libs/typescript/cua-cli/src/storage.ts
@@ -10,17 +10,29 @@ function getDb(): Database {
export function setApiKey(token: string) {
const db = getDb();
- const stmt = db.query("INSERT INTO kv (k, v) VALUES ('api_key', ?) ON CONFLICT(k) DO UPDATE SET v=excluded.v");
- stmt.run(token);
+ try {
+ const stmt = db.query("INSERT INTO kv (k, v) VALUES ('api_key', ?) ON CONFLICT(k) DO UPDATE SET v=excluded.v");
+ stmt.run(token);
+ } finally {
+ db.close();
+ }
}
export function getApiKey(): string | null {
const db = getDb();
- const row = db.query("SELECT v FROM kv WHERE k='api_key'").get() as { v: string } | undefined;
- return row?.v ?? null;
+ try {
+ const row = db.query("SELECT v FROM kv WHERE k='api_key'").get() as { v: string } | undefined;
+ return row?.v ?? null;
+ } finally {
+ db.close();
+ }
}
export function clearApiKey() {
const db = getDb();
- db.query("DELETE FROM kv WHERE k='api_key'").run();
+ try {
+ db.query("DELETE FROM kv WHERE k='api_key'").run();
+ } finally {
+ db.close();
+ }
}