Repository navigation
fix(core): don't duplicate MCP servers started during initial reconciliation - #58349
Merged
Merged
Conversation
…liation Initial reconciliation can yield before scheduling a server's startup, letting connect() or disconnect() for that server finish first. The queued startup then spawned a second connection, leaking the first, or started a server the user had just disconnected. It now skips once the entry has left its pending state. Fixes #52741
Contributor
There was a problem hiding this comment.
The fix looks correct. I ran the new tests against the base: both fail there (the server initializes twice after connect(), and starts once after disconnect()). Both pass with this change, and the rest of mcp.test.ts and typecheck:tsc show no regressions. Once connect() or disconnect() has finished, the server's status is never pending again. So the guard reliably skips a queued startup, and opening the latch keeps anything waiting on the server from hanging.
Public API: unchanged.
The fix is 8 lines and the test is 47. The test also depends on a bus listener pausing reconciliation at an internal point, so it can be dropped to keep the change minimal.
AbizardShafwan
pushed a commit
to AbizardShafwan/opencode
that referenced
this pull request
Nov 9, 2026
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.
Fixes #52741
On startup, MCP queues a launch for each enabled server. If
connect()ordisconnect()for that server finishes before the queued launch runs, the launch still goes ahead. That either starts a second copy (the first one's scope gets dropped while still open, so its process leaks) or brings back a server that was just disconnected.queued startup, under the server lock + if connect/disconnect already handled this server: skip startServer()A skipped launch still opens the startup latch, so calls waiting on the server don't hang. I added a test that runs both the connect and disconnect orderings; both fail without the fix.