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: retire completed detached launches when a zombie holds the process group (#9225)

[._.]/ Adam Eivya130e8551983
diff --git a/server/lib/databaseWriterRegistry.js b/server/lib/databaseWriterRegistry.js
index 727cb7aa62..41bc9b0f51 100644
--- a/server/lib/databaseWriterRegistry.js
+++ b/server/lib/databaseWriterRegistry.js
@@ -179,7 +179,13 @@ const START_SLACK_MS = 2000;
* job whose own group was never recorded, or an identity mismatch (possible
* PID reuse). Never safe to signal or to ignore.
*/
-export function classifyWriterQuiescence(row, processes) {
+// A zombie (ps state Z) is a reaped-by-nobody corpse: it holds no files open and
+// cannot write, so it must not keep a process group "occupied". Hosts whose
+// PID 1 does not reap orphans (containers without an init) leave them forever.
+export const isLiveProcess = proc => !String(proc.state ?? '').startsWith('Z');
+
+export function classifyWriterQuiescence(row, allProcesses) {
+ const processes = allProcesses.filter(isLiveProcess);
if (row.kind === 'retired-exits') return { verdict: 'ambiguous', groups: [] };
if (row.state === 'abandoned') return { verdict: 'quiescent', groups: [] };
if (row.state === 'unresolved') return { verdict: 'pending', groups: [] };
diff --git a/server/lib/databaseWriterRegistry.test.js b/server/lib/databaseWriterRegistry.test.js
index a65b9a7e1c..8015bf458a 100644
--- a/server/lib/databaseWriterRegistry.test.js
+++ b/server/lib/databaseWriterRegistry.test.js
@@ -4,7 +4,7 @@ import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { fileURLToPath } from 'node:url';
import { spawn, spawnSync } from 'node:child_process';
-import { createDatabaseWriterRegistry } from './databaseWriterRegistry.js';
+import { classifyWriterQuiescence, createDatabaseWriterRegistry } from './databaseWriterRegistry.js';
import { createDatabaseMaintenanceJournal } from './databaseMaintenanceJournal.js';
import { spawnDetached } from './detachedSpawn.js';
@@ -155,3 +155,14 @@ describe('durable detached launch admission', () => {
}
});
});
+
+describe('classifyWriterQuiescence zombies', () => {
+ const row = { kind: 'launch', state: 'exited', launcherPid: 4242, processGroup: false, createdAt: new Date().toISOString() };
+ const member = state => ({ pid: 4243, pgid: 4242, state, startedAt: Date.now(), command: 'sh' });
+
+ it('treats a zombie holding a recorded group as quiescent, but a live member as orphaned', () => {
+ expect(classifyWriterQuiescence(row, [member('Z')]).verdict).toBe('quiescent');
+ expect(classifyWriterQuiescence(row, [member('Z+')]).verdict).toBe('quiescent');
+ expect(classifyWriterQuiescence(row, [member('S')]).verdict).toBe('orphaned');
+ });
+});
diff --git a/server/lib/detachedSpawn.js b/server/lib/detachedSpawn.js
index 440d437689..15fc6274a5 100644
--- a/server/lib/detachedSpawn.js
+++ b/server/lib/detachedSpawn.js
@@ -523,16 +523,16 @@ const markAbandoned = (writer) => {
// Process-table snapshot, POSIX only. `lstart` is fixed-format under LC_ALL=C;
// an unparseable row throws rather than being skipped, because a skipped row
// could be exactly the surviving writer a caller is looking for.
-const PS_ROW = /^\s*(\d+)\s+(\d+)\s+(\w{3} \w{3}\s+\d{1,2} \d{2}:\d{2}:\d{2} \d{4})\s?(.*)$/;
+const PS_ROW = /^\s*(\d+)\s+(\d+)\s+(\S+)\s+(\w{3} \w{3}\s+\d{1,2} \d{2}:\d{2}:\d{2} \d{4})\s?(.*)$/;
export async function snapshotProcesses() {
if (process.platform === 'win32') throw new Error('Process-group snapshots are unavailable on Windows');
- const { stdout } = await execFileAsync('ps', ['-A', '-ww', '-o', 'pid=', '-o', 'pgid=', '-o', 'lstart=', '-o', 'command='],
+ const { stdout } = await execFileAsync('ps', ['-A', '-ww', '-o', 'pid=', '-o', 'pgid=', '-o', 'stat=', '-o', 'lstart=', '-o', 'command='],
safeChildProcessOptions({ timeout: 15000, maxBuffer: 32 * 1024 * 1024, env: { ...process.env, LC_ALL: 'C' } }));
const rows = stdout.split('\n').filter(line => line.trim()).map((line) => {
const match = PS_ROW.exec(line);
- const startedAt = match ? Date.parse(match[3]) : NaN;
+ const startedAt = match ? Date.parse(match[4]) : NaN;
if (!match || !Number.isFinite(startedAt)) throw new Error('Unreadable process table');
- return { pid: Number(match[1]), pgid: Number(match[2]), startedAt, command: match[4] };
+ return { pid: Number(match[1]), pgid: Number(match[2]), state: match[3], startedAt, command: match[5] };
});
if (!rows.some(row => row.pid === process.pid)) throw new Error('Incomplete process table');
return rows;
diff --git a/server/lib/detachedSpawn.test.js b/server/lib/detachedSpawn.test.js
index 49490ce3bc..f7c0ab7f96 100644
--- a/server/lib/detachedSpawn.test.js
+++ b/server/lib/detachedSpawn.test.js
@@ -1056,3 +1056,12 @@ describe.runIf(IS_POSIX)('reattachDetached', () => {
await originalClosed.catch(() => {});
});
});
+
+describe.runIf(IS_POSIX)('snapshotProcesses', () => {
+ it('reports the ps state of each row and includes this process', async () => {
+ const { snapshotProcesses } = await import('./detachedSpawn.js');
+ const own = (await snapshotProcesses()).find(row => row.pid === process.pid);
+ expect(own).toMatchObject({ pgid: expect.any(Number), state: expect.stringMatching(/^[A-Za-z]/) });
+ expect(Number.isFinite(own.startedAt)).toBe(true);
+ });
+});
diff --git a/server/services/databaseMaintenanceQuiescence.js b/server/services/databaseMaintenanceQuiescence.js
index 8e7d682885..d95731ee6a 100644
--- a/server/services/databaseMaintenanceQuiescence.js
+++ b/server/services/databaseMaintenanceQuiescence.js
@@ -1,7 +1,7 @@
import { sep } from 'node:path';
import { PATHS } from '../lib/paths.js';
import { createDatabaseMaintenanceJournal } from '../lib/databaseMaintenanceJournal.js';
-import { classifyWriterQuiescence, createDatabaseWriterRegistry } from '../lib/databaseWriterRegistry.js';
+import { classifyWriterQuiescence, createDatabaseWriterRegistry, isLiveProcess } from '../lib/databaseWriterRegistry.js';
import { isDetachedSupervisorCommand, signalProcessGroup, snapshotProcesses } from '../lib/detachedSpawn.js';
import { sleep } from '../lib/fileUtils.js';
import { stopOwnedDatabaseProducers } from './databaseMaintenanceProducers.js';
@@ -75,7 +75,7 @@ export async function reconcileDetachedWriters(id, token, { graceMs = TERMINATE_
for (const group of live) signalProcessGroup(group, signal);
await sleep(pollMs);
processes = await snapshotProcesses();
- live = new Set([...live].filter(group => processes.some(proc => proc.pgid === group)));
+ live = new Set([...live].filter(group => processes.some(proc => isLiveProcess(proc) && proc.pgid === group)));
if (signal === 'SIGTERM' && Date.now() >= deadline) signal = 'SIGKILL';
else if (signal === 'SIGKILL' && Date.now() >= deadline + graceMs) throw refused('writer process groups did not terminate');
}
@@ -122,7 +122,7 @@ export async function assertPredecessorCoordinatorsStopped(id, token, { graceMs
const processes = await snapshotProcesses();
const own = processes.find(proc => proc.pid === process.pid)?.pgid;
// A recorded ID equal to our own group was empty when ours was created.
- const occupied = processes.some(proc => proc.pgid !== own && groups.has(proc.pgid));
+ const occupied = processes.some(proc => isLiveProcess(proc) && proc.pgid !== own && groups.has(proc.pgid));
consecutive = occupied ? 0 : consecutive + 1;
if (occupied && Date.now() >= deadline) throw refused('a previous coordinator\'s dump or import process is still running');
if (consecutive < 2) await sleep(pollMs);