Skip to content

Commit 70d502e

Browse files
committed
Fix duplicate mirror-watcher messages and shutdown crash
- mirror-watcher: use watcher.once('ready') instead of .on() to prevent chokidar from firing the handler multiple times on macOS with non-existent watch paths (FSEvents quirk) - embedder: add disposeModels() that calls pipe.dispose() on each ONNX pipeline before process exit - project-manager: call disposeModels() in shutdown() to explicitly free ONNX sessions; prevents 'mutex lock failed: Invalid argument' crash on macOS during process teardown
1 parent b0e314a commit 70d502e

3 files changed

Lines changed: 12 additions & 2 deletions

File tree

src/lib/embedder.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ export function resetEmbedder(): void {
9696
_pipeCache.clear();
9797
}
9898

99+
/** Dispose all loaded ONNX pipelines. Call before process.exit() to avoid macOS mutex crash. */
100+
export async function disposeModels(): Promise<void> {
101+
for (const pipe of _pipeCache.values()) {
102+
try { await pipe.dispose(); } catch { /* ignore */ }
103+
}
104+
_models.clear();
105+
_pipeCache.clear();
106+
}
107+
99108
// Vectors are L2-normalized → dot product = cosine similarity
100109
export function cosineSimilarity(a: number[], b: number[]): number {
101110
if (a.length !== b.length) return 0;

src/lib/mirror-watcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ export function startMirrorWatcher(config: MirrorWatcherConfig): WatcherHandle {
498498
watcher.on('add', handleAddOrChange);
499499
watcher.on('change', handleAddOrChange);
500500
watcher.on('unlink', handleUnlink);
501-
watcher.on('ready', () => {
501+
watcher.once('ready', () => {
502502
const watched = ['.notes/', '.tasks/'];
503503
if (config.skillManager) watched.push('.skills/');
504504
process.stderr.write(`[mirror-watcher] Watching ${watched.join(', ')} in ${config.projectDir}\n`);

src/lib/project-manager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EventEmitter } from 'events';
2-
import { loadModel, embed, embedQuery } from '@/lib/embedder';
2+
import { loadModel, embed, embedQuery, disposeModels } from '@/lib/embedder';
33
import { loadGraph, saveGraph, type DocGraph, DocGraphManager } from '@/graphs/docs';
44
import { loadCodeGraph, saveCodeGraph, type CodeGraph, CodeGraphManager } from '@/graphs/code';
55
import { loadKnowledgeGraph, saveKnowledgeGraph, KnowledgeGraphManager } from '@/graphs/knowledge';
@@ -463,6 +463,7 @@ export class ProjectManager extends EventEmitter {
463463
}
464464
this.projects.clear();
465465
this.workspaces.clear();
466+
await disposeModels();
466467
process.stderr.write('[project-manager] Shutdown complete\n');
467468
}
468469

0 commit comments

Comments
 (0)