Skip to content

Make memory graph writes atomic - #4617

Closed
joenermunch wants to merge 1 commit into
modelcontextprotocol:mainfrom
joenermunch:fix/atomic-memory-writes
Closed

Make memory graph writes atomic#4617
joenermunch wants to merge 1 commit into
modelcontextprotocol:mainfrom
joenermunch:fix/atomic-memory-writes

Conversation

@joenermunch

Copy link
Copy Markdown

Description

Memory graph updates are now written to a unique temporary file in the same directory. The completed file is renamed over the target only after the write succeeds. Failed writes remove the temporary file and leave the previous graph intact.

Fixes #4614.

Server Details

  • Server: memory
  • Changes to: persistence

Motivation and Context

Direct writes truncate the target before the replacement content is complete. An interrupted write can therefore corrupt the only persisted copy of the graph.

How Has This Been Tested?

  • Added a regression test that simulates a partial write failure and verifies the original graph remains unchanged.
  • Verified temporary files are removed after failure.
  • npm test --workspace @modelcontextprotocol/server-memory -- --maxWorkers=4 (51 tests)
  • npm run build --workspace @modelcontextprotocol/server-memory

Breaking Changes

None.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling

Additional context

The temporary file is created beside the memory file so the rename stays on the same filesystem. Its randomized name also avoids collisions between concurrent processes.

@joenermunch joenermunch closed this Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

saveGraph() in src/memory uses non-atomic fs.writeFile — risk of corrupted memory file on interruption

1 participant