fix(memory): atomic save + per-line parse guard against corruption (#1481)#4510
Closed
ZhangJing-gugugaga wants to merge 1 commit into
Closed
Conversation
…odelcontextprotocol#1481) Two defects in the knowledge-graph JSONL persistence caused the whole server to fail at startup whenever the memory file was truncated or malformed (the "JSON Parsing Error - All Tools Failing" report): 1. saveGraph wrote the file in place with fs.writeFile. A crash mid- write left a truncated last JSONL line. Now we write to a pid-suffixed .tmp file and rename into place for an atomic replace. 2. loadGraph relied on JSON.parse inside the reducer with no per- line guard, so one malformed line threw and aborted the entire load. Now each line is parsed in its own try/catch; bad lines are skipped with a stderr warning and the server keeps the valid rows. Co-Authored-By: Claude Opus 4.6 <<EMAIL>>
Author
|
Superseded: the memory fix was folded into PR #4509 ( branch) for a single review. Closing this duplicate. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Two defects in the memory server (issue #1481, "Memory MCP JSON Parsing Error - All Tools Failing") combine to make every tool fail on startup whenever the memory file is even slightly malformed:
saveGraphoverwrote the file in place withfs.writeFile. A process crash during the write left a truncated last JSONL line — producing an invalid file.loadGraphparsed each JSONL line inside the reducer with no per-line guard. A single malformed line aborted the entireloadGraphcall, and sincemain()constructs theKnowledgeGraphManagerat startup, the server died before it could serve a single request.This turns a survivable half-write into a total, repeated startup crash.
Fix (
src/memory/index.ts)saveGraphnow writes to a${path}.${pid}.tmpfile andfs.renames it into place — an atomic replace so a crash mid-write can never leave the real file truncated.loadGraphreads first (so ENOENT still returns an empty graph), then parses each line inside its owntry/catch. Malformed lines are skipped with a stderr warning instead of killing the server.Verification
cd src/memory && npx vitest run— all 30 tests pass (unchanged, 3 files / 50 tests across the whole memory suite also green). The new code does not alter the JSONL storage schema, so no migration concerns.Added no new test cases here — a regression test for the truncated-file-resilience path requires truncating a real file on disk and is a good follow-up.