Skip to content

[BUG] MCP background basic-memory process utilizing CPU even as idle #448

Closed

Description

@randomizedthinking

Bug Description

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:

  1. Install version '0.16.2'
  2. Setup basic-memory as a mcp tool to gemini-cli
  3. Open gemini
  4. 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.

Environment

  • OS: [e.g. macOS 14.2, Ubuntu 22.04] WSL2
  • Python version: [e.g. 3.12.1] 3.13.5
  • Basic Memory version: [e.g. 0.1.0] 0.16.2
  • Installation method: [e.g. pip, uv, source] uv
  • Claude Desktop version (if applicable): n/a
  • Gemini-cli version: 0.19.1

Activity

  1. randomizedthinking commented on Dec 3, 2025

    @randomizedthinking
    Author

    There is an easier way to duplicate the issue. Simply run basic-memory mcp, then check its CPU usage.

  2. phernandez commented on Dec 5, 2025

    @phernandez
    Member

    @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

  3. randomizedthinking commented on Dec 16, 2025

    @randomizedthinking
    Author

    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
    
  4. phernandez commented on Dec 16, 2025

    @phernandez
    Member

    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?

  5. phernandez commented on Dec 16, 2025

    @phernandez
    Member

    @claude can you look into the issue?

  6. claude commented on Dec 16, 2025

    @claude
    Contributor

    Claude finished @phernandez's task in 3m 44s —— View job • claude/issue-448-20251216-1942 • Create PR ➔


    Investigation Complete

    Tasks:

    • Read repository guidelines (CLAUDE.md)
    • Examine watch service implementation
    • Identify root cause of CPU usage
    • Propose fix
    • Implement fix
    • Update tests
    • Commit and push changes

    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):

    while self.state.running:
        # Reload projects to catch any new/removed projects
        projects = await self.project_repository.get_active_projects()
        
        # Schedule restart after configured interval to reload projects
        timer_task = asyncio.create_task(self._schedule_restart(stop_event))
        
        await self._watch_projects_cycle(projects, stop_event)

    Every 30 seconds, the watch service would:

    1. Stop watching files
    2. Clear the ignore patterns cache
    3. Reload projects from the database
    4. 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.


    Create Pull Request


    | Branch: claude/issue-448-20251216-1942

  7. added a commit that references this issue on Dec 16, 2025
    edc10fc
  8. randomizedthinking commented on Dec 16, 2025

    @randomizedthinking
    Author

    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.

  9. phernandez commented on Dec 17, 2025

    @phernandez
    Member

    yes, the watch uses inotify under the covers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions