main
1// The deployment lock (scripts/cd/lock.ts). Run 2026-11-07T18-30-47-501Z ran beside another run and replaced its 0%
2// canary b494b751: a second run that writes the deployment must not get past the lock while the first is alive.
3import assert from 'node:assert/strict'
4import { spawn } from 'node:child_process'
5import { mkdtemp, readFile, writeFile } from 'node:fs/promises'
6import { tmpdir } from 'node:os'
7import { join } from 'node:path'
8import { test } from 'node:test'
9import { acquireLock, pidIsCd, type LockIo } from '../scripts/cd/lock.ts'
11function fakeIo(alive: ReadonlySet<number>, host = 'box'): LockIo {
12 return { alive: pid => alive.has(pid), host: () => host, now: () => new Date('2026-11-08T12:00:00Z') }
13}
15async function dir(): Promise<string> {
16 return mkdtemp(join(tmpdir(), 'ply-cd-lock-'))
17}
19test('a second run is refused while the first holder is alive, and gets the lock once it is released', async () => {
20 const path = join(await dir(), 'deploy.lock')
21 const io = fakeIo(new Set([101, 202]))
22 const first = await acquireLock(path, { command: 'cd --no-bump', cwd: '/w/a', pid: 101 }, io)
23 assert.equal(first.ok, true)
24 const second = await acquireLock(path, { command: 'cd --no-bump', cwd: '/w/b', pid: 202 }, io)
25 assert.equal(second.ok, false)
26 if (!second.ok) {
27 assert.match(second.reason, /held by a live run: pid 101 on box/)
28 assert.match(second.reason, /cwd \/w\/a/)
29 }
30 if (first.ok) await first.release()
31 const third = await acquireLock(path, { command: 'cd --no-bump', cwd: '/w/b', pid: 202 }, io)
32 assert.equal(third.ok, true)
33})
35test('a lock whose pid is gone (or reused by a non-CD process) is stale and taken over, and says so', async () => {
36 const path = join(await dir(), 'deploy.lock')
37 await acquireLock(path, { command: 'cd', cwd: '/w/a', pid: 101 }, fakeIo(new Set([101])))
38 const next = await acquireLock(path, { command: 'cd', cwd: '/w/b', pid: 202 }, fakeIo(new Set([202])))
39 assert.equal(next.ok, true)
40 if (next.ok) {
41 assert.equal(next.tookOver === null, false)
42 if (next.tookOver) assert.equal(next.tookOver.pid, 101)
43 const body = JSON.parse(await readFile(path, 'utf8')) as { pid: number; cwd: string }
44 assert.equal(body.pid, 202)
45 assert.equal(body.cwd, '/w/b')
46 }
47})
49test('a lock from another host is never taken over', async () => {
50 const path = join(await dir(), 'deploy.lock')
51 await acquireLock(path, { command: 'cd', cwd: '/w/a', pid: 101 }, fakeIo(new Set(), 'other-box'))
52 const next = await acquireLock(path, { command: 'cd', cwd: '/w/b', pid: 202 }, fakeIo(new Set()))
53 assert.equal(next.ok, false)
54 if (!next.ok) assert.match(next.reason, /another host/)
55})
57test('an unreadable lock body is stale, not a permanent block', async () => {
58 const path = join(await dir(), 'deploy.lock')
59 await writeFile(path, 'not json')
60 const next = await acquireLock(path, { command: 'cd', cwd: '/w/b', pid: 202 }, fakeIo(new Set([202])))
61 assert.equal(next.ok, true)
62})
64test('release removes only its own lock', async () => {
65 const path = join(await dir(), 'deploy.lock')
66 const first = await acquireLock(path, { command: 'cd', cwd: '/w/a', pid: 101 }, fakeIo(new Set()))
67 // 101 died; 202 took the lock over. A late release from 101 must not delete 202's lock.
68 const second = await acquireLock(path, { command: 'cd', cwd: '/w/b', pid: 202 }, fakeIo(new Set([202])))
69 assert.equal(second.ok, true)
70 if (first.ok) await first.release()
71 const body = JSON.parse(await readFile(path, 'utf8')) as { pid: number }
72 assert.equal(body.pid, 202)
73})
75test('two runs racing for one free lock: exactly one wins', async () => {
76 const path = join(await dir(), 'deploy.lock')
77 const io = fakeIo(new Set([1, 2, 3, 4, 5, 6, 7, 8]))
78 const results = await Promise.all([1, 2, 3, 4, 5, 6, 7, 8].map(pid => acquireLock(path, { command: 'cd', cwd: '/w', pid }, io)))
79 assert.equal(results.filter(result => result.ok).length, 1)
80})
82test('two runs racing to take over one stale lock: exactly one wins', async () => {
83 const path = join(await dir(), 'deploy.lock')
84 await acquireLock(path, { command: 'cd', cwd: '/w', pid: 99 }, fakeIo(new Set()))
85 const io = fakeIo(new Set([1, 2, 3, 4, 5, 6]))
86 const results = await Promise.all([1, 2, 3, 4, 5, 6].map(pid => acquireLock(path, { command: 'cd', cwd: '/w', pid }, io)))
87 assert.equal(results.filter(result => result.ok).length, 1)
88})
90test('pidIsCd: this test process is alive but not a CD run; a dead pid is not alive', async () => {
91 assert.equal(pidIsCd(process.pid), false)
92 assert.equal(pidIsCd(-1), false)
93 const child = spawn(process.execPath, ['-e', 'setTimeout(() => {}, 5000)', 'scripts/cd/main.ts'], { stdio: 'ignore' })
94 try {
95 await new Promise(resolve => setTimeout(resolve, 200))
96 assert.equal(pidIsCd(Number(child.pid)), true)
97 } finally {
98 child.kill()
99 }
100 await new Promise(resolve => child.once('exit', resolve))
101 assert.equal(pidIsCd(Number(child.pid)), false)
102})
Diagnostics4 git calls · 54 ms
CallStatusSizeTime054 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200145 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=145 git.pkt.payload.max=76 git.ls_refs.count=2 git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020040 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=41349 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=526b305930b8ef382f2d7a1f9c0238730a585921 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=6 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=41349 git.pkt.payload.max=41254 git.sideband.band1.bytes=41253 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=61 git.pack.bytes=41253 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c git.pack.full=61Open pack map
fetch2001.5 KiB · hit49 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=49 git.response.bytes=1501 git.response.cache=HIT git.response.duration_ms=49 git.fetch.wants=4626f700157047bafb6aaa5e7e26b852fc630895 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1501 git.pkt.payload.max=1480 git.sideband.band1.bytes=1479 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply-web.git
- Ref
- refs/heads/main
- Commit
- 526b305930b8ef382f2d7a1f9c0238730a585921
- cf-ray
- a5e2e0081aa82308
- Request
- 222d68e8…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format