| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426 |
- import { describe, expect, test } from "bun:test"
- import fs from "fs/promises"
- import { spawn } from "child_process"
- import path from "path"
- import os from "os"
- import { Flock } from "@opencode-ai/shared/util/flock"
- import { Hash } from "@opencode-ai/shared/util/hash"
- type Msg = {
- key: string
- dir: string
- staleMs?: number
- timeoutMs?: number
- baseDelayMs?: number
- maxDelayMs?: number
- holdMs?: number
- ready?: string
- active?: string
- done?: string
- }
- const root = path.join(import.meta.dir, "../..")
- const worker = path.join(import.meta.dir, "../fixture/flock-worker.ts")
- async function tmpdir() {
- const dir = await fs.mkdtemp(path.join(os.tmpdir(), "flock-test-"))
- return {
- path: dir,
- async [Symbol.asyncDispose]() {
- await fs.rm(dir, { recursive: true, force: true })
- },
- }
- }
- function lock(dir: string, key: string) {
- return path.join(dir, Hash.fast(key) + ".lock")
- }
- function sleep(ms: number) {
- return new Promise<void>((resolve) => {
- setTimeout(resolve, ms)
- })
- }
- async function exists(file: string) {
- return fs
- .stat(file)
- .then(() => true)
- .catch(() => false)
- }
- async function wait(file: string, timeout = 3_000) {
- const stop = Date.now() + timeout
- while (Date.now() < stop) {
- if (await exists(file)) return
- await sleep(20)
- }
- throw new Error(`Timed out waiting for file: ${file}`)
- }
- function run(msg: Msg) {
- return new Promise<{ code: number; stdout: Buffer; stderr: Buffer }>((resolve) => {
- const proc = spawn(process.execPath, [worker, JSON.stringify(msg)], {
- cwd: root,
- })
- const stdout: Buffer[] = []
- const stderr: Buffer[] = []
- proc.stdout?.on("data", (data) => stdout.push(Buffer.from(data)))
- proc.stderr?.on("data", (data) => stderr.push(Buffer.from(data)))
- proc.on("close", (code) => {
- resolve({
- code: code ?? 1,
- stdout: Buffer.concat(stdout),
- stderr: Buffer.concat(stderr),
- })
- })
- })
- }
- function spawnWorker(msg: Msg) {
- return spawn(process.execPath, [worker, JSON.stringify(msg)], {
- cwd: root,
- stdio: ["ignore", "pipe", "pipe"],
- })
- }
- function stopWorker(proc: ReturnType<typeof spawnWorker>) {
- if (proc.exitCode !== null || proc.signalCode !== null) return Promise.resolve()
- if (process.platform !== "win32" || !proc.pid) {
- proc.kill()
- return Promise.resolve()
- }
- return new Promise<void>((resolve) => {
- const killProc = spawn("taskkill", ["/pid", String(proc.pid), "/T", "/F"])
- killProc.on("close", () => {
- proc.kill()
- resolve()
- })
- })
- }
- async function readJson<T>(p: string): Promise<T> {
- return JSON.parse(await fs.readFile(p, "utf8"))
- }
- describe("util.flock", () => {
- test("enforces mutual exclusion under process contention", async () => {
- await using tmp = await tmpdir()
- const dir = path.join(tmp.path, "locks")
- const done = path.join(tmp.path, "done.log")
- const active = path.join(tmp.path, "active")
- const key = "flock:stress"
- const n = 16
- const out = await Promise.all(
- Array.from({ length: n }, () =>
- run({
- key,
- dir,
- done,
- active,
- holdMs: 30,
- staleMs: 1_000,
- timeoutMs: 15_000,
- }),
- ),
- )
- expect(out.map((x) => x.code)).toEqual(Array.from({ length: n }, () => 0))
- expect(out.map((x) => x.stderr.toString()).filter(Boolean)).toEqual([])
- const lines = (await fs.readFile(done, "utf8"))
- .split("\n")
- .map((x) => x.trim())
- .filter(Boolean)
- expect(lines.length).toBe(n)
- }, 20_000)
- test("times out while waiting when lock is still healthy", async () => {
- await using tmp = await tmpdir()
- const dir = path.join(tmp.path, "locks")
- const key = "flock:timeout"
- const ready = path.join(tmp.path, "ready")
- const proc = spawnWorker({
- key,
- dir,
- ready,
- holdMs: 20_000,
- staleMs: 10_000,
- timeoutMs: 30_000,
- })
- try {
- await wait(ready, 5_000)
- const seen: string[] = []
- const err = await Flock.withLock(key, async () => {}, {
- dir,
- staleMs: 10_000,
- timeoutMs: 1_000,
- onWait: (tick) => {
- seen.push(tick.key)
- },
- }).catch((err) => err)
- expect(err).toBeInstanceOf(Error)
- if (!(err instanceof Error)) throw err
- expect(err.message).toContain("Timed out waiting for lock")
- expect(seen.length).toBeGreaterThan(0)
- expect(seen.every((x) => x === key)).toBe(true)
- } finally {
- await stopWorker(proc).catch(() => undefined)
- await new Promise((resolve) => proc.on("close", resolve))
- }
- }, 15_000)
- test("recovers after a crashed lock owner", async () => {
- await using tmp = await tmpdir()
- const dir = path.join(tmp.path, "locks")
- const key = "flock:crash"
- const ready = path.join(tmp.path, "ready")
- const proc = spawnWorker({
- key,
- dir,
- ready,
- holdMs: 20_000,
- staleMs: 500,
- timeoutMs: 30_000,
- })
- await wait(ready, 5_000)
- await stopWorker(proc)
- await new Promise((resolve) => proc.on("close", resolve))
- let hit = false
- await Flock.withLock(
- key,
- async () => {
- hit = true
- },
- {
- dir,
- staleMs: 500,
- timeoutMs: 8_000,
- },
- )
- expect(hit).toBe(true)
- }, 20_000)
- test("breaks stale lock dirs when heartbeat is missing", async () => {
- await using tmp = await tmpdir()
- const dir = path.join(tmp.path, "locks")
- const key = "flock:missing-heartbeat"
- const lockDir = lock(dir, key)
- await fs.mkdir(lockDir, { recursive: true })
- const old = new Date(Date.now() - 2_000)
- await fs.utimes(lockDir, old, old)
- let hit = false
- await Flock.withLock(
- key,
- async () => {
- hit = true
- },
- {
- dir,
- staleMs: 200,
- timeoutMs: 3_000,
- },
- )
- expect(hit).toBe(true)
- })
- test("recovers when a stale breaker claim was left behind", async () => {
- await using tmp = await tmpdir()
- const dir = path.join(tmp.path, "locks")
- const key = "flock:stale-breaker"
- const lockDir = lock(dir, key)
- const breaker = lockDir + ".breaker"
- await fs.mkdir(lockDir, { recursive: true })
- await fs.mkdir(breaker)
- const old = new Date(Date.now() - 2_000)
- await fs.utimes(lockDir, old, old)
- await fs.utimes(breaker, old, old)
- let hit = false
- await Flock.withLock(
- key,
- async () => {
- hit = true
- },
- {
- dir,
- staleMs: 200,
- timeoutMs: 3_000,
- },
- )
- expect(hit).toBe(true)
- expect(await exists(breaker)).toBe(false)
- })
- test("fails clearly if lock dir is removed while held", async () => {
- await using tmp = await tmpdir()
- const dir = path.join(tmp.path, "locks")
- const key = "flock:compromised"
- const lockDir = lock(dir, key)
- const err = await Flock.withLock(
- key,
- async () => {
- await fs.rm(lockDir, {
- recursive: true,
- force: true,
- })
- },
- {
- dir,
- staleMs: 1_000,
- timeoutMs: 3_000,
- },
- ).catch((err) => err)
- expect(err).toBeInstanceOf(Error)
- if (!(err instanceof Error)) throw err
- expect(err.message).toContain("compromised")
- let hit = false
- await Flock.withLock(
- key,
- async () => {
- hit = true
- },
- {
- dir,
- staleMs: 200,
- timeoutMs: 3_000,
- },
- )
- expect(hit).toBe(true)
- })
- test("writes owner metadata while lock is held", async () => {
- await using tmp = await tmpdir()
- const dir = path.join(tmp.path, "locks")
- const key = "flock:meta"
- const file = path.join(lock(dir, key), "meta.json")
- await Flock.withLock(
- key,
- async () => {
- const json = await readJson<{
- token?: unknown
- pid?: unknown
- hostname?: unknown
- createdAt?: unknown
- }>(file)
- expect(typeof json.token).toBe("string")
- expect(typeof json.pid).toBe("number")
- expect(typeof json.hostname).toBe("string")
- expect(typeof json.createdAt).toBe("string")
- },
- {
- dir,
- staleMs: 1_000,
- timeoutMs: 3_000,
- },
- )
- })
- test("supports acquire with await using", async () => {
- await using tmp = await tmpdir()
- const dir = path.join(tmp.path, "locks")
- const key = "flock:acquire"
- const lockDir = lock(dir, key)
- {
- await using _ = await Flock.acquire(key, {
- dir,
- staleMs: 1_000,
- timeoutMs: 3_000,
- })
- expect(await exists(lockDir)).toBe(true)
- }
- expect(await exists(lockDir)).toBe(false)
- })
- test("refuses token mismatch release and recovers from stale", async () => {
- await using tmp = await tmpdir()
- const dir = path.join(tmp.path, "locks")
- const key = "flock:token"
- const lockDir = lock(dir, key)
- const meta = path.join(lockDir, "meta.json")
- const err = await Flock.withLock(
- key,
- async () => {
- const json = await readJson<{ token?: string }>(meta)
- json.token = "tampered"
- await fs.writeFile(meta, JSON.stringify(json, null, 2))
- },
- {
- dir,
- staleMs: 500,
- timeoutMs: 3_000,
- },
- ).catch((err) => err)
- expect(err).toBeInstanceOf(Error)
- if (!(err instanceof Error)) throw err
- expect(err.message).toContain("token mismatch")
- expect(await exists(lockDir)).toBe(true)
- let hit = false
- await Flock.withLock(
- key,
- async () => {
- hit = true
- },
- {
- dir,
- staleMs: 500,
- timeoutMs: 6_000,
- },
- )
- expect(hit).toBe(true)
- })
- test("fails clearly on unwritable lock roots", async () => {
- if (process.platform === "win32") return
- await using tmp = await tmpdir()
- const dir = path.join(tmp.path, "locks")
- const key = "flock:perm"
- await fs.mkdir(dir, { recursive: true })
- await fs.chmod(dir, 0o500)
- try {
- const err = await Flock.withLock(key, async () => {}, {
- dir,
- staleMs: 100,
- timeoutMs: 500,
- }).catch((err) => err)
- expect(err).toBeInstanceOf(Error)
- if (!(err instanceof Error)) throw err
- const text = err.message
- expect(text.includes("EACCES") || text.includes("EPERM")).toBe(true)
- } finally {
- await fs.chmod(dir, 0o700)
- }
- })
- })
|