Skip to content

[BUG] save_basic_memory_config() performs unlocked read-modify-write on config.json across concurrent MCP processes #2181

Backlog

Description

@Flynn-Kawolsky

Summary

save_basic_memory_config() (src/basic_memory/config.py:348-383) reads config.json in _file_config_dict() (:326-328), merges fields in memory, writes a temp file, and calls os.replace(temp_path, file_path) — without holding an inter-process lock.

filelock>=3.12 is already a direct dependency of basic-memory (pyproject.toml:56, used to serialize concurrent bm hook flush runs).

Impact

When multiple MCP clients run concurrently on the same machine (for example, Claude Desktop + Codex CLI + another MCP host, or a retry spawn during a slow startup), every process periodically stamps auto_update_last_checked_at via _save_last_checked_timestamp() (src/basic_memory/cli/auto_update.py:294-298) and CLI commands run bm config set / bm project add:

  1. Lost updates: Process A reads config.json, Process B writes a setting change, Process A overwrites config.json with stale fields plus its own auto_update_last_checked_at.
  2. Windows PermissionError on os.replace: On Windows, concurrent os.replace() or open file handles from another process / AV scanner fail with PermissionError, which save_basic_memory_config() catches and logs as an error.

Proposed Fix

Wrap the read-modify-replace critical section in save_basic_memory_config() with filelock.FileLock(file_path.with_suffix(".json.lock"), timeout=...) (matching the existing filelock usage in the codebase), and optionally perform the load -> mutate -> save inside _save_last_checked_timestamp() under that lock so concurrent timestamp stamps never clobber user edits.

Happy to put together a small PR with a concurrency test on Windows if this approach makes sense.

Activity

  1. added
    bug
    on Nov 30, 2026
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

    bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions