Fix blocking async server handlers#618
Merged
Merged
Conversation
jonsaadfalcon
pushed a commit
that referenced
this pull request
Jul 6, 2026
The OpenAI-compat and Ollama engines exposed stream()/stream_full() as async def but iterated a synchronous httpx.Client.iter_lines() internally, blocking the single event loop on every inter-token read (serializing concurrent chats; one wedged upstream read froze the whole API). Convert both to a shared AsyncHTTPEngineMixin using httpx.AsyncClient + aiter_lines() with a per-event-loop pooled client and the configured timeout applied; map mid-stream transport errors (RemoteProtocolError/ReadError) to EngineConnectionError via a deliberately narrow set that keeps CancelledError/GeneratorExit propagating; handle non-2xx explicitly (incl. 3xx and a typed EngineContextLengthError for context-window overflow 400s); switch litellm streaming to acompletion; and offload the blocking non-streaming handlers and websocket generate() to asyncio.to_thread. No public API change. Strong MockTransport-based tests, including a pin that the async path never touches the sync client. Complements #618. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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
httpx.AsyncClientcallsCloses #219
Testing
OPENJARVIS_HOME=/tmp/openjarvis-test-home-219 PYTHONPATH=src python3 -m pytest tests/server/test_model_management.py tests/server/test_speech_routes.py tests/server/test_agent_manager_routes.py tests/telemetry/test_store.pypython3 -m ruff check src/openjarvis/server/routes.py src/openjarvis/server/api_routes.py src/openjarvis/server/agent_manager_routes.py src/openjarvis/telemetry/store.py tests/server/test_model_management.py tests/server/test_speech_routes.py tests/server/test_agent_manager_routes.py tests/telemetry/test_store.pygit diff --checkNotes: local
uv run pytest ...is blocked by local uv 0.5.9 failing to parse the current lockfile (uv.lockline 4129, editable package without version). A broader local adjacent run passed 162 tests and had 3 unrelated existing environment failures from the missing native memory extension / old metrics assertion.