Repository navigation
fix: keep server inference off the event loop (issue #18) - #20
Merged
Merged
Conversation
…event-loop # Conflicts: # CHANGELOG.md
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 #18.
Synchronous chat, legacy completion and embedding inference runs directly in async routes, blocking unrelated HTTP work. With a 250 ms fake backend, two concurrent requests took 506–549 ms and scheduled health work waited 507–550 ms. Initial model loading also blocks the loop, including on the otherwise-offloaded Responses route.
Use the existing
asyncio.to_threadpattern for those inference calls and all lazy model loading. Synchronize construction so concurrent first requests initialize one model. Keep synchronous SSE iterators in their existing workers, and offload fallback embedding work too. This does not add model batching or make a backend safe for concurrent calls; request cancellation does not terminate work already running in a worker.Validation: 476 tests passed, 16 skipped, 237 subtests passed, including official OpenAI/MCP SDK checks. Eight focused concurrency tests verify health responses while work is still blocked, cold loading across all four inference routes, single initialization, native/fallback embeddings and streaming. The affected tests fail on the original implementation. CI explicitly runs them in the optional-server step on Linux, macOS and Windows. Ruff, Black and fresh strict Sphinx passed.
The fixed ASGI fixture was also exercised with 1, 2, 10, 25 and 50 concurrent requests for chat, completion and embeddings. Inference ran off the loop, and scheduled health work completed in 3–19 ms. These measurements use a sleeping fake backend and do not establish real-model throughput or backend thread safety.
This PR is independent of the Transformers context-window fix in #19 and is not included in PyPI 0.3.0.