diff --git a/packages/gcp-ai/lib/lock.mjs b/packages/gcp-ai/lib/lock.mjs index 4562c03..eef3681 100644 --- a/packages/gcp-ai/lib/lock.mjs +++ b/packages/gcp-ai/lib/lock.mjs @@ -26,8 +26,15 @@ function alive(pid) { } } -async function abandoned(path) { - const locked = (await stat(path)).mtimeMs +// abandoned says whether the lock at path was left behind by a holder that is +// gone, or null when there is no lock any more: the holder let go between our +// open and this look, and the lock is free to try again (#650). +export async function abandoned(path) { + let locked + try { locked = (await stat(path)).mtimeMs } catch (error) { + if (error?.code === "ENOENT") return null + throw error + } let pid try { pid = Number((await readFile(path, "utf8")).trim()) } catch { return Date.now() - locked > 10000 } // An empty lock is one just created by another process, not one abandoned: @@ -76,7 +83,11 @@ export async function take(file, { timeout = 180000, poll = 250, busy = "Another } } catch (error) { if (error.code !== "EEXIST") throw error - if (await abandoned(path)) { await rm(path, { force: true }); continue } + const left = await abandoned(path) + // Gone already: open again, and remove nothing, since by now the file + // may be a new holder's. + if (left === null) continue + if (left) { await rm(path, { force: true }); continue } // Say that someone is waiting, so a holder that is only keeping the lock // warm lets go now instead of making this process sit out its leisure. await writeFile(`${file}.want`, String(process.pid), { mode: 0o600 }).catch(() => {}) diff --git a/packages/gcp-ai/package.json b/packages/gcp-ai/package.json index 251f511..0446d8b 100644 --- a/packages/gcp-ai/package.json +++ b/packages/gcp-ai/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-gcp-ai-auth", - "version": "0.1.3", + "version": "0.1.4", "description": "Vertex AI models and Google Cloud promotional credits, signed with your own gcloud / ADC login", "type": "module", "main": "./opencode.mjs", diff --git a/packages/gcp-ai/test/lock.test.mjs b/packages/gcp-ai/test/lock.test.mjs index 2bf4737..2935177 100644 --- a/packages/gcp-ai/test/lock.test.mjs +++ b/packages/gcp-ai/test/lock.test.mjs @@ -3,7 +3,7 @@ import test from "node:test" import { mkdtemp, readFile, rm, utimes, writeFile } from "node:fs/promises" import { tmpdir } from "node:os" import { join } from "node:path" -import { apart, take } from "../lib/lock.mjs" +import { abandoned, apart, take } from "../lib/lock.mjs" // Two accounts on one Chrome profile are read one after the other, and magpie // may run each account's read in a process of its own — so the lock that keeps @@ -102,3 +102,12 @@ test("Linux keeps a live holder even when its lock is over fifteen minutes old", await rm(directory, { recursive: true, force: true }) } }) + +// #650: a holder can let go between a waiter's failed open and its look at the +// lock. A lock that is gone by then is free, not an error for the sign-in. +test("a lock released before it is looked at is free, not an error", async () => { + const directory = await mkdtemp(join(tmpdir(), "vertex-lock-")) + try { + assert.equal(await abandoned(join(directory, "balance.lock")), null) + } finally { await rm(directory, { recursive: true, force: true }) } +})