Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: treat zombie group members as cleared when verifying worker teardown (#12826)

Adam Eivy72459d770296
diff --git a/server/services/codeAnimation/containedWorker.js b/server/services/codeAnimation/containedWorker.js
index 39e68b1783..f77c2a0919 100644
--- a/server/services/codeAnimation/containedWorker.js
+++ b/server/services/codeAnimation/containedWorker.js
@@ -7,7 +7,7 @@
* trusted-local entry point requires explicit server-side operator authorization;
* it reuses supervision but provides no host filesystem/network boundary.
*/
-import { constants } from 'node:fs';
+import { constants, readdirSync, readFileSync } from 'node:fs';
import { access, lstat, mkdir, open, readFile, readdir, realpath, rm } from 'node:fs/promises';
import { join } from 'node:path';
import { randomUUID } from 'node:crypto';
@@ -140,10 +140,35 @@ async function residentBytes(pid, processGroup = false) {
return Number.isFinite(kib) ? kib * 1024 : 0;
}
-function processAlive(pid) {
- try { process.kill(pid, 0); return true; } catch (error) { return error.code === 'EPERM'; }
+// Linux: [state, pgrp] of a host-visible pid, or null when it is gone or unreadable.
+function procStat(pid) {
+ try {
+ const fields = readFileSync(`/proc/${pid}/stat`, 'utf8').replace(/^.*\)\s+/s, '').split(' ');
+ return [fields[0], Number(fields[2])];
+ } catch { return null; }
+}
+// A reaped-but-unwaited child is a zombie: it holds a pid slot, not a process.
+// Under a PID 1 that never wait()s (a container without an init), killed
+// workers linger as zombies forever, so they must not count as alive.
+const isZombie = (state) => state === 'Z' || state === 'X';
+
+export function processAlive(pid) {
+ try { process.kill(pid, 0); } catch (error) { return error.code === 'EPERM'; }
+ if (process.platform !== 'linux') return true;
+ const stat = procStat(pid);
+ return !stat || !isZombie(stat[0]);
+}
+
+function groupAlive(pgid) {
+ try { process.kill(-pgid, 0); } catch (error) { return error.code === 'EPERM'; }
+ if (process.platform !== 'linux') return true;
+ let pids;
+ try { pids = readdirSync('/proc').filter((name) => /^\d+$/.test(name)); } catch { return true; }
+ return pids.some((pid) => {
+ const stat = procStat(pid);
+ return stat && stat[1] === pgid && !isZombie(stat[0]);
+ });
}
-const groupAlive = (pgid) => processAlive(-pgid);
const tail = () => {
let text = '';
diff --git a/server/services/codeAnimation/containedWorker.test.js b/server/services/codeAnimation/containedWorker.test.js
index 2ad8fa47c9..95d7e1e33d 100644
--- a/server/services/codeAnimation/containedWorker.test.js
+++ b/server/services/codeAnimation/containedWorker.test.js
@@ -2,7 +2,7 @@ import { afterAll, afterEach, describe, expect, it, vi } from 'vitest';
import { mkdtemp, readdir, readFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
-import { currentContainmentMechanism, runContainedWorker, runTrustedLocalWorker } from './containedWorker.js';
+import { currentContainmentMechanism, processAlive, runContainedWorker, runTrustedLocalWorker } from './containedWorker.js';
// Real-mechanism suite: these run only where the enforced sandbox exists. The
// adversarial boundary and limit checks run end to end through the probe route
@@ -142,7 +142,7 @@ setInterval(() => {}, 1000);
const evidence = JSON.parse(result.stdout.trim());
expect(evidence.child).toBeGreaterThan(0);
expect(evidence.token).toBe(false);
- expect(() => process.kill(evidence.child, 0)).toThrow();
+ expect(processAlive(evidence.child)).toBe(false);
expect(await readdir(root)).toEqual([]);
}, 20000);
});