From 4586f8623b94db75a8cf11dedc5cf8d94716bca4 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 3 Nov 2026 15:48:42 +0800 Subject: [PATCH] gcp-ai 0.1.4: a sign-in waiting on a lock its holder just let go takes it, instead of failing with ENOENT (#650) take() opened the lock with "wx", got EEXIST, then stat'ed it in abandoned(). When the holder released between the two, stat threw ENOENT and the second concurrent sign-in failed with it (TestConcurrentSignIns on CI). abandoned() now returns null for a lock that is gone, and take() opens again without removing anything, since the path may already be a new holder's. Run on mote (bun 1.4.2, node 22), HOME=$(mktemp -d): - packages/gcp-ai node --test: 66 pass, 0 fail; under bun test 66 pass, 10 skip. - Broken by hand (ENOENT catch removed): the new lock test goes red. - The concurrent sign-ins test 30 times: 30 green. - Full bun test 5164 pass, 15 skip, 0 fail; scripts/check.mjs ok. Not tried: the original race itself is timing-bound and was not reproduced on demand; the unit test covers the gone-lock path directly. --- packages/gcp-ai/lib/lock.mjs | 17 ++++++++++++++--- packages/gcp-ai/package.json | 2 +- packages/gcp-ai/test/lock.test.mjs | 11 ++++++++++- 3 files changed, 25 insertions(+), 5 deletions(-) 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 }) } +})