From b3c8a997c5b55058dcca192e62997a60d27a85ad Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 2 Nov 2026 07:08:18 +0000 Subject: [PATCH] Keep the SSE endpoint's blocking work off the event loop /v1/explain/stream is an async handler, but it ran the CPU-bound analysis and iterated the provider's synchronous stream inline. The Anthropic SDK and the Gemini client both stream over blocking HTTP, so for the whole enrichment (Gemini's timeout is 120s) the event loop was stuck: every other request, /healthz included, waited behind it. The Procfile runs one worker, so that is the whole service. The analysis now runs via run_in_threadpool and the provider stream via iterate_in_threadpool, the helpers Starlette provides for this. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UigC7feaY8NkAy9oay3GDW --- explain_my_code/api/main.py | 10 +++++++-- tests/test_api.py | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/explain_my_code/api/main.py b/explain_my_code/api/main.py index d68a57d..fb15bf6 100644 --- a/explain_my_code/api/main.py +++ b/explain_my_code/api/main.py @@ -14,6 +14,7 @@ from fastapi import FastAPI, HTTPException, Request from fastapi.responses import HTMLResponse, JSONResponse, StreamingResponse from fastapi.staticfiles import StaticFiles +from starlette.concurrency import iterate_in_threadpool, run_in_threadpool from explain_my_code import __version__ from explain_my_code.api.schemas import ( @@ -183,6 +184,10 @@ async def explain_stream(payload: ExplainRequest) -> StreamingResponse: The static pass takes about a millisecond, so the page can render fully before the model has produced a token. The LLM is a progressive enhancement, not a dependency. + + This handler is async, so everything blocking runs in the threadpool: the analysis + is CPU-bound and the provider SDKs stream over blocking HTTP. Run inline, a single + enrichment stalled every other request on the event loop until the model finished. """ async def events() -> AsyncIterator[str]: @@ -190,7 +195,8 @@ def sse(event: str, data: Any) -> str: return f"event: {event}\ndata: {json.dumps(data)}\n\n" try: - result = explain( + result = await run_in_threadpool( + explain, payload.code, language=payload.language, level=payload.level, @@ -215,7 +221,7 @@ def sse(event: str, data: Any) -> str: yield sse("enrich_start", {"provider": provider.name, "model": provider.model}) prompt = build_context(result.analysis, result.level, result.source) try: - for chunk in provider.stream(SYSTEM_PROMPT, prompt): + async for chunk in iterate_in_threadpool(provider.stream(SYSTEM_PROMPT, prompt)): yield sse("delta", {"text": chunk}) except Exception as exc: # provider SDKs raise their own error types yield sse("error", {"error": "enrichment_failed", "detail": str(exc)}) diff --git a/tests/test_api.py b/tests/test_api.py index 9358b18..9d5a7dd 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -129,6 +129,50 @@ def test_streaming_reports_a_missing_provider_without_failing_the_request(monkey assert "no_provider" in body +class _SlowProvider: + """Streams like a real SDK: blocking network reads between chunks.""" + + name = "fake" + model = "fake-model" + + def stream(self, system, prompt): + import time + + for chunk in ('{"summary": "', "slow", '"}'): + time.sleep(0.3) + yield chunk + + +async def test_streaming_enrichment_does_not_block_other_requests(monkeypatch): + """A blocking provider stream used to run on the event loop itself, so every other + request (here /healthz) waited for the model to finish.""" + import asyncio + import time + + import httpx + + monkeypatch.setattr("explain_my_code.api.main.get_provider", lambda name=None: _SlowProvider()) + transport = httpx.ASGITransport(app=app) + async with httpx.AsyncClient(transport=transport, base_url="http://test") as ac: + started = time.perf_counter() + + async def stream() -> tuple[str, float]: + response = await ac.post("/v1/explain/stream", json={"code": PY, "enrich": True}) + return response.text, time.perf_counter() - started + + async def health() -> float: + await asyncio.sleep(0.1) # let the stream start first + response = await ac.get("/healthz") + assert response.status_code == 200 + return time.perf_counter() - started + + (body, stream_done), health_done = await asyncio.gather(stream(), health()) + + assert "event: delta" in body and "slow" in body + assert stream_done >= 0.9 + assert health_done < 0.6, f"/healthz waited {health_done:.2f}s behind the stream" + + # -- cache -------------------------------------------------------------------------