P2-T3: Fix double-spawn race condition when MCP client toggles rapidly#126
Merged
SoundBlaster merged 8 commits intomainfrom Mar 1, 2026
Merged
P2-T3: Fix double-spawn race condition when MCP client toggles rapidly#126SoundBlaster merged 8 commits intomainfrom
SoundBlaster merged 8 commits intomainfrom
Conversation
…oggles rapidly (PASS)
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
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.
Description
Fixes the double-spawn race condition that occurs when an MCP client (e.g. Zed) toggles its connection off/on rapidly. Two proxy processes could simultaneously detect no broker running and each spawn a daemon — two competing daemons then race for the Unix socket, one crashes, and the losing proxy's client shows 0 tools.
Fix: wrap
_spawn_broker_if_neededin afcntl.flock(LOCK_EX)critical section on abroker.lockfile co-located withbroker.pid. The second proxy waiter blocks onflock, then re-checks liveness under the lock and short-circuits to the connect path if the first spawner succeeded. The lock is automatically released on process exit (including crash) by the OS.Changes:
src/mcpbridge_wrapper/broker/proxy.py— addedimport fcntl;_spawn_broker_if_needednow acquiresLOCK_EXviarun_in_executorbefore the spawn decisiontests/unit/test_broker_proxy.py— addedTestBrokerProxySpawnLock(4 tests): lock file location, LOCK_EX acquisition, second-proxy skip, lock release on timeoutType of Change
Quality Gates
make test- All tests pass with ≥90% coverage (682 passed, 91.43% coverage)make lint- No linting errors (ruff check src/passed)make format- Code is properly formattedmake typecheck- Type checking passesmake doccheck- Documentation is synced with DocC (if docs changed)Documentation Sync
Testing
TestBrokerProxySpawnLock)Checklist