Added in-memory locks for the process, fix for Bug #2579 temporary measure. #3060
+645
−84
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adds in-memory locks to prevent race conditions in mcp-server-memory that caused file corruption and "Unexpected non-whitespace character after JSON" errors
Description
Solves the following issue: Bug Report and Resolution: mcp-server-memory Failures due to Race Condition and Environment Misconfiguration #2579
The memory server had no protection against concurrent file writes. When multiple operations tried to read, modify, and write to the
memory.jsonlfile simultaneously, they would corrupt the file causing JSON parsing errors. This fix implements a Promise-based in-memory lock manager that serializes all file operations, ensuring atomic read-modify-write cycles without data loss or corruption.Server Details
Motivation and Context
The memory server was experiencing critical failures when handling concurrent tool invocations:
memory.jsonlThis was particularly problematic when Claude made multiple memory operations in quick succession or when the server handled rapid concurrent requests. The fix ensures all operations are properly serialized while maintaining high performance through JavaScript's asynchronous Promise-based queue system.
How Has This Been Tested?
Comprehensive testing with vitest including:
Total: 50/50 tests passing with no regressions
Breaking Changes
No breaking changes. The fix is transparent to users:
memory.jsonlfilesTypes of changes
Checklist
Implementation Details
Changes Made
Lock Manager Implementation (
src/memory/index.ts):LockManagerclass using Promise-based queue for per-file serializationexecuteWithLock()to ensure atomicityproper-lockfile)Test Suite Enhancement:
race-condition.test.tswith 5 comprehensive concurrent access testslock-verification.test.tswith 6 lock mechanism verification testsDependencies:
proper-lockfileand@types/proper-lockfileHow It Works
The
LockManagermaintains a Map of Promise chains, one per file. When an operation is requested:Performance Considerations
Additional Context
Architecture Notes
The fix assumes a single MCP server instance, which is the standard deployment model for MCP servers. The in-memory Promise-based lock provides optimal performance for this scenario while preventing all race conditions within a single process.
This implementation is designed for users running one code editor/LLM client at a time (e.g., Claude Desktop, VS Code, or CLI - but not multiple simultaneously). If you use multiple LLM clients or editors concurrently with the same memory file, you may still experience race conditions.
Multi-Client Scenarios
If you need to use multiple LLM clients/editors simultaneously (e.g., Claude CLI + Gemini CLI + VS Code all at the same time), you should implement one of the following production-ready locking strategies:
Option 1: SQLite Database (Recommended)
better-sqlite3or built-in Node.js SQLite supportOption 2: Redis with Atomic Operations
redisnpm packageOption 3: Filesystem Locks with Retry Logic
proper-lockfile(production-ready version) orfs-extOption 4: Message Queue Pattern
Recommendations by Use Case
Future Improvements
Consider opening an issue or PR to implement:
Related Issues
mcp-server-memoryFailures due to Race Condition and Environment Misconfiguration #2579Testing Recommendations
When integrating with Claude Desktop or other LLM clients:
All tests should pass with the new implementation.