fix: prevent stale reads in MCP server with concurrent processes#66
Open
jeeshofone wants to merge 3 commits intosirsjg:mainfrom
Open
fix: prevent stale reads in MCP server with concurrent processes#66jeeshofone wants to merge 3 commits intosirsjg:mainfrom
jeeshofone wants to merge 3 commits intosirsjg:mainfrom
Conversation
Fixes two critical concurrency issues when multiple agents use MCP via docker exec: 1. Race condition on writes (lost updates) - Multiple processes overwriting each other's changes - Fixed with transaction-based merge in SQLite adapter - Test: 30 concurrent writes → 0 lost (was 29 lost, 97% loss rate) 2. Stale reads (agents not seeing each other's changes) - Each MCP process reads DB once at startup, never refreshes - Fixed by calling adapter.read() before each MCP tool call - Test: Agent 1 immediately sees Agent 2's new task Changes: - SQLite adapter: Add transaction-based write with mergeById helper - MCP server: Refresh data from disk before each operation - Tests: Comprehensive unit tests + reproduction scripts - Docker: Switch to SQLite by default in docker-compose.yml Fixes sirsjg#60
Keep additional test files and organization improvements
Upstream added blob storage which requires FLUX_DIR to locate the .flux directory. Without it, server tries to create /home/flux/.flux/blobs and crashes with permission denied. Changes: - Add FLUX_DIR=/app/packages/data to docker-compose.yml - Single volume mount (./data:/app/packages/data) is sufficient - Blobs directory auto-created by mkdirSync in createFilesystemBlobStorage Tested: - Clean install (no blobs dir) ✅ - Stale read fix still works ✅ - Write race condition fix still works ✅
10c6795 to
85bd6ea
Compare
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
Agents using MCP via
docker execcannot see each other's changes because each process reads the database once at startup and never refreshes.Example:
Solution
Add
adapter.read()before each MCP tool call to refresh data from disk.Testing
Automated Test
# Run cross-agent visibility test bun tests/concurrency/test-cross-agent-visibility.tsManual Test
Test Results
Changes
packages/mcp/src/index.ts: Addadapter.read()before each tool calltests/concurrency/: Add comprehensive test suitetest-stale-reads.ts: Demonstrates the issuetest-cross-agent-visibility.ts: Verifies the fixREADME.md: DocumentationBreaking Changes
None. Backward compatible.
Related
This PR completes the concurrency fix suite for multi-agent workflows.