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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (codex): surface spawnDetached setup failures via the handle's error event, not a reject (#1319)
[._.]/ Adam Eivybd4679d68a6c
diff --git a/server/lib/detachedSpawn.js b/server/lib/detachedSpawn.jsindex d8222f4c9a..f2468063e6 100644--- a/server/lib/detachedSpawn.js+++ b/server/lib/detachedSpawn.js@@ -110,15 +110,6 @@ export async function spawnDetached(bin, args = [], { env, cwd, controlDir, pollreturn spawn(bin, args, { env, cwd, stdio: ['ignore', 'pipe', 'pipe'] });}- await ensureDir(controlDir);- // Clear any stale control files from a prior run reusing this dir, so the- // PID/exit pollers can't latch onto a previous job's results.- const pidFile = join(controlDir, 'pid');- const exitFile = join(controlDir, 'exit');- const stdoutLog = join(controlDir, 'stdout.log');- const stderrLog = join(controlDir, 'stderr.log');- await Promise.all([pidFile, exitFile, stdoutLog, stderrLog].map((f) => rm(f, { force: true })));-const handle = new EventEmitter();handle.stdout = new EventEmitter();handle.stderr = new EventEmitter();@@ -126,6 +117,30 @@ export async function spawnDetached(bin, args = [], { env, cwd, controlDir, pollhandle.killed = false;handle.exitCode = null;handle.signalCode = null;+ // Default no-op so a setup failure (below) returns a usable handle.+ handle.kill = () => false;++ const pidFile = join(controlDir, 'pid');+ const exitFile = join(controlDir, 'exit');+ const stdoutLog = join(controlDir, 'stdout.log');+ const stderrLog = join(controlDir, 'stderr.log');++ // Setup is filesystem I/O that can fail (permissions, a stale non-dir path,+ // disk full). Surface those as the handle's 'error' event — like a real+ // ChildProcess spawn failure — rather than rejecting: callers attach+ // `on('error')` AFTER the await for their finalization/cleanup, and a reject+ // would bypass that and strand a `running` run or leak temp files. Deferred+ // so the listener is attached before it fires.+ const ensureControlDir = await ensureDir(controlDir).then(+ () => Promise.all([pidFile, exitFile, stdoutLog, stderrLog].map((f) => rm(f, { force: true }))),+ ).then(() => null, (err) => err);+ if (ensureControlDir) {+ setImmediate(() => {+ if (cleanup) rm(controlDir, { recursive: true, force: true }).catch(() => {});+ handle.emit('error', ensureControlDir);+ });+ return handle;+ }// Launch the double-fork. `detached` gives the outer sh its own session;// `stdio: 'ignore'` because all job output is redirected to files by thediff --git a/server/lib/detachedSpawn.test.js b/server/lib/detachedSpawn.test.jsindex ee5417476b..5508286244 100644--- a/server/lib/detachedSpawn.test.js+++ b/server/lib/detachedSpawn.test.js@@ -1,5 +1,5 @@import { describe, it, expect, afterEach } from 'vitest';-import { mkdtemp, rm, stat } from 'fs/promises';+import { mkdtemp, rm, stat, writeFile } from 'fs/promises';import { tmpdir } from 'os';import { join } from 'path';import { execFile } from 'child_process';@@ -142,6 +142,19 @@ describe('spawnDetached', () => {await expect(spawnDetached('sh', ['-c', 'true'], {})).rejects.toThrow(/controlDir/);});+ it('surfaces a setup failure as an error event, not a rejection', async () => {+ // controlDir under a regular FILE → ensureDir fails (ENOTDIR). spawnDetached+ // must still resolve a handle and emit 'error' so the caller's on('error')+ // finalization runs (rejecting would strand the run / leak temps).+ const base = await tmpControlDir();+ const filePath = join(base, 'not-a-dir');+ await writeFile(filePath, 'x');+ const handle = await spawnDetached('sh', ['-c', 'true'], { controlDir: join(filePath, 'sub') });+ const err = await new Promise((resolve) => handle.on('error', resolve));+ expect(err).toBeInstanceOf(Error);+ expect(handle.pid).toBeNull();+ });+it('falls back to a plain spawn on win32 (no POSIX sh double-fork)', async () => {const original = process.platform;Object.defineProperty(process, 'platform', { value: 'win32', configurable: true });