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): publisher claims change hands only under the relay lock [H5]

Corey Olson21248a0047a3
diff --git a/src/relay-checkpoint-worker.js b/src/relay-checkpoint-worker.js
index 3c74b884..0b11fa8f 100644
--- a/src/relay-checkpoint-worker.js
+++ b/src/relay-checkpoint-worker.js
@@ -12,6 +12,7 @@ import {
utimesSync,
writeFileSync,
} from 'node:fs';
+import { locked } from './relay-lock.js';
import {
checkpointFiles,
checkpointQuietMs,
@@ -20,25 +21,36 @@ import {
import { publishLatestCheckpoint } from './relay.js';
const [root, file] = process.argv.slice(2);
-const { due, pid, log } = checkpointFiles(file);
+const { due, done, pid, log } = checkpointFiles(file);
/** Read the time of the last move, or 0 when none was noted. */
function lastMove() {
try { return Number(readFileSync(due, 'utf8')) || 0; } catch { return 0; }
}
-/** Claim the publisher's place for this board, replacing only a holder that has gone. */
-function claim() {
- for (let attempt = 0; attempt < 2; attempt += 1) {
- try { writeFileSync(pid, `${process.pid}\n`, { flag: 'wx', mode: 0o600 }); return true; }
- catch (error) {
- if (error.code !== 'EEXIST') throw error;
- const holder = publisherHolding(pid);
- if (holder && holder !== process.pid) return false;
- rmSync(pid, { force: true });
- }
- }
- return false;
+/** Read the move time the last upload covered, or 0. */
+function lastCovered() {
+ try { return Number(readFileSync(done, 'utf8')) || 0; } catch { return 0; }
+}
+
+/**
+ * Claim the publisher's place for this board. The decision is made under the relay lock, so two publishers that
+ * both find a dead holder's claim cannot each replace it: the second sees the first's live claim and stands down.
+ */
+async function claim() {
+ return locked(file, () => {
+ const holder = publisherHolding(pid);
+ if (holder && holder !== process.pid) return false;
+ writeFileSync(pid, `${process.pid}\n`, { mode: 0o600 });
+ return true;
+ }, io);
+}
+
+/** Give up the claim under the relay lock, and only while it is still this process's own. */
+async function release() {
+ await locked(file, () => {
+ try { if (Number(readFileSync(pid, 'utf8')) === process.pid) rmSync(pid, { force: true }); } catch { /* Already gone. */ }
+ }, io);
}
/** Keep a short record of failures beside the link; the next command's status reports pending uploads. */
@@ -54,20 +66,20 @@ async function publishUntilQuiet() {
// Refresh the claim, so a scheduler never takes this live publisher for a dead one.
try { utimesSync(pid, new Date(), new Date()); } catch { /* A removed claim is re-made by the next pass. */ }
const moved = lastMove();
+ // An earlier publisher already covered this move: nothing to send.
+ if (lastCovered() >= moved) return moved;
const wait = quiet - (Date.now() - moved);
if (wait > 0) { await new Promise(next => setTimeout(next, wait)); continue; }
- await publishLatestCheckpoint(root, io);
+ if (await publishLatestCheckpoint(root, io)) writeFileSync(done, `${moved}\n`, { mode: 0o600 });
if (lastMove() === moved) return moved;
}
}
// A move noted after the last look, but before the claim was released, saw a live publisher and spawned none:
// look once more after releasing, and go again if it is newer than what was covered.
-for (let covered = -1; claim();) {
+for (let covered = -1; await claim();) {
try { covered = await publishUntilQuiet(); }
catch (error) { io.err(`checkpoint not published: ${error.message}`); covered = Infinity; }
- finally {
- try { if (Number(readFileSync(pid, 'utf8')) === process.pid) rmSync(pid, { force: true }); } catch { /* Already gone. */ }
- }
+ finally { await release(); }
if (lastMove() <= covered) break;
}
diff --git a/src/relay-checkpoint.js b/src/relay-checkpoint.js
index 6e7a301f..82bfa031 100644
--- a/src/relay-checkpoint.js
+++ b/src/relay-checkpoint.js
@@ -49,14 +49,16 @@ export function checkpointQuietMs(env = process.env) {
}
/**
- * The files the publisher keeps beside the relay link: when the last move happened, and which process publishes.
+ * The files the publisher keeps beside the relay link: when the last move happened, which move the last upload
+ * covered, and which process publishes.
*
* @param {string} file - The relay link file.
- * @returns {{ due: string, pid: string, log: string }}
+ * @returns {{ due: string, done: string, pid: string, log: string }}
*/
export function checkpointFiles(file) {
const directory = dirname(file);
- return { due: join(directory, 'relay-checkpoint.due'), pid: join(directory, 'relay-checkpoint.pid'), log: join(directory, 'relay-checkpoint.log') };
+ return { due: join(directory, 'relay-checkpoint.due'), done: join(directory, 'relay-checkpoint.done'),
+ pid: join(directory, 'relay-checkpoint.pid'), log: join(directory, 'relay-checkpoint.log') };
}
// Budget: a publisher refreshes its claim on every pass, far more often than this; an older claim belongs to a
diff --git a/test/relay-checkpoint.test.js b/test/relay-checkpoint.test.js
index df0b0a3c..1fe4ddc1 100644
--- a/test/relay-checkpoint.test.js
+++ b/test/relay-checkpoint.test.js
@@ -1,6 +1,8 @@
/** A move sends only its own event; one background publisher sends the checkpoint once moves go quiet [H5]. */
import assert from 'node:assert/strict';
-import { existsSync, readFileSync } from 'node:fs';
+import { existsSync, readFileSync, writeFileSync } from 'node:fs';
+import { spawn } from 'node:child_process';
+import { fileURLToPath } from 'node:url';
import { DatabaseSync } from 'node:sqlite';
import { dirname, join } from 'node:path';
import { test } from 'node:test';
@@ -98,3 +100,24 @@ test('a move during a held checkpoint upload returns before the upload ends [H5]
box.holdPublishes(false);
await publisherDone(box, from, 2);
});
+
+test('publishers that find a dead holder\'s claim together upload one checkpoint between them [H5]', async (t) => {
+ const inline = process.env[CHECKPOINT_BACKGROUND_ENV];
+ process.env[CHECKPOINT_BACKGROUND_ENV] = '0'; // This case starts its publishers itself.
+ t.after(() => { if (inline === undefined) delete process.env[CHECKPOINT_BACKGROUND_ENV]; else process.env[CHECKPOINT_BACKGROUND_ENV] = inline; });
+ const box = await relayClientFixture(t);
+ await box.link();
+ assert.equal((await box.cli('shout', 'all', 'a move whose checkpoint is due')).code, 0);
+ const directory = dirname(box.linkFile);
+ // A claim left by a publisher that died, and a move older than the quiet period.
+ writeFileSync(join(directory, 'relay-checkpoint.pid'), '999999\n');
+ writeFileSync(join(directory, 'relay-checkpoint.due'), `${Date.now() - 60_000}\n`);
+ const from = box.calls.length;
+ const worker = fileURLToPath(new URL('../src/relay-checkpoint-worker.js', import.meta.url));
+ const exits = Array.from({ length: 4 }, () => new Promise(done => {
+ spawn(process.execPath, [worker, box.root, box.linkFile], { cwd: box.root, env: box.env, stdio: 'ignore' }).once('exit', done);
+ }));
+ await Promise.all(exits);
+ assert.equal(box.calls.slice(from).filter(checkpointUpload).length, 1, 'four publishers recovering one dead claim sent one checkpoint');
+ assert.equal(existsSync(join(directory, 'relay-checkpoint.pid')), false, 'the claim is released');
+});