Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.
JavaScript agentic-codingai-agentsai-governanceautonomous-agentsclaude-codeclicode-reviewcodexcoding-agentsdeveloper-toolsgit-hooksllmlocal-firstmulti-agentsoftware-factoryspec-driven-developmentverificationvibe-coding
fix(relay): a dead owner's lock is reaped by exactly one waiter [H5]
Corey Olsonb487ef8dfdfd
diff --git a/src/relay-lock.js b/src/relay-lock.jsindex e08bbe97..39b01ac7 100644--- a/src/relay-lock.js+++ b/src/relay-lock.js@@ -16,6 +16,52 @@ import { Refused } from './refused.js';/** Longest wait for a sibling worktree's relay send, and how often the waiting command says so. */const LOCK_WAIT_MS = 120000;const LOCK_PROGRESS_MS = 10000;+// Budget: a lock with no readable owner this long after it was made belongs to a command that died making it.+const UNOWNED_STALE_MS = 30000;++/** The lock's owner, or null when it names none yet (its maker is between creating it and writing its pid). */+function ownerOf(lock) {+ const pid = Number(readFileSync(join(lock, 'pid'), 'utf8'));+ return Number.isInteger(pid) && pid > 0 ? pid : null;+}++/** Whether a process has exited; one another user owns, or one we cannot signal, still counts as alive. */+function gone(pid) {+ try { process.kill(pid, 0); return false; }+ catch (error) { return error.code === 'ESRCH'; }+}++/**+ * Remove a lock whose owner is gone, as the only waiter allowed to (#1119). Waiters that found the same dead owner+ * compete for one short reap lock; the winner checks again that the lock still has that dead owner, or still none+ * after the stale age, and only then removes it. Nothing else removes another's lock, so the check cannot be+ * overtaken: a live owner's lock is never removed, and the lock it frees is then won by exactly one mkdir.+ *+ * @returns {boolean} Whether this waiter cleared the lock.+ */+function reap(lock, seenOwner) {+ const reaper = lock + '.reap';+ try { mkdirSync(reaper, { mode: 0o700 }); }+ catch (error) {+ if (error.code !== 'EEXIST') throw error;+ // A reaper holds this only for a check and a removal; one this old died doing it.+ try { if (Date.now() - lstatSync(reaper).mtimeMs > UNOWNED_STALE_MS) rmSync(reaper, { recursive: true, force: true }); }+ catch (gone) { if (gone.code !== 'ENOENT') throw gone; }+ return false;+ }+ try {+ let owner;+ try { owner = ownerOf(lock); } catch (error) { if (error.code === 'ENOENT') return false; owner = null; }+ const dead = seenOwner === null+ ? owner === null && Date.now() - lstatSync(lock).mtimeMs > UNOWNED_STALE_MS+ : owner === seenOwner && gone(owner);+ if (dead) rmSync(lock, { recursive: true, force: true });+ return dead;+ } catch (error) {+ if (error.code === 'ENOENT') return false;+ throw error;+ } finally { rmSync(reaper, { recursive: true, force: true }); }+}/*** Wait for another worktree's upload, recovering a lock only when its owner is gone.@@ -33,15 +79,18 @@ export async function locked(file, work, io) {try { mkdirSync(lock, { mode: 0o700 }); writeFileSync(join(lock, 'pid'), String(process.pid)); break; }catch (error) {if (error.code !== 'EEXIST') throw error;- try {- const pid = Number(readFileSync(join(lock, 'pid'), 'utf8'));- if (Number.isInteger(pid) && pid > 0) {- try { process.kill(pid, 0); } catch (dead) { if (dead.code === 'ESRCH') { rmSync(lock, { recursive: true, force: true }); continue; } }+ let owner;+ try { owner = ownerOf(lock); }+ catch (unread) {+ if (unread.code === 'ENOENT') {+ try { lstatSync(lock); } catch (missing) { if (missing.code === 'ENOENT') continue; throw missing; }}- } catch {- try { if (Date.now() - lstatSync(lock).mtimeMs > 30000) { rmSync(lock, { recursive: true, force: true }); continue; } }- catch (gone) { if (gone.code === 'ENOENT') continue; throw gone; }+ owner = null;}+ let stale;+ try { stale = owner !== null ? gone(owner) : Date.now() - lstatSync(lock).mtimeMs > UNOWNED_STALE_MS; }+ catch (vanished) { if (vanished.code === 'ENOENT') continue; throw vanished; }+ if (stale && reap(lock, owner)) continue;if (Date.now() >= deadline) throw new Refused('RELAY_BUSY', `another worktree was still sending this board after ${Math.round((Date.now() - startedAt) / 1000)} s; nothing was lost, so run the command again`);if (io?.err && Date.now() >= nextProgress) {nextProgress = Date.now() + progressEvery;@@ -56,5 +105,10 @@ export async function locked(file, work, io) {if (/^relay\.json\.[0-9a-f-]+\.tmp$/u.test(name)) rmSync(join(dirname(file), name), { force: true });}return await work();- } finally { rmSync(lock, { recursive: true, force: true }); }+ } finally {+ // Remove only this command's own lock; another's is never ours to free.+ let owner = null;+ try { owner = ownerOf(lock); } catch { /* Already gone, or never named: nothing of ours to keep. */ }+ if (owner === null || owner === process.pid) rmSync(lock, { recursive: true, force: true });+ }}diff --git a/test/relay-lock.test.js b/test/relay-lock.test.jsnew file mode 100644index 00000000..8e5edcdf--- /dev/null+++ b/test/relay-lock.test.js@@ -0,0 +1,53 @@+/** The relay lock has one holder at a time, even when many waiters find the same dead owner [H5]. */+import assert from 'node:assert/strict';+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { spawn, spawnSync } from 'node:child_process';+import { test } from 'node:test';++const LOCK_MODULE = new URL('../src/relay-lock.js', import.meta.url).href;++/** A process number that belonged to a process that has already exited. */+function deadPid() {+ return spawnSync(process.execPath, ['-e', '']).pid;+}++/** One waiter: take the lock, note entry and exit in the shared log, and hold it briefly. */+const WAITER = `+import { appendFileSync } from 'node:fs';+const { locked } = await import(process.argv[1]);+const [log, file] = process.argv.slice(2);+await locked(file, async () => {+ appendFileSync(log, 'enter ' + process.pid + '\\n');+ await new Promise(done => setTimeout(done, 30));+ appendFileSync(log, 'exit ' + process.pid + '\\n');+});+`;++test('waiters on a dead owner leave exactly one holder at a time [H5]', async (t) => {+ const root = mkdtempSync(join(tmpdir(), 'pullboard-relay-lock-'));+ t.after(() => rmSync(root, { recursive: true, force: true }));+ for (let round = 0; round < 15; round += 1) {+ const file = join(root, `relay-${round}.json`);+ const log = join(root, `log-${round}`);+ writeFileSync(log, '');+ // The lock a crashed command left behind: its owner's process is gone.+ mkdirSync(`${file}.lock`);+ writeFileSync(join(`${file}.lock`, 'pid'), String(deadPid()));+ const waiters = Array.from({ length: 6 }, () => new Promise((done, fail) => {+ const child = spawn(process.execPath, ['--input-type=module', '-e', WAITER, LOCK_MODULE, log, file], { stdio: ['ignore', 'ignore', 'pipe'] });+ let stderr = '';+ child.stderr.on('data', part => { stderr += part; });+ child.once('exit', code => (code === 0 ? done() : fail(new Error(stderr))));+ }));+ await Promise.all(waiters);+ let holders = 0;+ const lines = readFileSync(log, 'utf8').trim().split('\n');+ for (const line of lines) {+ holders += line.startsWith('enter') ? 1 : -1;+ assert.ok(holders <= 1, `round ${round}: two waiters held the lock at once:\n${lines.join('\n')}`);+ }+ assert.equal(lines.filter(line => line.startsWith('enter')).length, 6, `round ${round}: every waiter held the lock once`);+ }+});