π The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
fix(daemon): atomic lockfile + drop init.ts:424 shell `&` (#2407)
ruv7cafd268f8e1
diff --git a/v3/@claude-flow/cli/src/commands/daemon.ts b/v3/@claude-flow/cli/src/commands/daemon.tsindex f6b686f3d4..d49450b96d 100644--- a/v3/@claude-flow/cli/src/commands/daemon.ts+++ b/v3/@claude-flow/cli/src/commands/daemon.ts@@ -92,16 +92,70 @@ const startCommand: Command = {}// Check if background daemon already running (skip if we ARE the daemon process)+ //+ // #2407 β without an atomic lockfile, N concurrent `daemon start` calls+ // (devcontainer setup + VS Code task + MCP hook within ~500 ms) all see+ // an empty PID file in the same instant, all proceed past this dedup,+ // and all fork their own background daemon. One incident accumulated+ // 39 zombie daemons holding ~8.5 GiB β kernel panic.+ //+ // Wrap the check + the subsequent fork in an O_EXCL lockfile so the+ // dedup is process-atomic. Lock holder gets to spawn; competing callers+ // see EEXIST, wait briefly, then re-read the PID file (which the holder+ // has now written), and return "already running" cleanly.if (!isDaemonProcess) {- const bgPid = getBackgroundDaemonPid(projectRoot);- if (bgPid && isProcessRunning(bgPid)) {- if (!quiet) {- output.printWarning(`Daemon already running in background (PID: ${bgPid}). Stop it first with: daemon stop`);+ const stateDir = join(resolve(projectRoot), '.claude-flow');+ const lockFile = join(stateDir, 'daemon.lock');+ try { fs.mkdirSync(stateDir, { recursive: true }); } catch { /* exists */ }+ let lockFd: number | null = null;+ try {+ lockFd = fs.openSync(lockFile, fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_WRONLY);+ fs.writeSync(lockFd, String(process.pid));+ } catch (e) {+ if ((e as NodeJS.ErrnoException).code === 'EEXIST') {+ // Another `daemon start` is mid-spawn. Wait up to 5s for it to+ // finish, then re-check the PID file. If the holder crashed+ // mid-spawn, fall through and reset; killStaleDaemons + a fresh+ // attempt will recover.+ const deadline = Date.now() + 5000;+ while (Date.now() < deadline) {+ await new Promise((r) => setTimeout(r, 100));+ const winnerPid = getBackgroundDaemonPid(projectRoot);+ if (winnerPid && isProcessRunning(winnerPid)) {+ if (!quiet) {+ output.printWarning(`Daemon already running in background (PID: ${winnerPid}). Stop it first with: daemon stop`);+ }+ return { success: true };+ }+ }+ // Stale lockfile from a crashed prior attempt β clear it and+ // proceed without a held lock. Worst case we double-spawn ONCE+ // and the killStaleDaemons sweep below cleans up.+ try { fs.unlinkSync(lockFile); } catch { /* ignore */ }+ } else {+ throw e;+ }+ }+ try {+ const bgPid = getBackgroundDaemonPid(projectRoot);+ if (bgPid && isProcessRunning(bgPid)) {+ if (!quiet) {+ output.printWarning(`Daemon already running in background (PID: ${bgPid}). Stop it first with: daemon stop`);+ }+ return { success: true };+ }+ // #1551: Kill any stale daemon processes that weren't tracked by PID file+ await killStaleDaemons(projectRoot, quiet);+ } finally {+ // Release the lock so the lock-loser can re-check (or the next+ // legitimate invocation can spawn). startBackgroundDaemon below+ // writes the PID file synchronously before returning so the+ // post-release re-check finds it.+ if (lockFd !== null) {+ try { fs.closeSync(lockFd); } catch { /* ignore */ }+ try { fs.unlinkSync(lockFile); } catch { /* ignore */ }}- return { success: true };}- // #1551: Kill any stale daemon processes that weren't tracked by PID file- await killStaleDaemons(projectRoot, quiet);}// Background mode (default): fork a detached process.diff --git a/v3/@claude-flow/cli/src/commands/init.ts b/v3/@claude-flow/cli/src/commands/init.tsindex bbd5626384..f1a3028073 100644--- a/v3/@claude-flow/cli/src/commands/init.ts+++ b/v3/@claude-flow/cli/src/commands/init.ts@@ -417,17 +417,38 @@ const initAction = async (ctx: CommandContext): Promise<CommandResult> => {}}- // Start daemon+ // Start daemon β #2407 fix+ //+ // The previous version used `daemon start ... &` (shell background)+ // which made execSync return as soon as the shell forked, BEFORE the+ // daemon process wrote its PID file. Concurrent init runs+ // (devcontainer setup + VS Code task + MCP hook firing within ~500 ms)+ // all saw an empty PID file via getBackgroundDaemonPid(), so+ // daemon.ts:99-103's dedup short-circuit didn't fire β every caller+ // spawned its own daemon. One incident accumulated 39 zombie daemons+ // holding ~8.5 GiB resident, which together with macOS compressor+ // pressure (27 GiB compressed) caused the configd watchdog timeout+ // and the 2026-06-15 21:06 kernel panic.+ //+ // Fix: drop the shell `&`. `daemon start` (default non-foreground+ // mode) already forks its own detached background process via+ // startBackgroundDaemon() AND writes the PID file BEFORE returning,+ // so execSync without `&` waits for the dedup-relevant PID-file+ // write but does NOT wait for the daemon itself to exit. Timeout+ // bumped to 30s for npx cold-cache scenarios.if (startDaemon) {try {output.writeln(output.dim(' Starting daemon...'));- execSync('npx @claude-flow/cli@latest daemon start 2>/dev/null &', {+ execSync('npx @claude-flow/cli@latest daemon start 2>/dev/null', {stdio: 'pipe',cwd: ctx.cwd,- timeout: 10000+ timeout: 30000});output.writeln(output.success(' β Daemon started'));} catch {+ // Daemon dedup hit (already running) OR spawn timed out.+ // Either way the worst case is a single retry on next init,+ // not a forked race producing N zombies.output.writeln(output.warning(' Daemon may already be running'));}}