Skip to content

Performance: Synchronous Milvus search blocks the async event loop in execute_tool() #110

Closed as not planned

Description

@Kunal-Somani

Hi team! I am an aspiring GSoC 2026 contributor auditing the concurrency performance of the chat servers. I noticed a critical bottleneck in the tool execution pipeline that blocks the async event loop.

Root Cause Analysis

In both server/app.py (WebSocket) and server-https/app.py (FastAPI), the execute_tool() function is defined as async, but it directly calls milvus_search(), which contains synchronous blocking I/O (PyMilvus connections.connect, collection.load, and collection.search).

Because this synchronous network call is executed directly on the main thread rather than being offloaded, it freezes the Uvicorn/asyncio event loop. Under concurrent load, the server will completely halt all other WebSockets, HTTP connections, and health checks until the Milvus I/O operation finishes.

Proposed Architecture Fix

We need to offload the synchronous database call to a background worker thread.

In server/app.py (Standard Asyncio):
Modify the call in execute_tool to use asyncio.to_thread:

result = await asyncio.to_thread(milvus_search, query, top_k)

In server-https/app.py (FastAPI):
Utilize FastAPI's built-in threadpool concurrency:

from fastapi.concurrency import run_in_threadpool
result = await run_in_threadpool(milvus_search, query, top_k)

Impact

This small change ensures the main event loop remains unblocked, allowing the servers to handle concurrent LLM streaming and multiple users while waiting for the vector database to return results.

If the core team agrees this optimization is necessary for production, I have a local fix prepared and would be happy to submit a PR!

Activity

  1. Kunal-Somani commented on Mar 6, 2026

    @Kunal-Somani
    Author

    /assign

  2. Kunal-Somani commented on Mar 6, 2026

    @Kunal-Somani
    Author

    Comment for Issue #110

    @franciscojavierarceo

    I have submitted a Pull Request (linked above) that provides a surgical fix for the event loop starvation and health check middleware issues discussed here.

    Technical Validation

    During local profiling, I confirmed that the milvus_search tool—which handles heavy embedding and network I/O—was executing synchronously on the main thread. This resulted in the event loop freezing for ~10.14 seconds, paralyzing the server's ability to process concurrent heartbeats or new connections.

    The Fix:

    • Asynchronous Offloading: I implemented loop.run_in_executor() with functools.partial to offload the synchronous search workload to a background thread pool. This ensures the main asyncio thread remains responsive.
    • Middleware Stabilization: I corrected the health_check signature to prevent the HTTP 500 AttributeError during client handshakes.

    Profiling Results

    • Before: Heartbeat logs (0.1s intervals) halted completely during tool execution.
    • After: Heartbeat logs continued uninterrupted while the background search processed, confirming a healthy event loop.

    The PR is now signed-off (DCO) and squashed into a clean, minimal diff. Ready for your final review!

  3. chasecadet commented on Nov 20, 2026

    @chasecadet
    Contributor

    Thank you for reporting this!

    We are closing pre-restructure issues targeting the legacy server/, server-https/, manifests/, kagent-feast-mcp/, or in-process SentenceTransformer prototype code. In #207, #210, #233, #240, and #249, kubeflow/docs-agent migrated to dedicated TEI (text-embeddings-inference), FastMCP (docs-agent-mcp/mcp-server/), shared modular KFP pipelines (docs-agent-mcp/pipelines/), and Helm charts (docs-agent-mcp/charts/).

    If you still observe an equivalent issue on the current main stack, please feel free to open a new issue using the current issue templates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions