Memory: fix async sync close race

This commit is contained in:
Vignesh Natarajan
2026-02-20 19:55:03 -08:00
parent 2649e9e044
commit be756b9a89
3 changed files with 69 additions and 17 deletions

View File

@@ -379,6 +379,9 @@ export class MemoryIndexManager extends MemoryManagerEmbeddingOps implements Mem
force?: boolean;
progress?: (update: MemorySyncProgressUpdate) => void;
}): Promise<void> {
if (this.closed) {
return;
}
if (this.syncing) {
return this.syncing;
}
@@ -602,6 +605,7 @@ export class MemoryIndexManager extends MemoryManagerEmbeddingOps implements Mem
return;
}
this.closed = true;
const pendingSync = this.syncing;
if (this.watchTimer) {
clearTimeout(this.watchTimer);
this.watchTimer = null;
@@ -622,6 +626,11 @@ export class MemoryIndexManager extends MemoryManagerEmbeddingOps implements Mem
this.sessionUnsubscribe();
this.sessionUnsubscribe = null;
}
if (pendingSync) {
try {
await pendingSync;
} catch {}
}
this.db.close();
INDEX_CACHE.delete(this.cacheKey);
}