Skip to content

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.

fix(relay): an ownerless lock past the stale age is recovered [H5]

Corey Olson1e0f775d149a
diff --git a/src/relay-lock.js b/src/relay-lock.js
index 83be21e1..04fc620d 100644
--- a/src/relay-lock.js
+++ b/src/relay-lock.js
@@ -75,7 +75,15 @@ function takeDirectory(lock) {
try { mkdirSync(lock, { mode: 0o700 }); writeFileSync(join(lock, 'pid'), String(process.pid)); return true; }
catch (error) { if (error.code !== 'EEXIST') throw error; }
let owner = null;
- try { owner = ownerOf(lock); } catch (error) { if (error.code === 'ENOENT') return false; }
+ try { owner = ownerOf(lock); }
+ catch (error) {
+ // No pid file: either the lock is gone, or its maker died between creating it and naming itself, which the
+ // age check below recovers.
+ if (error.code !== 'ENOENT') owner = null;
+ else {
+ try { lstatSync(lock); } catch (missing) { if (missing.code === 'ENOENT') return false; throw missing; }
+ }
+ }
let stale;
try { stale = owner !== null ? gone(owner) : Date.now() - lstatSync(lock).mtimeMs > UNOWNED_STALE_MS; }
catch (error) { if (error.code === 'ENOENT') return false; throw error; }
diff --git a/test/relay-lock.test.js b/test/relay-lock.test.js
index 5d4c546a..3af965a8 100644
--- a/test/relay-lock.test.js
+++ b/test/relay-lock.test.js
@@ -1,6 +1,6 @@
/** 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 { mkdirSync, mkdtempSync, readFileSync, rmSync, utimesSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { spawn } from 'node:child_process';
@@ -73,3 +73,22 @@ test('a lease left by a dead holder is taken over by one waiter at a time [H5]',
t.after(() => rmSync(root, { recursive: true, force: true }));
for (let index = 0; index < 10; index += 1) await round(root, index, { deadLease: true });
});
+
+test('a lock left with no owner by a command that died making it is recovered once it is old [H5]', async (t) => {
+ const root = mkdtempSync(join(tmpdir(), 'pullboard-relay-lock-unowned-'));
+ t.after(() => rmSync(root, { recursive: true, force: true }));
+ const file = join(root, 'relay.json');
+ const log = join(root, 'log');
+ writeFileSync(log, '');
+ // The command made the directory and died before writing its pid; the lock is past the stale age.
+ mkdirSync(`${file}.lock`);
+ const old = new Date(Date.now() - 120_000);
+ utimesSync(`${file}.lock`, old, old);
+ await 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))));
+ });
+ assert.equal(readFileSync(log, 'utf8').trim().split('\n').filter(line => line.startsWith('enter')).length, 1, 'the waiter took the recovered lock');
+});