Fix SearchIndexService OOM — rebuild guard + memory cap#
Problem#
SearchIndexService.buildIndex() had no concurrency guard. Rapid file changes triggered overlapping full rebuilds, each allocating a ~24K-document index Map, causing OOM crashes.
Task / Link#
GitHub Issue #14 — AgentSDE/meridian-backend
Changes#
- Add
AbortController-based rebuild guard: concurrenthandleFileChange()calls abort the in-flight rebuild and setrebuildPendinginstead of spawning a second full build - After a rebuild completes or is aborted, check
rebuildPendingand trigger one follow-up rebuild if needed - Skip
handleFileChangeDetail()incremental updates whenrebuildPendingis true (upcoming full rebuild covers them) - Add
node_args: '--max-old-space-size=512'andmax_memory_restart: '512M'toecosystem.config.js - Add
--max-old-space-size=512toexec nodeline inservices/start-backend.sh - Add unit tests: concurrent guard, abort-preserves-index, follow-up rebuild, incremental-skip-when-pending
Notes#
- The 500ms debounce on
FILE_CHANGE_EVENTinFileWatcherServiceis unchanged and still applies - Single rebuild peak memory is well under 512MB — OOM was caused by concurrent allocations, not a single run
signal.abortedis checked per-task (not per-file) to avoid per-file overhead with small markdown files
Testing#
- All 655 unit tests pass (
npm run test) - Lint clean — zero errors (
npm run lint)