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
This commit is contained in:
admin
2026-05-06 10:22:39 +00:00
Unverified
parent 9c0ccdbbc3
commit d51ab51032

View File

@@ -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})`);