diff --git a/graphify/serve.py b/graphify/serve.py index 5e3149dca4..2b31e578d3 100644 --- a/graphify/serve.py +++ b/graphify/serve.py @@ -2970,6 +2970,24 @@ def _select_graph(project_path) -> None: G, communities = _load_ctx(path) active_graph_path = str(Path(path).resolve()) + async def _warm_graph(project_path) -> None: + """Load (or reload) the target graph in a worker thread (#5371). + + ``_ctx_cache.load`` parses graph.json and builds its trigram index, + which takes seconds on a large graph. Run on the event loop, it stops + every other request, so the stale entry ``load`` hands concurrent + callers during a rebuild (#5177) was never reachable. After this + returns, ``_select_graph`` is a cache hit, or gets the current + entry while another call's rebuild is still in flight. + + Only the load leaves the loop. ``_select_graph`` and the handler still + run with no await between them, so the shared ``G`` cannot be rebound + by another call in the middle of a handler. + """ + import anyio + + await anyio.to_thread.run_sync(_load_ctx, _resolve_graph_path(project_path)) + # NOTE: no decorators here — the handlers below are plain coroutines, # bound to the Server at the END of this function in a version-aware way: # mcp 1.x exposes the @server.list_tools()/... decorator API, mcp 2.x @@ -3455,6 +3473,7 @@ async def list_resources() -> list[types.Resource]: ] async def read_resource(uri: AnyUrl) -> str: + await _warm_graph(None) _select_graph(None) # resources read the server's default graph uri_str = str(uri) if uri_str == "graphify://report": @@ -3512,6 +3531,7 @@ async def call_tool(name: str, arguments: dict) -> list[types.TextContent]: if not handler: return [types.TextContent(type="text", text=f"Unknown tool: {name}")] try: + await _warm_graph(project_path) _select_graph(project_path) # bind G/communities to the target graph return [types.TextContent(type="text", text=handler(arguments))] except ToolError: diff --git a/tests/test_serve_reload_off_loop.py b/tests/test_serve_reload_off_loop.py new file mode 100644 index 0000000000..d28f647c7d --- /dev/null +++ b/tests/test_serve_reload_off_loop.py @@ -0,0 +1,121 @@ +"""MCP requests are not blocked by a graph.json reload (#5371). + +#5181 made ``_GraphContextCache.load`` hand a stale entry to a second caller +while a rebuild is in flight, but ``call_tool`` / ``read_resource`` ran the +load synchronously on the event loop, so no second caller could ever get in. +These tests drive the server's own request handlers concurrently. +""" +from __future__ import annotations + +import json +import os +import time + +import anyio +import pytest + +pytest.importorskip("mcp") +from mcp import types # noqa: E402 + +from graphify import serve as serve_mod # noqa: E402 + +DELAY = 0.8 +GRAPH = {"directed": True, "nodes": [{"id": "a", "label": "A", "community": 0}], "edges": []} + + +def _server(tmp_path, monkeypatch, slow_only=None): + path = tmp_path / "graph.json" + path.write_text(json.dumps(GRAPH)) + server = serve_mod._build_server(str(path)) # warm load, fast + original = serve_mod._GraphContextCache._load_entry + + def slow(self, resolved_path, key): + if slow_only is None or slow_only in resolved_path: + time.sleep(DELAY) # stands in for parsing a large graph.json + return original(self, resolved_path, key) + + monkeypatch.setattr(serve_mod._GraphContextCache, "_load_entry", slow) + later = time.time() + 10 + os.utime(path, (later, later)) # what any rebuild does + return server + + +def _call_params(arguments=None): + return ("tools/call", types.CallToolRequestParams(name="graph_stats", arguments=arguments or {})) + + +def _read_params(uri): + return ("resources/read", types.ReadResourceRequestParams(uri=uri)) + + +async def _send(server, method, params) -> str: + """Dispatch one request to the server's own handler, on mcp 1.x or 2.x.""" + if hasattr(server, "request_handlers"): # mcp 1.x + if method == "tools/call": + req = types.CallToolRequest(method=method, params=params) + return (await server.request_handlers[types.CallToolRequest](req)).root.content[0].text + req = types.ReadResourceRequest(method=method, params=params) + return (await server.request_handlers[types.ReadResourceRequest](req)).root.contents[0].text + # mcp 2.x: graphify's on_* callbacks ignore the request context + result = await server.get_request_handler(method).handler(None, params) + return result.content[0].text if method == "tools/call" else result.contents[0].text + + +async def _timed(server, req, out, k, arrival): + # Each request has a planned arrival time and is timed from it. Timing + # from when the coroutine starts would hide the stall: a load blocking + # the event loop also delays the start of every other coroutine. + await anyio.sleep_until(arrival) + await _send(server, *req) + out[k] = anyio.current_time() - arrival + + +def _concurrent(server, reqs): + async def main(): + out = {} + base = anyio.current_time() + async with anyio.create_task_group() as tg: + for k, req in enumerate(reqs): + tg.start_soon(_timed, server, req, out, k, base + 0.05 * k) + return [out[k] for k in range(len(reqs))] + + return anyio.run(main) + + +def test_concurrent_tool_calls_served_from_current_graph_during_reload(tmp_path, monkeypatch): + times = _concurrent(_server(tmp_path, monkeypatch), [_call_params() for _ in range(3)]) + assert times[0] >= DELAY # the call that triggered the reload pays for it + assert max(times[1:]) < DELAY / 2, times # the others get the current graph + + +def test_resource_read_not_blocked_by_a_tool_call_reload(tmp_path, monkeypatch): + times = _concurrent(_server(tmp_path, monkeypatch), [_call_params(), _read_params("graphify://stats")]) + assert times[1] < DELAY / 2, times + + +def test_project_call_during_default_reload_answers_its_own_graph(tmp_path, monkeypatch): + """A project_path call that arrives while the default graph reloads in a + worker thread is answered quickly from the project's graph, and the call + that triggered the reload is still answered from the default graph.""" + proj = tmp_path / "proj" + (proj / "graphify-out").mkdir(parents=True) + nodes = [{"id": f"p{i}", "label": f"P{i}", "community": 0} for i in range(3)] + (proj / "graphify-out" / "graph.json").write_text(json.dumps({**GRAPH, "nodes": nodes})) + server = _server(tmp_path, monkeypatch, slow_only=str(tmp_path / "graph.json")) + out, took = {}, {} + + async def call(k, arguments, arrival): + await anyio.sleep_until(arrival) + out[k] = await _send(server, *_call_params(arguments)) + took[k] = anyio.current_time() - arrival + + async def main(): + base = anyio.current_time() + async with anyio.create_task_group() as tg: + tg.start_soon(call, 0, {}, base) + tg.start_soon(call, 1, {"project_path": str(proj)}, base + 0.05) + + anyio.run(main) + assert "Nodes: 1" in out[0], out + assert "Nodes: 3" in out[1], out + assert took[1] < DELAY / 2, took