Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 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

fix(daemon): keep one timer per worker type (#4412)

ordinary beinge0bc9b3efcf4
diff --git a/v3/@claude-flow/cli/__tests__/worker-daemon-duplicate-timers-4410.test.ts b/v3/@claude-flow/cli/__tests__/worker-daemon-duplicate-timers-4410.test.ts
new file mode 100644
index 0000000000..19d2e2c34e
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/worker-daemon-duplicate-timers-4410.test.ts
@@ -0,0 +1,49 @@
+/**
+ * #4410 automation scan: `setWorkerEnabled(type, true)` scheduled another
+ * timeout on every call without clearing the previous one. `timers` kept only
+ * the newest handle, so the older timer leaked, survived `stop()`, and could
+ * run the worker again untracked.
+ */
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { mkdtempSync, mkdirSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { WorkerDaemon } from '../src/services/worker-daemon.js';
+
+let dir: string;
+let daemon: WorkerDaemon;
+
+beforeEach(() => {
+ dir = mkdtempSync(join(tmpdir(), 'worker-daemon-timers-4410-'));
+ mkdirSync(join(dir, '.claude-flow', 'logs'), { recursive: true });
+ vi.useFakeTimers();
+ daemon = new WorkerDaemon(dir, { aiWorkersEnabled: false });
+ (daemon as any).running = true;
+});
+
+afterEach(() => {
+ (daemon as any).running = false;
+ vi.clearAllTimers();
+ vi.useRealTimers();
+ rmSync(dir, { recursive: true, force: true });
+ process.removeAllListeners('SIGTERM');
+ process.removeAllListeners('SIGINT');
+ process.removeAllListeners('SIGHUP');
+});
+
+describe('WorkerDaemon duplicate worker timers (#4410)', () => {
+ it('keeps one pending timer when enable is called repeatedly', () => {
+ daemon.setWorkerEnabled('map', true);
+ daemon.setWorkerEnabled('map', true);
+ daemon.setWorkerEnabled('map', true);
+
+ expect(vi.getTimerCount()).toBe(1);
+ });
+
+ it('drops a disabled worker from the deferred queue', () => {
+ (daemon as any).pendingWorkers.push('map');
+ daemon.setWorkerEnabled('map', false);
+
+ expect((daemon as any).pendingWorkers).not.toContain('map');
+ });
+});
diff --git a/v3/@claude-flow/cli/src/services/worker-daemon.ts b/v3/@claude-flow/cli/src/services/worker-daemon.ts
index ce7c7bc016..456b825f2c 100644
--- a/v3/@claude-flow/cli/src/services/worker-daemon.ts
+++ b/v3/@claude-flow/cli/src/services/worker-daemon.ts
@@ -1325,6 +1325,15 @@ export class WorkerDaemon extends EventEmitter {
const internalConfig = workerConfig as WorkerConfigInternal;
const staggerOffset = internalConfig.offsetMs || 0;
+ // #4410: only one timer may own a worker type. A redundant enable used to
+ // schedule a second timeout while this.timers kept only the newer handle,
+ // so the original leaked and could fire again after stop().
+ const previousTimer = this.timers.get(workerConfig.type);
+ if (previousTimer) {
+ clearTimeout(previousTimer);
+ this.timers.delete(workerConfig.type);
+ }
+
// Calculate initial delay with stagger offset
let initialDelay = staggerOffset;
if (state.lastRun) {
@@ -1340,8 +1349,9 @@ export class WorkerDaemon extends EventEmitter {
// Use concurrency-controlled execution (P0 fix)
await this.executeWorkerWithConcurrencyControl(workerConfig);
- // Reschedule
- if (this.running) {
+ // Reschedule only while the daemon is running and this worker is still
+ // enabled. Disabling during an in-flight run must not resurrect it.
+ if (this.running && workerConfig.enabled) {
const timer = setTimeout(runAndReschedule, workerConfig.intervalMs);
this.timers.set(workerConfig.type, timer);
state.nextRun = new Date(Date.now() + workerConfig.intervalMs);
@@ -2149,13 +2159,18 @@ export class WorkerDaemon extends EventEmitter {
workerConfig.enabled = enabled;
if (enabled && this.running) {
- this.scheduleWorker(workerConfig);
+ // A live timer or an in-flight run will schedule the next iteration
+ // itself; scheduling here would create a second chain (#4410).
+ if (!this.timers.has(type) && !this.runningWorkers.has(type)) {
+ this.scheduleWorker(workerConfig);
+ }
} else if (!enabled) {
const timer = this.timers.get(type);
if (timer) {
clearTimeout(timer);
this.timers.delete(type);
}
+ this.pendingWorkers = this.pendingWorkers.filter(workerType => workerType !== type);
}
this.saveState();