flock.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. import { describe, expect, test } from "bun:test"
  2. import fs from "fs/promises"
  3. import { spawn } from "child_process"
  4. import path from "path"
  5. import os from "os"
  6. import { Flock } from "@opencode-ai/shared/util/flock"
  7. import { Hash } from "@opencode-ai/shared/util/hash"
  8. type Msg = {
  9. key: string
  10. dir: string
  11. staleMs?: number
  12. timeoutMs?: number
  13. baseDelayMs?: number
  14. maxDelayMs?: number
  15. holdMs?: number
  16. ready?: string
  17. active?: string
  18. done?: string
  19. }
  20. const root = path.join(import.meta.dir, "../..")
  21. const worker = path.join(import.meta.dir, "../fixture/flock-worker.ts")
  22. async function tmpdir() {
  23. const dir = await fs.mkdtemp(path.join(os.tmpdir(), "flock-test-"))
  24. return {
  25. path: dir,
  26. async [Symbol.asyncDispose]() {
  27. await fs.rm(dir, { recursive: true, force: true })
  28. },
  29. }
  30. }
  31. function lock(dir: string, key: string) {
  32. return path.join(dir, Hash.fast(key) + ".lock")
  33. }
  34. function sleep(ms: number) {
  35. return new Promise<void>((resolve) => {
  36. setTimeout(resolve, ms)
  37. })
  38. }
  39. async function exists(file: string) {
  40. return fs
  41. .stat(file)
  42. .then(() => true)
  43. .catch(() => false)
  44. }
  45. async function wait(file: string, timeout = 3_000) {
  46. const stop = Date.now() + timeout
  47. while (Date.now() < stop) {
  48. if (await exists(file)) return
  49. await sleep(20)
  50. }
  51. throw new Error(`Timed out waiting for file: ${file}`)
  52. }
  53. function run(msg: Msg) {
  54. return new Promise<{ code: number; stdout: Buffer; stderr: Buffer }>((resolve) => {
  55. const proc = spawn(process.execPath, [worker, JSON.stringify(msg)], {
  56. cwd: root,
  57. })
  58. const stdout: Buffer[] = []
  59. const stderr: Buffer[] = []
  60. proc.stdout?.on("data", (data) => stdout.push(Buffer.from(data)))
  61. proc.stderr?.on("data", (data) => stderr.push(Buffer.from(data)))
  62. proc.on("close", (code) => {
  63. resolve({
  64. code: code ?? 1,
  65. stdout: Buffer.concat(stdout),
  66. stderr: Buffer.concat(stderr),
  67. })
  68. })
  69. })
  70. }
  71. function spawnWorker(msg: Msg) {
  72. return spawn(process.execPath, [worker, JSON.stringify(msg)], {
  73. cwd: root,
  74. stdio: ["ignore", "pipe", "pipe"],
  75. })
  76. }
  77. function stopWorker(proc: ReturnType<typeof spawnWorker>) {
  78. if (proc.exitCode !== null || proc.signalCode !== null) return Promise.resolve()
  79. if (process.platform !== "win32" || !proc.pid) {
  80. proc.kill()
  81. return Promise.resolve()
  82. }
  83. return new Promise<void>((resolve) => {
  84. const killProc = spawn("taskkill", ["/pid", String(proc.pid), "/T", "/F"])
  85. killProc.on("close", () => {
  86. proc.kill()
  87. resolve()
  88. })
  89. })
  90. }
  91. async function readJson<T>(p: string): Promise<T> {
  92. return JSON.parse(await fs.readFile(p, "utf8"))
  93. }
  94. describe("util.flock", () => {
  95. test("enforces mutual exclusion under process contention", async () => {
  96. await using tmp = await tmpdir()
  97. const dir = path.join(tmp.path, "locks")
  98. const done = path.join(tmp.path, "done.log")
  99. const active = path.join(tmp.path, "active")
  100. const key = "flock:stress"
  101. const n = 16
  102. const out = await Promise.all(
  103. Array.from({ length: n }, () =>
  104. run({
  105. key,
  106. dir,
  107. done,
  108. active,
  109. holdMs: 30,
  110. staleMs: 1_000,
  111. timeoutMs: 15_000,
  112. }),
  113. ),
  114. )
  115. expect(out.map((x) => x.code)).toEqual(Array.from({ length: n }, () => 0))
  116. expect(out.map((x) => x.stderr.toString()).filter(Boolean)).toEqual([])
  117. const lines = (await fs.readFile(done, "utf8"))
  118. .split("\n")
  119. .map((x) => x.trim())
  120. .filter(Boolean)
  121. expect(lines.length).toBe(n)
  122. }, 20_000)
  123. test("times out while waiting when lock is still healthy", async () => {
  124. await using tmp = await tmpdir()
  125. const dir = path.join(tmp.path, "locks")
  126. const key = "flock:timeout"
  127. const ready = path.join(tmp.path, "ready")
  128. const proc = spawnWorker({
  129. key,
  130. dir,
  131. ready,
  132. holdMs: 20_000,
  133. staleMs: 10_000,
  134. timeoutMs: 30_000,
  135. })
  136. try {
  137. await wait(ready, 5_000)
  138. const seen: string[] = []
  139. const err = await Flock.withLock(key, async () => {}, {
  140. dir,
  141. staleMs: 10_000,
  142. timeoutMs: 1_000,
  143. onWait: (tick) => {
  144. seen.push(tick.key)
  145. },
  146. }).catch((err) => err)
  147. expect(err).toBeInstanceOf(Error)
  148. if (!(err instanceof Error)) throw err
  149. expect(err.message).toContain("Timed out waiting for lock")
  150. expect(seen.length).toBeGreaterThan(0)
  151. expect(seen.every((x) => x === key)).toBe(true)
  152. } finally {
  153. await stopWorker(proc).catch(() => undefined)
  154. await new Promise((resolve) => proc.on("close", resolve))
  155. }
  156. }, 15_000)
  157. test("recovers after a crashed lock owner", async () => {
  158. await using tmp = await tmpdir()
  159. const dir = path.join(tmp.path, "locks")
  160. const key = "flock:crash"
  161. const ready = path.join(tmp.path, "ready")
  162. const proc = spawnWorker({
  163. key,
  164. dir,
  165. ready,
  166. holdMs: 20_000,
  167. staleMs: 500,
  168. timeoutMs: 30_000,
  169. })
  170. await wait(ready, 5_000)
  171. await stopWorker(proc)
  172. await new Promise((resolve) => proc.on("close", resolve))
  173. let hit = false
  174. await Flock.withLock(
  175. key,
  176. async () => {
  177. hit = true
  178. },
  179. {
  180. dir,
  181. staleMs: 500,
  182. timeoutMs: 8_000,
  183. },
  184. )
  185. expect(hit).toBe(true)
  186. }, 20_000)
  187. test("breaks stale lock dirs when heartbeat is missing", async () => {
  188. await using tmp = await tmpdir()
  189. const dir = path.join(tmp.path, "locks")
  190. const key = "flock:missing-heartbeat"
  191. const lockDir = lock(dir, key)
  192. await fs.mkdir(lockDir, { recursive: true })
  193. const old = new Date(Date.now() - 2_000)
  194. await fs.utimes(lockDir, old, old)
  195. let hit = false
  196. await Flock.withLock(
  197. key,
  198. async () => {
  199. hit = true
  200. },
  201. {
  202. dir,
  203. staleMs: 200,
  204. timeoutMs: 3_000,
  205. },
  206. )
  207. expect(hit).toBe(true)
  208. })
  209. test("recovers when a stale breaker claim was left behind", async () => {
  210. await using tmp = await tmpdir()
  211. const dir = path.join(tmp.path, "locks")
  212. const key = "flock:stale-breaker"
  213. const lockDir = lock(dir, key)
  214. const breaker = lockDir + ".breaker"
  215. await fs.mkdir(lockDir, { recursive: true })
  216. await fs.mkdir(breaker)
  217. const old = new Date(Date.now() - 2_000)
  218. await fs.utimes(lockDir, old, old)
  219. await fs.utimes(breaker, old, old)
  220. let hit = false
  221. await Flock.withLock(
  222. key,
  223. async () => {
  224. hit = true
  225. },
  226. {
  227. dir,
  228. staleMs: 200,
  229. timeoutMs: 3_000,
  230. },
  231. )
  232. expect(hit).toBe(true)
  233. expect(await exists(breaker)).toBe(false)
  234. })
  235. test("fails clearly if lock dir is removed while held", async () => {
  236. await using tmp = await tmpdir()
  237. const dir = path.join(tmp.path, "locks")
  238. const key = "flock:compromised"
  239. const lockDir = lock(dir, key)
  240. const err = await Flock.withLock(
  241. key,
  242. async () => {
  243. await fs.rm(lockDir, {
  244. recursive: true,
  245. force: true,
  246. })
  247. },
  248. {
  249. dir,
  250. staleMs: 1_000,
  251. timeoutMs: 3_000,
  252. },
  253. ).catch((err) => err)
  254. expect(err).toBeInstanceOf(Error)
  255. if (!(err instanceof Error)) throw err
  256. expect(err.message).toContain("compromised")
  257. let hit = false
  258. await Flock.withLock(
  259. key,
  260. async () => {
  261. hit = true
  262. },
  263. {
  264. dir,
  265. staleMs: 200,
  266. timeoutMs: 3_000,
  267. },
  268. )
  269. expect(hit).toBe(true)
  270. })
  271. test("writes owner metadata while lock is held", async () => {
  272. await using tmp = await tmpdir()
  273. const dir = path.join(tmp.path, "locks")
  274. const key = "flock:meta"
  275. const file = path.join(lock(dir, key), "meta.json")
  276. await Flock.withLock(
  277. key,
  278. async () => {
  279. const json = await readJson<{
  280. token?: unknown
  281. pid?: unknown
  282. hostname?: unknown
  283. createdAt?: unknown
  284. }>(file)
  285. expect(typeof json.token).toBe("string")
  286. expect(typeof json.pid).toBe("number")
  287. expect(typeof json.hostname).toBe("string")
  288. expect(typeof json.createdAt).toBe("string")
  289. },
  290. {
  291. dir,
  292. staleMs: 1_000,
  293. timeoutMs: 3_000,
  294. },
  295. )
  296. })
  297. test("supports acquire with await using", async () => {
  298. await using tmp = await tmpdir()
  299. const dir = path.join(tmp.path, "locks")
  300. const key = "flock:acquire"
  301. const lockDir = lock(dir, key)
  302. {
  303. await using _ = await Flock.acquire(key, {
  304. dir,
  305. staleMs: 1_000,
  306. timeoutMs: 3_000,
  307. })
  308. expect(await exists(lockDir)).toBe(true)
  309. }
  310. expect(await exists(lockDir)).toBe(false)
  311. })
  312. test("refuses token mismatch release and recovers from stale", async () => {
  313. await using tmp = await tmpdir()
  314. const dir = path.join(tmp.path, "locks")
  315. const key = "flock:token"
  316. const lockDir = lock(dir, key)
  317. const meta = path.join(lockDir, "meta.json")
  318. const err = await Flock.withLock(
  319. key,
  320. async () => {
  321. const json = await readJson<{ token?: string }>(meta)
  322. json.token = "tampered"
  323. await fs.writeFile(meta, JSON.stringify(json, null, 2))
  324. },
  325. {
  326. dir,
  327. staleMs: 500,
  328. timeoutMs: 3_000,
  329. },
  330. ).catch((err) => err)
  331. expect(err).toBeInstanceOf(Error)
  332. if (!(err instanceof Error)) throw err
  333. expect(err.message).toContain("token mismatch")
  334. expect(await exists(lockDir)).toBe(true)
  335. let hit = false
  336. await Flock.withLock(
  337. key,
  338. async () => {
  339. hit = true
  340. },
  341. {
  342. dir,
  343. staleMs: 500,
  344. timeoutMs: 6_000,
  345. },
  346. )
  347. expect(hit).toBe(true)
  348. })
  349. test("fails clearly on unwritable lock roots", async () => {
  350. if (process.platform === "win32") return
  351. await using tmp = await tmpdir()
  352. const dir = path.join(tmp.path, "locks")
  353. const key = "flock:perm"
  354. await fs.mkdir(dir, { recursive: true })
  355. await fs.chmod(dir, 0o500)
  356. try {
  357. const err = await Flock.withLock(key, async () => {}, {
  358. dir,
  359. staleMs: 100,
  360. timeoutMs: 500,
  361. }).catch((err) => err)
  362. expect(err).toBeInstanceOf(Error)
  363. if (!(err instanceof Error)) throw err
  364. const text = err.message
  365. expect(text.includes("EACCES") || text.includes("EPERM")).toBe(true)
  366. } finally {
  367. await fs.chmod(dir, 0o700)
  368. }
  369. })
  370. })