Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix: server uvloop conflicts with nest_asyncio (#166)

MaojiaSheng621ebdff2d47
diff --git a/openviking_cli/utils/async_utils.py b/openviking_cli/utils/async_utils.py
index bc60c8b92..f2ced92fa 100644
--- a/openviking_cli/utils/async_utils.py
+++ b/openviking_cli/utils/async_utils.py
@@ -12,8 +12,8 @@ from typing import Coroutine, TypeVar
T = TypeVar("T")
_lock = threading.Lock()
-_loop: asyncio.AbstractEventLoop = None
-_loop_thread: threading.Thread = None
+_loop: asyncio.AbstractEventLoop | None = None
+_loop_thread: threading.Thread | None = None
def _get_loop() -> asyncio.AbstractEventLoop:
@@ -34,7 +34,7 @@ def _get_loop() -> asyncio.AbstractEventLoop:
def _shutdown_loop():
"""Shutdown the shared loop on process exit."""
global _loop, _loop_thread
- if _loop is not None and not _loop.is_closed():
+ if _loop is not None and not _loop.is_closed() and _loop_thread is not None:
_loop.call_soon_threadsafe(_loop.stop)
_loop_thread.join(timeout=5)
_loop.close()
@@ -44,12 +44,14 @@ def _shutdown_loop():
def run_async(coro: Coroutine[None, None, T]) -> T:
"""
- Run async coroutine from sync code, handling nested event loops.
+ Run async coroutine from sync code.
+
+ This function uses a shared background-thread event loop to run coroutines
+ from synchronous code. This approach avoids compatibility issues with uvloop
+ and other event loop implementations that don't support nested loops.
- This function safely runs a coroutine whether or not there's already
- a running event loop (e.g., when called from within an MCP server).
- When no loop is running, uses a shared background-thread loop so that
- stateful async objects (e.g. httpx.AsyncClient) stay on the same loop.
+ The shared loop ensures stateful async objects (e.g. httpx.AsyncClient) stay
+ on the same loop across multiple calls.
Args:
coro: The coroutine to run
@@ -57,13 +59,6 @@ def run_async(coro: Coroutine[None, None, T]) -> T:
Returns:
The result of coroutine
"""
- try:
- loop = asyncio.get_running_loop()
- import nest_asyncio
-
- nest_asyncio.apply()
- return loop.run_until_complete(coro)
- except RuntimeError:
- loop = _get_loop()
- future = asyncio.run_coroutine_threadsafe(coro, loop)
- return future.result()
+ loop = _get_loop()
+ future = asyncio.run_coroutine_threadsafe(coro, loop)
+ return future.result()
diff --git a/pyproject.toml b/pyproject.toml
index adb70ab47..dd777a688 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -47,7 +47,6 @@ dependencies = [
"uvicorn>=0.39.0",
"xxhash>=3.0.0",
"jinja2>=3.1.6",
- "nest-asyncio>=1.5.0",
"tabulate>=0.9.0",
"urllib3>=2.6.3",
"protobuf>=6.33.5",
diff --git a/uv.lock b/uv.lock
index 2549a33cb..0633c6084 100644
--- a/uv.lock
+++ b/uv.lock
@@ -1231,12 +1231,12 @@ wheels = [
]
[[package]]
-name = "nest-asyncio"
-version = "1.6.0"
+name = "nest-asyncio2"
+version = "1.7.2"
source = { registry = "https://pypi.org/simple" }
-sdist = { url = "https://files.pythonhosted.org/packages/83/f8/51569ac65d696c8ecbee95938f89d4abf00f47d58d48f6fbabfe8f0baefe/nest_asyncio-1.6.0.tar.gz", hash = "sha256:6f172d5449aca15afd6c646851f4e31e02c598d553a667e38cafa997cfec55fe", size = 7418, upload-time = "2024-01-21T14:25:19.227Z" }
+sdist = { url = "https://files.pythonhosted.org/packages/b4/73/731debf26e27e0a0323d7bda270dc2f634b398e38f040a09da1f4351d0aa/nest_asyncio2-1.7.2.tar.gz", hash = "sha256:1921d70b92cc4612c374928d081552efb59b83d91b2b789d935c665fa01729a8", size = 14743, upload-time = "2026-02-13T00:34:04.386Z" }
wheels = [
- { url = "https://files.pythonhosted.org/packages/a0/c4/c2971a3ba4c6103a3d10c4b0f24f461ddc027f0f09763220cf35ca1401b3/nest_asyncio-1.6.0-py3-none-any.whl", hash = "sha256:87af6efd6b5e897c81050477ef65c62e2b2f35d51703cae01aff2905b1852e1c", size = 5195, upload-time = "2024-01-21T14:25:17.223Z" },
+ { url = "https://files.pythonhosted.org/packages/c5/3c/3179b85b0e1c3659f0369940200cd6d0fa900e6cefcc7ea0bc6dd0e29ffb/nest_asyncio2-1.7.2-py3-none-any.whl", hash = "sha256:f5dfa702f3f81f6a03857e9a19e2ba578c0946a4ad417b4c50a24d7ba641fe01", size = 7843, upload-time = "2026-02-13T00:34:02.691Z" },
]
[[package]]
@@ -1268,7 +1268,7 @@ dependencies = [
{ name = "jinja2" },
{ name = "json-repair" },
{ name = "markdownify" },
- { name = "nest-asyncio" },
+ { name = "nest-asyncio2" },
{ name = "openai" },
{ name = "pdfminer-six" },
{ name = "pdfplumber" },
@@ -1319,7 +1319,7 @@ requires-dist = [
{ name = "markdownify", specifier = ">=0.11.0" },
{ name = "mypy", marker = "extra == 'dev'", specifier = ">=1.0.0" },
{ name = "myst-parser", marker = "extra == 'doc'", specifier = ">=2.0.0" },
- { name = "nest-asyncio", specifier = ">=1.5.0" },
+ { name = "nest-asyncio2", specifier = ">=1.7.2" },
{ name = "openai", specifier = ">=1.0.0" },
{ name = "pdfminer-six", specifier = ">=20251230" },
{ name = "pdfplumber", specifier = ">=0.10.0" },