From a786a3cf0965448cefb503241abaebbb1e8905ea Mon Sep 17 00:00:00 2001 From: Kyle Sexton <153232337+kyle-sexton@users.noreply.github.com> Date: Sat, 21 Nov 2026 18:15:17 -0500 Subject: [PATCH] fix(lib): read the clock under the lock in cli-version-check claimDaily claimDaily took `now` from a default parameter, before the lock. A claimant that read the clock, then took the lock only after another claimant had stamped a later time, saw that stamp as a future one and claimed again, so two of 24 concurrent claims won on CI (main run 44950479481, attempt 1). The clock is now read inside the lock, and checkVersion no longer captures it early either. A regression test lands a claim between the clock read and the lock and expects the late claimant to lose. Closes #18063 Co-Authored-By: Claude Opus 5.5 --- ...x-18063-cli-version-claim-race-8077c651.md | 7 +++++ ...x-18063-cli-version-claim-race-93866a48.md | 7 +++++ ...x-18063-cli-version-claim-race-30e9f311.md | 7 +++++ lib/cli-version-check.mjs | 5 ++-- lib/cli-version-check.test.mjs | 27 ++++++++++++++++++- plugins/context7/lib/cli-version-check.mjs | 5 ++-- plugins/firecrawl/lib/cli-version-check.mjs | 5 ++-- plugins/playwright/lib/cli-version-check.mjs | 5 ++-- 8 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 .changes/context7/fix-18063-cli-version-claim-race-8077c651.md create mode 100644 .changes/firecrawl/fix-18063-cli-version-claim-race-93866a48.md create mode 100644 .changes/playwright/fix-18063-cli-version-claim-race-30e9f311.md diff --git a/.changes/context7/fix-18063-cli-version-claim-race-8077c651.md b/.changes/context7/fix-18063-cli-version-claim-race-8077c651.md new file mode 100644 index 00000000000..1716d0b9969 --- /dev/null +++ b/.changes/context7/fix-18063-cli-version-claim-race-8077c651.md @@ -0,0 +1,7 @@ +--- +bump: patch +--- + +### Fixed + +- The once-a-day CLI version check no longer runs twice when two sessions start at the same moment: the shared `cli-version-check.mjs` now reads the clock after it takes its lock ([#18063](https://github.com/melodic-software/claude-code-plugins/issues/18063)). diff --git a/.changes/firecrawl/fix-18063-cli-version-claim-race-93866a48.md b/.changes/firecrawl/fix-18063-cli-version-claim-race-93866a48.md new file mode 100644 index 00000000000..1716d0b9969 --- /dev/null +++ b/.changes/firecrawl/fix-18063-cli-version-claim-race-93866a48.md @@ -0,0 +1,7 @@ +--- +bump: patch +--- + +### Fixed + +- The once-a-day CLI version check no longer runs twice when two sessions start at the same moment: the shared `cli-version-check.mjs` now reads the clock after it takes its lock ([#18063](https://github.com/melodic-software/claude-code-plugins/issues/18063)). diff --git a/.changes/playwright/fix-18063-cli-version-claim-race-30e9f311.md b/.changes/playwright/fix-18063-cli-version-claim-race-30e9f311.md new file mode 100644 index 00000000000..1716d0b9969 --- /dev/null +++ b/.changes/playwright/fix-18063-cli-version-claim-race-30e9f311.md @@ -0,0 +1,7 @@ +--- +bump: patch +--- + +### Fixed + +- The once-a-day CLI version check no longer runs twice when two sessions start at the same moment: the shared `cli-version-check.mjs` now reads the clock after it takes its lock ([#18063](https://github.com/melodic-software/claude-code-plugins/issues/18063)). diff --git a/lib/cli-version-check.mjs b/lib/cli-version-check.mjs index 330280bcb76..4a728b86fa3 100644 --- a/lib/cli-version-check.mjs +++ b/lib/cli-version-check.mjs @@ -52,9 +52,10 @@ function exclusively(file, claim) { return false; } -export function claimDaily({ stateDir, name, now = Date.now() }) { +export function claimDaily({ stateDir, name, now }) { const file = machineStateFile(stateDir, name); return exclusively(file, () => { + now ??= Date.now(); const last = lastClaim(file); if (last <= now && now - last < DAY_MS) return false; const tmp = `${file}.${process.pid}.tmp`; @@ -86,7 +87,7 @@ function release(source) { throw new Error(`invalid source: ${source} (expected npm: or github:/)`); } -export async function checkVersion({ stateDir, cli, installed, source, hint = "", now = Date.now(), fetch = globalThis.fetch, timeoutMs = 3000 }) { +export async function checkVersion({ stateDir, cli, installed, source, hint = "", now, fetch = globalThis.fetch, timeoutMs = 3000 }) { const { url, field } = release(source); // Claimed before the lookup so a failed one also waits a day: unauthenticated api.github.com allows 60 requests an hour per IP, https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api if (!claimDaily({ stateDir, name: cli, now })) return { status: "skipped" }; diff --git a/lib/cli-version-check.test.mjs b/lib/cli-version-check.test.mjs index ca9f99e8b53..9a228f8b5cf 100644 --- a/lib/cli-version-check.test.mjs +++ b/lib/cli-version-check.test.mjs @@ -1,6 +1,7 @@ import { strict as assert } from "node:assert"; import { spawn, spawnSync } from "node:child_process"; -import { existsSync, mkdirSync, utimesSync, writeFileSync } from "node:fs"; +import fs, { existsSync, mkdirSync, utimesSync, writeFileSync } from "node:fs"; +import { syncBuiltinESMExports } from "node:module"; import { dirname, join } from "node:path"; import { test } from "node:test"; import { fileURLToPath } from "node:url"; @@ -135,6 +136,30 @@ test("only one of many processes claiming the same name at once wins", async () assert.equal(codes.filter((code) => code === 1).length, 23); }); +test("a claim that lands while another claimant is on its way to the lock makes that claimant lose", () => { + const dir = tempDir("cvc-"); + const original = fs.mkdirSync; + const restore = () => { + fs.mkdirSync = original; + syncBuiltinESMExports(); + }; + let inner; + fs.mkdirSync = (...args) => { + restore(); + const until = Date.now() + 2; + while (Date.now() < until); + inner = claimDaily({ stateDir: dir, name: "race" }); + return original(...args); + }; + syncBuiltinESMExports(); + try { + const outer = claimDaily({ stateDir: dir, name: "race" }); + assert.deepEqual({ inner, outer }, { inner: true, outer: false }); + } finally { + restore(); + } +}); + test("a fresh lock held by another claimant refuses; a stale one is broken", () => { const dir = tempDir("cvc-"); const lock = join(dir, "cli-version-check", "agy.json.lock"); diff --git a/plugins/context7/lib/cli-version-check.mjs b/plugins/context7/lib/cli-version-check.mjs index 2863e96e9c9..1339142c117 100644 --- a/plugins/context7/lib/cli-version-check.mjs +++ b/plugins/context7/lib/cli-version-check.mjs @@ -54,9 +54,10 @@ function exclusively(file, claim) { return false; } -export function claimDaily({ stateDir, name, now = Date.now() }) { +export function claimDaily({ stateDir, name, now }) { const file = machineStateFile(stateDir, name); return exclusively(file, () => { + now ??= Date.now(); const last = lastClaim(file); if (last <= now && now - last < DAY_MS) return false; const tmp = `${file}.${process.pid}.tmp`; @@ -88,7 +89,7 @@ function release(source) { throw new Error(`invalid source: ${source} (expected npm: or github:/)`); } -export async function checkVersion({ stateDir, cli, installed, source, hint = "", now = Date.now(), fetch = globalThis.fetch, timeoutMs = 3000 }) { +export async function checkVersion({ stateDir, cli, installed, source, hint = "", now, fetch = globalThis.fetch, timeoutMs = 3000 }) { const { url, field } = release(source); // Claimed before the lookup so a failed one also waits a day: unauthenticated api.github.com allows 60 requests an hour per IP, https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api if (!claimDaily({ stateDir, name: cli, now })) return { status: "skipped" }; diff --git a/plugins/firecrawl/lib/cli-version-check.mjs b/plugins/firecrawl/lib/cli-version-check.mjs index 2863e96e9c9..1339142c117 100644 --- a/plugins/firecrawl/lib/cli-version-check.mjs +++ b/plugins/firecrawl/lib/cli-version-check.mjs @@ -54,9 +54,10 @@ function exclusively(file, claim) { return false; } -export function claimDaily({ stateDir, name, now = Date.now() }) { +export function claimDaily({ stateDir, name, now }) { const file = machineStateFile(stateDir, name); return exclusively(file, () => { + now ??= Date.now(); const last = lastClaim(file); if (last <= now && now - last < DAY_MS) return false; const tmp = `${file}.${process.pid}.tmp`; @@ -88,7 +89,7 @@ function release(source) { throw new Error(`invalid source: ${source} (expected npm: or github:/)`); } -export async function checkVersion({ stateDir, cli, installed, source, hint = "", now = Date.now(), fetch = globalThis.fetch, timeoutMs = 3000 }) { +export async function checkVersion({ stateDir, cli, installed, source, hint = "", now, fetch = globalThis.fetch, timeoutMs = 3000 }) { const { url, field } = release(source); // Claimed before the lookup so a failed one also waits a day: unauthenticated api.github.com allows 60 requests an hour per IP, https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api if (!claimDaily({ stateDir, name: cli, now })) return { status: "skipped" }; diff --git a/plugins/playwright/lib/cli-version-check.mjs b/plugins/playwright/lib/cli-version-check.mjs index 2863e96e9c9..1339142c117 100644 --- a/plugins/playwright/lib/cli-version-check.mjs +++ b/plugins/playwright/lib/cli-version-check.mjs @@ -54,9 +54,10 @@ function exclusively(file, claim) { return false; } -export function claimDaily({ stateDir, name, now = Date.now() }) { +export function claimDaily({ stateDir, name, now }) { const file = machineStateFile(stateDir, name); return exclusively(file, () => { + now ??= Date.now(); const last = lastClaim(file); if (last <= now && now - last < DAY_MS) return false; const tmp = `${file}.${process.pid}.tmp`; @@ -88,7 +89,7 @@ function release(source) { throw new Error(`invalid source: ${source} (expected npm: or github:/)`); } -export async function checkVersion({ stateDir, cli, installed, source, hint = "", now = Date.now(), fetch = globalThis.fetch, timeoutMs = 3000 }) { +export async function checkVersion({ stateDir, cli, installed, source, hint = "", now, fetch = globalThis.fetch, timeoutMs = 3000 }) { const { url, field } = release(source); // Claimed before the lookup so a failed one also waits a day: unauthenticated api.github.com allows 60 requests an hour per IP, https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api if (!claimDaily({ stateDir, name: cli, now })) return { status: "skipped" };