mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-20 04:59:34 +01:00
Merge #818: a chokidar watcher whose fs.watch fails is closed instead of retrying forever
A persistent:false watcher closes on its first error (closeWatcherOnError), so a failing fs.watch costs one warning instead of an endless climb to /, and TeamWatcher re-creates a stopped watcher from its 30 s poll.
This commit is contained in:
commit
756d8d875e
5 files changed
+743
-12
No files matched your search
+32
-8
@@ -15,7 +15,8 @@ import { join } from 'node:path';
|
||||
|
||||
import { watch as chokidarWatch, type FSWatcher as ChokidarWatcher } from 'chokidar';
|
||||
|
||||
import type { TeamConfig, TeamTask, InboxMessage } from './types.js';
|
||||
import { getErrorMessage, type TeamConfig, type TeamTask, type InboxMessage } from './types.js';
|
||||
import { closeWatcherOnError } from './utils/close-watcher-on-error.js';
|
||||
import { LRUMap } from './utils/lru-map.js';
|
||||
import { TEAM_POLL_INTERVAL_MS, MAX_CACHED_TEAMS, MAX_CACHED_TASKS } from './config/team-config.js';
|
||||
|
||||
@@ -36,6 +37,8 @@ export class TeamWatcher extends EventEmitter {
|
||||
private sessionToTeam: Map<string, string> = new Map();
|
||||
private teamsWatcher: ChokidarWatcher | null = null;
|
||||
private tasksWatcher: ChokidarWatcher | null = null;
|
||||
// Watchers stopped by an error and not yet back for a whole poll: one warning per outage.
|
||||
private watchersDown = new Set<'teams' | 'tasks'>();
|
||||
|
||||
constructor(teamsDir?: string, tasksDir?: string) {
|
||||
super();
|
||||
@@ -47,11 +50,28 @@ export class TeamWatcher extends EventEmitter {
|
||||
start(): void {
|
||||
if (this.pollTimer) return;
|
||||
this.poll();
|
||||
this.pollTimer = setInterval(() => this.poll(), TEAM_POLL_INTERVAL_MS);
|
||||
this.pollTimer = setInterval(() => {
|
||||
this.setupFsWatchers(); // re-creates a watcher an error stopped
|
||||
this.poll();
|
||||
}, TEAM_POLL_INTERVAL_MS);
|
||||
this.setupFsWatchers();
|
||||
}
|
||||
|
||||
private watcherStopped(name: 'teams' | 'tasks', err: unknown): void {
|
||||
if (this.watchersDown.has(name)) return;
|
||||
this.watchersDown.add(name);
|
||||
console.warn(`[TeamWatcher] ${name} watcher stopped, relying on polling: ${getErrorMessage(err)}`);
|
||||
}
|
||||
|
||||
/** Creates each watcher that is not running; one still running since the last call ends its outage. */
|
||||
private setupFsWatchers(): void {
|
||||
if (this.teamsWatcher) this.watchersDown.delete('teams');
|
||||
else this.setupTeamsWatcher();
|
||||
if (this.tasksWatcher) this.watchersDown.delete('tasks');
|
||||
else this.setupTasksWatcher();
|
||||
}
|
||||
|
||||
private setupTeamsWatcher(): void {
|
||||
try {
|
||||
this.teamsWatcher = chokidarWatch(this.teamsDir, {
|
||||
depth: 2,
|
||||
@@ -66,13 +86,16 @@ export class TeamWatcher extends EventEmitter {
|
||||
this.teamsWatcher.on('change', teamsHandler);
|
||||
this.teamsWatcher.on('unlink', teamsHandler);
|
||||
this.teamsWatcher.on('unlinkDir', teamsHandler);
|
||||
this.teamsWatcher.on('error', (err) => {
|
||||
console.warn('[TeamWatcher] chokidar teams watcher error:', err);
|
||||
closeWatcherOnError(this.teamsWatcher, (err) => {
|
||||
this.teamsWatcher = null;
|
||||
this.watcherStopped('teams', err);
|
||||
});
|
||||
} catch (err) {
|
||||
console.warn('[TeamWatcher] Failed to set up teams chokidar watcher, relying on polling:', err);
|
||||
this.watcherStopped('teams', err);
|
||||
}
|
||||
}
|
||||
|
||||
private setupTasksWatcher(): void {
|
||||
try {
|
||||
this.tasksWatcher = chokidarWatch(this.tasksDir, {
|
||||
depth: 1,
|
||||
@@ -84,11 +107,12 @@ export class TeamWatcher extends EventEmitter {
|
||||
|
||||
this.tasksWatcher.on('add', () => this.pollTasks().catch(() => {})); // Ignore - poll errors are non-fatal, next poll will retry
|
||||
this.tasksWatcher.on('change', () => this.pollTasks().catch(() => {})); // Ignore - poll errors are non-fatal, next poll will retry
|
||||
this.tasksWatcher.on('error', (err) => {
|
||||
console.warn('[TeamWatcher] chokidar tasks watcher error:', err);
|
||||
closeWatcherOnError(this.tasksWatcher, (err) => {
|
||||
this.tasksWatcher = null;
|
||||
this.watcherStopped('tasks', err);
|
||||
});
|
||||
} catch (err) {
|
||||
console.warn('[TeamWatcher] Failed to set up tasks chokidar watcher, relying on polling:', err);
|
||||
this.watcherStopped('tasks', err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* @fileoverview Close a chokidar watcher on its first error, safely.
|
||||
*
|
||||
* chokidar 3 with `persistent: false` has no guard for an `fs.watch` that
|
||||
* throws (EMFILE once the inotify instance limit is used up, ENOSPC once the
|
||||
* watch limit is): it emits the error, then a TypeError from closing the
|
||||
* watcher it never got, then retries on the parent directory, one level per
|
||||
* pass, and re-adds `/` forever, since the parent of `/` is `/`. Only a closed
|
||||
* watcher ends that loop, so a watcher whose `'error'` handler just logs logs
|
||||
* forever.
|
||||
*
|
||||
* Closing from the handler is not enough on its own:
|
||||
* - `close()` removes every listener, and the TypeError of the same pass is
|
||||
* emitted after it. With no `'error'` listener EventEmitter throws it, and it
|
||||
* reaches the process as an unhandled rejection. So a no-op listener stays
|
||||
* on the closed watcher.
|
||||
* - callbacks chokidar already has in flight can still call `add()` (a deleted
|
||||
* file's `_remove()` re-adds the file when chokidar thinks it was the only
|
||||
* watched path), and `add()` sets `closed = false`, which starts the climb
|
||||
* again, silently now. So `add()` becomes a no-op on the closed watcher.
|
||||
*
|
||||
* @module utils/close-watcher-on-error
|
||||
*/
|
||||
|
||||
import type { FSWatcher } from 'chokidar';
|
||||
|
||||
/**
|
||||
* Close `watcher` on its first `'error'` and pass that error to `onClosed`,
|
||||
* once. The caller's polling has to cover what the watcher watched and create it
|
||||
* again later: chokidar 3 reports a one-off ENOENT race through the same error.
|
||||
*/
|
||||
export function closeWatcherOnError(watcher: FSWatcher, onClosed: (err: unknown) => void): void {
|
||||
watcher.once('error', (err: unknown) => {
|
||||
watcher.close().catch(() => {}); // best-effort, like the stop() paths
|
||||
watcher.on('error', () => {}); // see the fileoverview
|
||||
watcher.add = () => watcher; // likewise
|
||||
onClosed(err);
|
||||
});
|
||||
}
|
||||
@@ -40,6 +40,7 @@ import { join } from 'node:path';
|
||||
import { watch as chokidarWatch, type FSWatcher as ChokidarWatcher } from 'chokidar';
|
||||
|
||||
import type { WorkflowRunInfo, WorkflowRunSummary, WorkflowAgentInfo, WorkflowRunPhase } from './types/workflow-run.js';
|
||||
import { closeWatcherOnError } from './utils/close-watcher-on-error.js';
|
||||
import { LRUMap } from './utils/lru-map.js';
|
||||
import {
|
||||
WORKFLOW_RUN_POLL_INTERVAL_MS,
|
||||
@@ -865,10 +866,8 @@ export class WorkflowRunWatcher extends EventEmitter {
|
||||
// subagents/workflows/ children are wf_<id>/ DIRS — catch their add/remove too.
|
||||
watcher.on('addDir', handler);
|
||||
watcher.on('unlinkDir', handler);
|
||||
watcher.on('error', () => {
|
||||
// chokidar surfaced an error for this dir — drop the watcher; poll still covers it.
|
||||
this.removeDirWatcher(workflowsDir);
|
||||
});
|
||||
// chokidar surfaced an error for this dir — drop the watcher; poll still covers it.
|
||||
closeWatcherOnError(watcher, () => this.dirWatchers.delete(workflowsDir));
|
||||
this.dirWatchers.set(workflowsDir, watcher);
|
||||
} catch {
|
||||
// Watch setup failed — periodic poll still discovers changes.
|
||||
|
||||
Reference in new issue
Block a user