Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test(supervisor-stop-drain-5062): wait for a parsed pid, not the pid file

garrytan18e59536dda7
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1962f8b58a..a8c344841b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -19,6 +19,7 @@ identifiers and attribution are available in the pre-removal Git revision
- `src/core/minions/worker.ts` (+3 lines, ceiling 1777): `gracefulShutdown` records its reason; `launchJob` aborts a job it registers after the shutdown fired with that reason, so the job takes the same abort path as one that was already in flight (attempt burned, `delayed`; `UnrecoverableError` dead).
- `test/worker-shutdown-error-matrix.test.ts`: a forced case holds the committed claim's result until the watchdog has fired (both engines; fails on the previous worker, passes now).
- Docs: `docs/architecture/key-files/core-minions-2.md` worker entry.
+- `test/e2e/supervisor-stop-drain-5062.test.ts` flake: the test read each barrier pid file as soon as it existed, but `echo $$ > f` creates the file before the pid lands in it, so an empty read gave pid 0 and `process.kill(0, 0)` (this test's own process group) reported it alive after the drain. The reader now waits for a parsed pid, `alive` refuses a non-positive pid, and the barrier job forces the window (create, 300 ms, write) on every run. Root cause reported from the Cat 40 Hard wave (#6668).
## [0.60.218.0] - 2026-10-14
**Fencing legacy facts no longer leaks, loops or blocks.** When a legacy fact is adopted into a fence row that an earlier, interrupted run left behind, the row is now rebuilt from the stored fact, so the fact's own kind, dates, confidence, visibility and speaker attribution are kept. Before, a leftover row with a different visibility made the adoption refuse every time, and one with the same visibility overwrote the fact's metadata. A managed adoption that gbrain refused as a planning defect is no longer resubmitted every cycle, so the writer stops collecting one identical failed request per page per cycle. And one fact that can't be fenced now holds up only its own page: the phantom-redirect pass runs for the rest of the source.
diff --git a/test/e2e/supervisor-stop-drain-5062.test.ts b/test/e2e/supervisor-stop-drain-5062.test.ts
index bc730d6dae..b52b49d758 100644
--- a/test/e2e/supervisor-stop-drain-5062.test.ts
+++ b/test/e2e/supervisor-stop-drain-5062.test.ts
@@ -65,18 +65,38 @@ async function until(pred: () => Promise<boolean> | boolean, ms: number, what: s
}
function alive(pid: number): boolean {
+ if (!Number.isInteger(pid) || pid <= 0) throw new Error(`alive(${pid}): not a process id (kill(0, 0) would signal this test's own group)`);
try { process.kill(pid, 0); return true; } catch { return false; }
}
-function readPid(path: string): number {
- return Number(readFileSync(path, 'utf8').trim());
+/** `echo $$ > f` creates `f` before the pid lands in it, so an existence check can read an empty file. */
+function readPid(path: string): number | null {
+ if (!existsSync(path)) return null;
+ const pid = Number(readFileSync(path, 'utf8').trim());
+ return Number.isInteger(pid) && pid > 0 ? pid : null;
}
-/** Barrier shell job: records each start, then leader + grandchild ignore SIGTERM. */
+async function barrierTree(dir: string): Promise<number[]> {
+ let tree: number[] = [];
+ await until(() => {
+ const pids = [readPid(join(dir, 'leader.pid')), readPid(join(dir, 'grandchild.pid'))];
+ if (pids.some(pid => pid === null)) return false;
+ tree = pids as number[];
+ return true;
+ }, 30_000, 'barrier job start');
+ return tree;
+}
+
+/** Barrier shell job: records each start, then leader + grandchild ignore SIGTERM.
+ * Each pid file is created empty and written 300 ms later, forcing the window
+ * `echo $$ > f` opens on its own (file exists, pid not yet in it); a reader
+ * that trusts existence gets pid 0 and `kill(0, 0)` reports it alive forever. */
function barrierCmd(dir: string): string {
return [
`echo start >> ${dir}/starts`,
- `sh -c 'trap "" TERM; echo $$ > ${dir}/grandchild.pid; exec sleep 300' &`,
+ `sh -c 'trap "" TERM; : > ${dir}/grandchild.pid; sleep 0.3; echo $$ > ${dir}/grandchild.pid; exec sleep 300' &`,
+ `: > ${dir}/leader.pid`,
+ `sleep 0.3`,
`echo $$ > ${dir}/leader.pid`,
`trap "" TERM`,
`sleep 301`,
@@ -117,8 +137,7 @@ describeDatabase('#5062 supervisor and worker stop drains real work', () => {
'--health-interval', '0', '--max-rss', '0', '--pid-file', pidFile]);
await until(async () => (await lockRow(queueName)) !== null, 30_000, 'supervisor lock');
const job = await queue.add('shell', { cmd: barrierCmd(home), cwd: home }, { queue: queueName }, { allowProtectedSubmit: true });
- await until(() => existsSync(join(home, 'grandchild.pid')) && existsSync(join(home, 'leader.pid')), 30_000, 'barrier job start');
- const tree = [readPid(join(home, 'leader.pid')), readPid(join(home, 'grandchild.pid'))];
+ const tree = await barrierTree(home);
expect(tree.every(alive)).toBe(true);
return { home, queueName, pidFile, sup, job, tree };
}
@@ -195,8 +214,7 @@ describeDatabase('#5062 supervisor and worker stop drains real work', () => {
const job = await queue.add('shell', { cmd: barrierCmd(home), cwd: home }, { queue: queueName }, { allowProtectedSubmit: true });
const worker = spawnCli(home, ['jobs', 'work', '--queue', queueName, '--allow-shell-jobs', '--health-interval', '0', '--max-rss', '0']);
try {
- await until(() => existsSync(join(home, 'grandchild.pid')) && existsSync(join(home, 'leader.pid')), 30_000, 'barrier job start');
- const tree = [readPid(join(home, 'leader.pid')), readPid(join(home, 'grandchild.pid'))];
+ const tree = await barrierTree(home);
worker.proc.kill('SIGTERM');
expect(await worker.proc.exited, await worker.out()).toBe(0);
expect(tree.filter(alive)).toEqual([]);