Skip to content

fix: keep server inference off the event loop (issue #18) - #20

Merged
1Utkarsh1 merged 2 commits into
mainfrom
fix/server-inference-event-loop
Oct 24, 2026
Merged

1Utkarsh1 merged 2 commits into
mainfrom
fix/server-inference-event-loop

Conversation

@1Utkarsh1

@1Utkarsh1 1Utkarsh1 commented Oct 24, 2026 •

Copy link
Copy Markdown
Owner

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_thread pattern 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.

Copilot AI balanced review requested due to automatic review settings October 24, 2026 11:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@1Utkarsh1
1Utkarsh1 merged commit eb0bc93 into main Oct 24, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Investigate potential event-loop blocking in async server routes

2 participants