yetihan (#1198): a lock whose owner is a Linux zombie was held for good. process.kill(pid, 0) succeeds on a zombie, so 15 packages' lock and liveness checks counted an exited, unreaped owner as alive. - scripts/proc.mjs: processStatus(pid) is dead on ESRCH, and on Linux also when /proc/<pid>/stat says Z or X; EPERM is "permission", any other error "unknown". processAlive / processExists keep each caller's old EPERM and unknown-error meaning. - scripts/sync-proc.mjs copies it into each package (--check in the tests); every package's own check keeps its old semantics, now with zombies dead. agent-console's daemon bundle and window-warmup's dist are rebuilt. - 15 packages bumped. Review run at 28ef6622 (mote, HOME=$(mktemp -d)): - node scripts/sync-proc.mjs --check: ok. - bun test (whole repo): 8934 pass, 0 fail. - bun scripts/check.mjs: every package ok. - window-warmup's build reproduces its dist/plugin.js byte for byte. agent-console's bundle diff read line by line: the proc module plus esbuild's renumbered imports, nothing else (esbuild isn't installed on mote, so it wasn't rebuilt there). - Real zombie on Linux (WSL, magpie-desktop): a bash child left unreaped by an exec'd sleep, /proc stat "Z": the old kill(0) check says alive, processStatus says dead; the running process says alive. Not tried: each package's lock contention end to end.
50 lines
1.9 KiB
JavaScript
50 lines
1.9 KiB
JavaScript
import fs from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
import { randomUUID } from 'node:crypto';
|
|
import { processExists } from './proc.mjs';
|
|
|
|
export async function readJSON(file) {
|
|
try { return JSON.parse(await fs.readFile(file, 'utf8')); }
|
|
catch (error) { if (error.code === 'ENOENT') return null; throw error; }
|
|
}
|
|
|
|
export async function writeJSON(file, value) {
|
|
await fs.mkdir(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
const temp = `${file}.${randomUUID()}.tmp`;
|
|
try {
|
|
const handle = await fs.open(temp, 'wx', 0o600);
|
|
try { await handle.writeFile(`${JSON.stringify(value, null, 2)}\n`); await handle.sync(); }
|
|
finally { await handle.close(); }
|
|
await fs.rename(temp, file);
|
|
} finally { await fs.rm(temp, { force: true }); }
|
|
}
|
|
|
|
export async function withLock(stateFile, run) {
|
|
const lock = `${stateFile}.lock`;
|
|
await fs.mkdir(path.dirname(lock), { recursive: true, mode: 0o700 });
|
|
let handle;
|
|
for (let attempt = 0; attempt < 2; attempt++) {
|
|
try { handle = await fs.open(lock, 'wx', 0o600); break; }
|
|
catch (error) {
|
|
if (error.code !== 'EEXIST') throw error;
|
|
let stale = false;
|
|
try {
|
|
const owner = await readJSON(lock);
|
|
if (Number.isSafeInteger(owner?.pid) && owner.pid > 0) {
|
|
stale = !processExists(owner.pid);
|
|
}
|
|
} catch (e) {
|
|
if (e.code === 'ENOENT') continue;
|
|
// A process can die between creating and filling the lock.
|
|
if (e instanceof SyntaxError) stale = Date.now() - (await fs.stat(lock)).mtimeMs > 30000;
|
|
else throw e;
|
|
}
|
|
if (!stale) throw new Error('Another Nowledge sync is running; retry shortly.');
|
|
await fs.rm(lock, { force: true });
|
|
}
|
|
}
|
|
if (!handle) throw new Error('Could not acquire Nowledge sync lock.');
|
|
try { await handle.writeFile(JSON.stringify({ pid: process.pid })); return await run(); }
|
|
finally { await handle.close(); await fs.rm(lock, { force: true }); }
|
|
}
|