Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test: wait for a pid in the process-tree pid files, never read an empty file as pid 0

garrytan041d2659dafe
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9e2ab790af..232bed9f7a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -20,6 +20,7 @@ identifiers and attribution are available in the pre-removal Git revision
|---|---|---|
| A PR changes a widely imported type (any new error code) | master's next typecheck was effectively cold (121 s) inside verify's 120 s per-check cap, and it timed out; a timed-out tsc saves no state, so a rerun restored the same stale cache | tsc runs in test.yml's `typecheck` job, in parallel with verify, under a 6-minute step timeout (3x the worst cold CI time); the job saves its tsbuildinfo on every outcome; `test-status`, and with it the release gate, requires the job |
| A crash-robot count role sees the git effect of `finish`'s probe write | the run role could exit before that effect ran, so a fault planned there went unreached (`authority_change_pending_effects` seed 5105, `effect:git:mid#4`) | `finish` drains the probe's effects, so every seam the count role sees comes again in the run; a probe whose effects do not drain is a `wedge` violation |
+| `test/minions-shell-process-tree.test.ts` reads a pid file the shell has created but not yet written | the empty read became pid 0, which counted as a live process, and the cleanup sent SIGKILL to pid 0, the test's own process group (the shard died with exit 137) | the test waits until each file holds a positive pid |
### What you need to do
diff --git a/test/minions-shell-process-tree.test.ts b/test/minions-shell-process-tree.test.ts
index b62e0a1276..e12f7c881e 100644
--- a/test/minions-shell-process-tree.test.ts
+++ b/test/minions-shell-process-tree.test.ts
@@ -9,7 +9,7 @@
*/
import { describe, test, expect, afterEach } from 'bun:test';
-import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
+import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import type { MinionJobContext } from '../src/core/minions/types.ts';
@@ -39,15 +39,24 @@ function alive(pid: number): boolean {
try { process.kill(pid, 0); return true; } catch { return false; }
}
-async function waitForFiles(paths: string[], ms = 10_000): Promise<void> {
+// `echo $$ > f` creates f before it writes the pid, so an existing file can still be empty. An empty read is pid 0,
+// which alive() reports live (kill(0, 0) signals the process group) and the cleanup's SIGKILL would send to this
+// test's own process group. Wait for every file to hold a pid.
+async function waitForPids(paths: string[], ms = 10_000): Promise<number[]> {
const deadline = Date.now() + ms;
- while (!paths.every(existsSync)) {
- if (Date.now() > deadline) throw new Error(`timed out waiting for ${paths.join(', ')}`);
+ for (;;) {
+ const pids = paths.map(path => existsSync(path) ? Number(readFileSync(path, 'utf8').trim()) : NaN);
+ if (pids.every(n => Number.isInteger(n) && n > 0)) return pids;
+ if (Date.now() > deadline) throw new Error(`timed out waiting for a pid in ${paths.join(', ')}`);
await new Promise(r => setTimeout(r, 25));
}
}
-const pid = (path: string) => Number(readFileSync(path, 'utf8').trim());
+const pid = (path: string) => {
+ const n = Number(readFileSync(path, 'utf8').trim());
+ if (!Number.isInteger(n) || n <= 0) throw new Error(`${path} holds no pid`);
+ return n;
+};
const dirs: string[] = [];
const leftovers: number[] = [];
afterEach(() => {
@@ -61,6 +70,14 @@ function scratch(): string {
}
describePosix('#5062 shell handler process tree', () => {
+ test('a pid file that exists before its pid is written is waited for, never read as pid 0', async () => {
+ const d = scratch();
+ const path = join(d, 'late.pid');
+ writeFileSync(path, '');
+ setTimeout(() => writeFileSync(path, `${process.pid}\n`), 200);
+ expect(await waitForPids([path])).toEqual([process.pid]);
+ });
+
test('abort kills a leader and grandchild that both ignore SIGTERM (escalates by liveness)', async () => {
const d = scratch();
const cmd = [
@@ -72,8 +89,7 @@ describePosix('#5062 shell handler process tree', () => {
await withEnv({ GBRAIN_ALLOW_SHELL_JOBS: '1' }, async () => {
const ac = new AbortController();
const run = shellHandler(makeCtx({ cmd, cwd: d }, ac.signal));
- await waitForFiles([`${d}/leader.pid`, `${d}/grandchild.pid`]);
- const tree = [pid(`${d}/leader.pid`), pid(`${d}/grandchild.pid`)];
+ const tree = await waitForPids([`${d}/leader.pid`, `${d}/grandchild.pid`]);
leftovers.push(...tree);
ac.abort(new Error('cancel'));
await expect(run).rejects.toThrow(/aborted/);
@@ -91,8 +107,7 @@ describePosix('#5062 shell handler process tree', () => {
await withEnv({ GBRAIN_ALLOW_SHELL_JOBS: '1' }, async () => {
const ac = new AbortController();
const run = shellHandler(makeCtx({ cmd, cwd: d }, ac.signal));
- await waitForFiles([`${d}/leader.pid`, `${d}/grandchild.pid`]);
- const grandchild = pid(`${d}/grandchild.pid`);
+ const [, grandchild] = await waitForPids([`${d}/leader.pid`, `${d}/grandchild.pid`]);
leftovers.push(grandchild);
ac.abort(new Error('cancel'));
await expect(run).rejects.toThrow(/aborted/);