Repository navigation
fix(api): bind the gateway's MCPServer to the serving event loop so index jobs run - #1747
Merged
Shashankss1205 merged 1 commit intoOct 15, 2026
Conversation
get_server() was a sync FastAPI dependency and ran in the threadpool, where MCPServer found no running loop and fell back to a fresh one nobody runs. Index jobs scheduled onto it with run_coroutine_threadsafe stayed pending forever whenever the first request to the gateway was a REST call. Make the dependency async and pass the running loop explicitly; await it in the two SSE handlers. Adds a regression test that fails on main. Closes CodeGraphContext#1744
|
Someone is attempting to deploy a commit to the shashankss1205's projects Team on Vercel. A member of the Team first needs to authorize it. |
4 tasks done
Shashankss1205
approved these changes
Oct 9, 2026
Shashankss1205
left a comment
Collaborator
There was a problem hiding this comment.
Serving-loop singleton creation fixes REST-first pending jobs. API tests pass, including combination with #1751. Preserve await calls when resolving their shared file conflict.
Integration detail: under MCP 2, handle_list_tools(ctx, params) and handle_call_tool(ctx, params) must both use server = await get_server(). The loop regression must call asyncio.run(mcp_sse.handle_list_tools(None, None)).tools.
This was referenced Oct 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.
Summary
get_server()inapi/router.pyis a sync FastAPI dependency, so it runs in the threadpool. ThereMCPServer.__init__finds no running loop and falls back to a freshasyncio.new_event_loop()that nothing ever runs. Every indexing job is scheduled onto it withrun_coroutine_threadsafeand stayspendingforever whenever the first request to the gateway is a REST call. Details and reproduction: #1744.Change
get_serverbecomesasync defand passesloop=asyncio.get_running_loop()toMCPServer, so the singleton is bound to the serving loop regardless of which request builds it. This also removes the first-request race where concurrent REST calls built several servers.api/mcp_sse.pyawait get_server().tests/unit/api/test_router_server_loop.py: asserts the server is bound to the serving loop through a REST request, and that SSElist_toolsstill gets a server. Fails on unpatchedmain, passes with this change.Trade-off
MCPServer.__init__(context resolution, DB driver creation) now runs once on the loop during the first request. The alternative is a lifespan hook, which would also give a place to callshutdown(), but then the gateway refuses to start while the DB is down. Happy to switch if you prefer that.Closes #1744.
Disclosure: the investigation, this description and the patch were prepared with AI assistance (Claude Code) and reviewed by a human before posting.