flock.test.ts 9.1 KB

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