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.
This commit is contained in:
@@ -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(() => {})
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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 }) }
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user