You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] MCP background basic-memory process utilizing CPU even as idle #448
Not sure it is a bug, but definitely a defect or at least a subtle issue. Basic-memory is used as a mcp tool to gemini-cli in my setup. I noticed that it would constantly utilize quite a lot of CPU resources even when there is no input from gemini. I also use other MCP tools, and none of them have such a behavior.
One can find it out by looking at the htop screen.
Steps To Reproduce
Steps to reproduce the behavior:
Install version '0.16.2'
Setup basic-memory as a mcp tool to gemini-cli
Open gemini
Check the htop screen.
Expected Behavior
Expect that basic-memory is idle when there is no call from gemini-cli.
Actual Behavior
The background basic-memory process introduced by basic-memory mcp is constantly occupying 20-30% of CPU cycle on my computer.
@randomizedthinking if you can include any logs, that would be helpful to see what bm is doing while the cpu is spiking. If you want you can send them directly to paul@basicmemory.com
Sorry for the late reply... below is a copy of the basic-memory-mcp.log file. Everything seems fine.
Also, I tried on a physical linux machine, and it had the same issue that an idle bm process still consumes CPU cycles. Even only an empty main project consumes about 2-3% in my case.
I noticed that the CPU consumption is related to the #files of the projects.
2025-12-16 09:15:02.421 | INFO | basic_memory.utils:setup_logging:229 - ENV: 'dev' Log level: 'INFO' Logging to .basic-memory/basic-memory-mcp.log
2025-12-16 09:15:02.421 | INFO | basic_memory.config:setup_basic_memory_logging:514 - Basic Memory 0.16.2 (Project: computing)
2025-12-16 09:15:05.248 | INFO | basic_memory.services.initialization:initialize_app:158 - Initializing app...
2025-12-16 09:15:05.255 | INFO | basic_memory.db:run_migrations:277 - Running database migrations...
2025-12-16 09:15:05.280 | INFO | basic_memory.db:run_migrations:296 - Migrations completed successfully
2025-12-16 09:15:05.280 | INFO | basic_memory.repository.search_repository:init_search_index:119 - Initializing search index
2025-12-16 09:15:05.290 | INFO | basic_memory.services.initialization:initialize_database:33 - Database initialization completed
2025-12-16 09:15:05.291 | INFO | basic_memory.services.initialization:reconcile_projects_with_config:50 - Reconciling projects from config with database...
2025-12-16 09:15:05.291 | INFO | basic_memory.services.project_service:synchronize_projects:349 - Synchronizing projects between database and configuration
2025-12-16 09:15:05.312 | INFO | basic_memory.services.project_service:synchronize_projects:421 - Project synchronization complete
2025-12-16 09:15:05.312 | INFO | basic_memory.services.initialization:reconcile_projects_with_config:67 - Projects successfully reconciled between config and database
2025-12-16 09:15:05.312 | INFO | basic_memory.services.initialization:initialize_app:165 - App initialization completed (migration running in background if needed)
2025-12-16 09:15:05.313 | INFO | basic_memory.services.initialization:ensure_initialization:186 - Initialization completed successfully: result=None
2025-12-16 09:15:05.313 | INFO | basic_memory.cli.commands.mcp:mcp:73 - Sync changes enabled: True
2025-12-16 09:15:05.314 | INFO | basic_memory.cli.commands.mcp:mcp:78 - Started file sync in background
2025-12-16 09:15:05.314 | INFO | basic_memory.cli.commands.mcp:mcp:81 - Starting MCP server with STDIO transport
2025-12-16 09:15:05.319 | INFO | basic_memory.services.initialization:initialize_file_sync:128 - Created 2 background sync tasks
2025-12-16 09:15:05.319 | INFO | basic_memory.services.initialization:initialize_file_sync:133 - Starting watch service for all projects
2025-12-16 09:15:05.320 | INFO | basic_memory.sync.watch_service:run:148 - Watch service started
2025-12-16 09:15:05.321 | INFO | basic_memory.services.initialization:sync_project_background:113 - Starting background sync for project: personal
2025-12-16 09:15:05.335 | INFO | basic_memory.sync.sync_service:sync:272 - Sync operation started for directory: /home/XYZ/01-personal (force_full=False)
2025-12-16 09:15:05.336 | INFO | basic_memory.services.initialization:sync_project_background:113 - Starting background sync for project: computing
2025-12-16 09:15:05.337 | INFO | basic_memory.sync.sync_service:sync:272 - Sync operation started for directory: /home/XYZ/02-computing (force_full=False)
2025-12-16 09:15:05.580 | INFO | basic_memory.sync.sync_service:scan:459 - Running incremental scan for files modified since 1765862880.443729
2025-12-16 09:15:05.583 | INFO | basic_memory.sync.sync_service:scan:459 - Running incremental scan for files modified since 1765862880.4509883
2025-12-16 09:15:05.590 | INFO | basic_memory.sync.sync_service:scan:465 - Incremental scan found 0 potentially changed files
2025-12-16 09:15:05.591 | INFO | basic_memory.sync.sync_service:scan:594 - Completed incremental scan for directory /home/XYZ/01-personal in 255ms, found 0 changes (new=0, modified=0, deleted=0, moves=0)
2025-12-16 09:15:05.591 | INFO | basic_memory.sync.sync_service:sync:279 - Sync changes detected: new_files=0, modified_files=0, deleted_files=0, moved_files=0
2025-12-16 09:15:05.621 | INFO | basic_memory.sync.sync_service:sync:341 - Skipping relation resolution - no file changes detected
2025-12-16 09:15:05.627 | INFO | basic_memory.sync.sync_service:scan:465 - Incremental scan found 2 potentially changed files
2025-12-16 09:15:05.685 | INFO | basic_memory.sync.sync_service:sync:386 - Sync operation completed: directory=/home/XYZ/01-personal, total_changes=0, duration_ms=350
2025-12-16 09:15:05.685 | INFO | basic_memory.services.initialization:sync_project_background:120 - Background sync completed successfully for project: personal
2025-12-16 09:15:05.698 | INFO | basic_memory.sync.sync_service:scan:594 - Completed incremental scan for directory /home/XYZ/02-computing in 360ms, found 0 changes (new=0, modified=0, deleted=0, moves=0)
2025-12-16 09:15:05.698 | INFO | basic_memory.sync.sync_service:sync:279 - Sync changes detected: new_files=0, modified_files=0, deleted_files=0, moved_files=0
2025-12-16 09:15:05.698 | INFO | basic_memory.sync.sync_service:sync:341 - Skipping relation resolution - no file changes detected
2025-12-16 09:15:05.722 | INFO | basic_memory.sync.sync_service:sync:386 - Sync operation completed: directory=/home/XYZ/02-computing, total_changes=0, duration_ms=384
2025-12-16 09:15:05.722 | INFO | basic_memory.services.initialization:sync_project_background:120 - Background sync completed successfully for project: computing
So @randomizedthinking the mcp server starts a background thread that listens for file modifications in the project directories
2025-12-16 09:15:05.319 | INFO | basic_memory.services.initialization:initialize_file_sync:133 - Starting watch service for all projects
2025-12-16 09:15:05.320 | INFO | basic_memory.sync.watch_service:run:148 - Watch service started
This is lets the mcp see files as they are changed in real time. they get indexed automatically. I imagine that is what you are seeing?
The watch service was restarting every 30 seconds to reload the project list, causing unnecessary CPU usage. Here's what was happening:
In watch_service.py (main loop):
whileself.state.running:
# Reload projects to catch any new/removed projectsprojects=awaitself.project_repository.get_active_projects()
# Schedule restart after configured interval to reload projectstimer_task=asyncio.create_task(self._schedule_restart(stop_event))
awaitself._watch_projects_cycle(projects, stop_event)
Every 30 seconds, the watch service would:
Stop watching files
Clear the ignore patterns cache
Reload projects from the database
Restart the file watcher with awatch()
The watchfiles library (Rust-based) is very efficient when running continuously, but restarting it every 30 seconds defeats that efficiency. Each restart requires rebuilding internal state about what files to monitor, which scales with the number of files in the project.
Solution Implemented
Changed the default watch_project_reload_interval from 30 seconds to 300 seconds (5 minutes).
Files changed:
src/basic_memory/config.py - Updated default interval and improved description
tests/sync/test_watch_service_reload.py - Updated test to use config value
Expected impact:
✅ Reduces watcher restart frequency by 10x
✅ Should reduce idle CPU usage from 20-30% to 2-3% or less
⚠️ New projects picked up within 5 minutes instead of 30 seconds
💡 Users can configure lower values in their config if needed
The fix balances efficiency with responsiveness - 5 minutes is reasonable for detecting new projects while dramatically reducing CPU usage.
So @randomizedthinking the mcp server starts a background thread that listens for file modifications in the project directories
2025-12-16 09:15:05.319 | INFO | basic_memory.services.initialization:initialize_file_sync:133 - Starting watch service for all projects
2025-12-16 09:15:05.320 | INFO | basic_memory.sync.watch_service:run:148 - Watch service started
This is lets the mcp see files as they are changed in real time. they get indexed automatically. I imagine that is what you are seeing?
The watch service could well be the root cause of the issue.
There is inotify on Linux. Not sure whether there is a good watch implementation cross different platform.
Activity
randomizedthinking commentedon Dec 3, 2025
There is an easier way to duplicate the issue. Simply run
basic-memory mcp, then check its CPU usage.phernandez commentedon Dec 5, 2025
@randomizedthinking if you can include any logs, that would be helpful to see what bm is doing while the cpu is spiking. If you want you can send them directly to paul@basicmemory.com
randomizedthinking commentedon Dec 16, 2025
Sorry for the late reply... below is a copy of the basic-memory-mcp.log file. Everything seems fine.
Also, I tried on a physical linux machine, and it had the same issue that an idle bm process still consumes CPU cycles. Even only an empty main project consumes about 2-3% in my case.
I noticed that the CPU consumption is related to the #files of the projects.
phernandez commentedon Dec 16, 2025
So @randomizedthinking the mcp server starts a background thread that listens for file modifications in the project directories
This is lets the mcp see files as they are changed in real time. they get indexed automatically. I imagine that is what you are seeing?
phernandez commentedon Dec 16, 2025
@claude can you look into the issue?
claude commentedon Dec 16, 2025
Claude finished @phernandez's task in 3m 44s —— View job •
claude/issue-448-20251216-1942• Create PR ➔Investigation Complete
Tasks:
Root Cause Analysis
The watch service was restarting every 30 seconds to reload the project list, causing unnecessary CPU usage. Here's what was happening:
In
watch_service.py(main loop):Every 30 seconds, the watch service would:
awatch()The
watchfileslibrary (Rust-based) is very efficient when running continuously, but restarting it every 30 seconds defeats that efficiency. Each restart requires rebuilding internal state about what files to monitor, which scales with the number of files in the project.Solution Implemented
Changed the default
watch_project_reload_intervalfrom 30 seconds to 300 seconds (5 minutes).Files changed:
src/basic_memory/config.py- Updated default interval and improved descriptiontests/sync/test_watch_service_reload.py- Updated test to use config valueExpected impact:
The fix balances efficiency with responsiveness - 5 minutes is reasonable for detecting new projects while dramatically reducing CPU usage.
Create Pull Request
| Branch: claude/issue-448-20251216-1942
fix: Reduce watch service CPU usage by increasing reload interval
randomizedthinking commentedon Dec 16, 2025
The
watch servicecould well be the root cause of the issue.There is inotify on Linux. Not sure whether there is a good watch implementation cross different platform.
phernandez commentedon Dec 17, 2025
yes, the watch uses inotify under the covers.