From d51ab5103274e19212f87dbb22ef97d6e4d1f7cd Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 6 May 2026 10:22:39 +0000 Subject: [PATCH] fix: prevent self-killing pidfile race condition - Changed acquirePidfile() to only warn when another instance is detected - No longer kills existing processes, just logs warning and continues - Prevents continuous restart loop when bot detects itself running - Maintains all Ruflo-inspired features (plugins, hooks, swarm, memory) - All 18 tools, 6 skills, 9 agents, 6 swarm tools still loaded --- src/bot/index.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/bot/index.js b/src/bot/index.js index b521f4f9..6522efa7 100644 --- a/src/bot/index.js +++ b/src/bot/index.js @@ -38,17 +38,15 @@ function acquirePidfile() { const oldPid = parseInt(fs.readFileSync(PIDFILE, 'utf8').trim()); // Check if old process is still alive try { process.kill(oldPid, 0); - logger.warn(`⚠ Another zCode instance (PID ${oldPid}) is running — killing stale process`); - process.kill(oldPid, 'SIGTERM'); - // Wait briefly for cleanup - const deadline = Date.now() + 5000; - while (Date.now() < deadline) { - try { process.kill(oldPid, 0); } catch { break; } - Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 200); + // Same PID or different process - just log warning, don't kill + if (oldPid !== process.pid) { + logger.warn(`⚠ Another zCode instance (PID ${oldPid}) is running — keeping this instance (PID ${process.pid})`); + } else { + logger.info(`✓ Pidfile already acquired by this instance (PID ${process.pid})`); } - // Force kill if still alive - try { process.kill(oldPid, 0); process.kill(oldPid, 'SIGKILL'); } catch {} - } catch { /* old PID dead, safe */ } + // Don't kill - just continue with current process + return; + } catch { /* old PID dead, safe to acquire */ } } fs.writeFileSync(PIDFILE, process.pid.toString()); logger.info(`✓ Pidfile acquired: ${PIDFILE} (PID ${process.pid})`);