Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(mem0): fix the event loop closing issue by ensuring it always runs in the same event loop (#1157)
Osier-Yib57f06f66bb3
diff --git a/examples/functionality/long_term_memory/mem0/memory_example.py b/examples/functionality/long_term_memory/mem0/memory_example.pyindex 5e0529ce6..c7a5507ae 100644--- a/examples/functionality/long_term_memory/mem0/memory_example.py+++ b/examples/functionality/long_term_memory/mem0/memory_example.py@@ -133,8 +133,8 @@ async def main() -> None:)print(f"Retrieved preference memories: {preference_memories}\n")- # Example 5: ReActAgent with long term memory- print("5. ReActAgent with long term memory")+ # Example 3: ReActAgent with long term memory+ print("3. ReActAgent with long term memory")print("-" * 40)toolkit = Toolkit()diff --git a/src/agentscope/memory/_long_term_memory/_mem0/_mem0_utils.py b/src/agentscope/memory/_long_term_memory/_mem0/_mem0_utils.pyindex b9f972dfc..ada175237 100644--- a/src/agentscope/memory/_long_term_memory/_mem0/_mem0_utils.py+++ b/src/agentscope/memory/_long_term_memory/_mem0/_mem0_utils.py@@ -5,6 +5,7 @@ This module provides wrapper classes that allow AgentScope models to be usedwith the mem0 library for long-term memory functionality."""import asyncio+import atexitimport threadingfrom typing import Any, Coroutine, Dict, List, Literal@@ -17,62 +18,116 @@ from ....embedding import EmbeddingModelBasefrom ....model import ChatModelBase, ChatResponse-def _run_async(- coro: Coroutine,- target_loop: asyncio.AbstractEventLoop | None = None,-) -> Any:- """Run an async coroutine, handling nested event loops.+class _EventLoopManager:+ """Global event loop manager for running async operations in sync context.++ This manager creates and maintains a persistent background event loop+ that runs in a separate daemon thread. This ensures that async model+ clients (like Ollama AsyncClient) remain bound to the same event loop+ across multiple calls, avoiding "Event loop is closed" errors.+ """++ _DEFAULT_TIMEOUT = 5.0 # Default timeout in seconds++ def __init__(self) -> None:+ """Initialize the event loop manager."""+ self.loop: asyncio.AbstractEventLoop | None = None+ self.thread: threading.Thread | None = None+ self.lock = threading.Lock()+ self.loop_started = threading.Event()++ # Register cleanup function to be called on program exit+ atexit.register(self.cleanup)++ def get_loop(self) -> asyncio.AbstractEventLoop:+ """Get or create the persistent background event loop.++ Returns:+ `asyncio.AbstractEventLoop`:+ The persistent event loop running in a background thread.++ Raises:+ `RuntimeError`: If the event loop fails to start within the+ timeout.+ """+ with self.lock:+ if self.loop is None or self.loop.is_closed():++ def run_loop() -> None:+ """Run the event loop in the background thread."""+ loop = asyncio.new_event_loop()+ asyncio.set_event_loop(loop)+ # Store the loop reference before starting+ self.loop = loop+ self.loop_started.set()+ loop.run_forever()++ # Clear the event before starting the thread+ self.loop_started.clear()++ # Create and start the background thread+ self.thread = threading.Thread(+ target=run_loop,+ daemon=True,+ name="AgentScope-AsyncLoop",+ )+ self.thread.start()++ # Wait for the loop to be ready+ if not self.loop_started.wait(timeout=self._DEFAULT_TIMEOUT):+ raise RuntimeError(+ "Timeout waiting for event loop to start",+ )++ # After waiting, self.loop should be set by the background thread+ assert (+ self.loop is not None+ ), "Event loop was not initialized properly"+ return self.loop++ def cleanup(self) -> None:+ """Cleanup the event loop and thread on program exit."""+ with self.lock:+ if self.loop is not None and not self.loop.is_closed():+ # Stop the event loop gracefully+ self.loop.call_soon_threadsafe(self.loop.stop)++ # Wait for the thread to finish+ if self.thread is not None and self.thread.is_alive():+ self.thread.join(timeout=self._DEFAULT_TIMEOUT)++ # Close the loop+ self.loop.close()+ self.loop = None+ self.thread = None+++# Global event loop manager instance+_event_loop_manager = _EventLoopManager()+++def _run_async_in_persistent_loop(coro: Coroutine) -> Any:+ """Run an async coroutine in the persistent background event loop.++ This function uses a global event loop manager to ensure that all+ async operations run in the same event loop, which is crucial for+ async clients like Ollama that bind to a specific event loop.+Args:coro (`Coroutine`):The coroutine to run.- target_loop (`asyncio.AbstractEventLoop | None`, optional):- Optional target event loop where the coroutine should- run. If provided and still valid, the coroutine will be- scheduled in that loop using run_coroutine_threadsafe.+Returns:`Any`:The result of the coroutine.++ Raises:+ `RuntimeError`:+ If there's an error running the coroutine."""- # If we have a target loop (where the model client was created),- # use run_coroutine_threadsafe to run in that loop- if target_loop is not None and not target_loop.is_closed():- future = asyncio.run_coroutine_threadsafe(coro, target_loop)- return future.result()-- # No target loop or target loop is closed, use standard approach- try:- # Try to get the running event loop- asyncio.get_running_loop()- # There's a running event loop. Use thread-based approach to avoid- # event loop binding issues with clients like Ollama.-- result_container = {"result": None, "exception": None}- event = threading.Event()-- def run_in_thread() -> None:- # Create a new event loop in this thread- new_loop = asyncio.new_event_loop()- asyncio.set_event_loop(new_loop)- try:- result_container["result"] = new_loop.run_until_complete(coro)- except Exception as e:- result_container["exception"] = e- finally:- new_loop.close()- event.set()-- # Run in a separate thread to avoid event loop binding issues- thread = threading.Thread(target=run_in_thread)- thread.start()- event.wait()-- exception = result_container["exception"]- if exception is not None:- raise exception- return result_container["result"]- except RuntimeError:- # No running event loop, we can use asyncio.run directly- return asyncio.run(coro)+ loop = _event_loop_manager.get_loop()+ future = asyncio.run_coroutine_threadsafe(coro, loop)+ return future.result()class AgentScopeLLM(LLMBase):@@ -98,14 +153,6 @@ class AgentScopeLLM(LLMBase):raise ValueError("`model` must be an instance of ChatModelBase")self.agentscope_model = self.config.model- # Store the event loop where the model was created (if any)- # This helps ensure the model client (e.g., Ollama) is used in the- # same loop context to avoid event loop binding issues- try:- self._creation_loop = asyncio.get_running_loop()- except RuntimeError:- # No running loop when model was created- self._creation_loop = Nonedef _parse_response(self,@@ -208,12 +255,11 @@ class AgentScopeLLM(LLMBase):tools=tools,)- # Use _run_async which will handle event loop context properly- # Pass the creation loop to ensure the model client runs in the- # same loop where it was created, avoiding Event binding issues- response = _run_async(+ # Run in the persistent event loop+ # This ensures the model client (e.g., Ollama AsyncClient)+ # always runs in the same event loop, avoiding binding issues+ response = _run_async_in_persistent_loop(_async_call(),- target_loop=self._creation_loop,)has_tool = tools is not None@@ -260,14 +306,6 @@ class AgentScopeEmbedding(EmbeddingBase):)self.agentscope_model = self.config.model- # Store the event loop where the model was created (if any)- # This helps ensure the model client (e.g., Ollama) is used in the- # same loop context to avoid event loop binding issues- try:- self._creation_loop = asyncio.get_running_loop()- except RuntimeError:- # No running loop when model was created- self._creation_loop = Nonedef embed(self,@@ -302,12 +340,11 @@ class AgentScopeEmbedding(EmbeddingBase):response = await self.agentscope_model(text_list)return response- # Use _run_async which will handle event loop context properly- # Pass the creation loop to ensure the model client runs in the- # same loop where it was created, avoiding Event binding issues- response = _run_async(+ # Run in the persistent event loop+ # This ensures the model client (e.g., Ollama AsyncClient)+ # always runs in the same event loop, avoiding binding issues+ response = _run_async_in_persistent_loop(_async_call(),- target_loop=self._creation_loop,)# Extract the embedding vector from the first Embedding objectdiff --git a/tests/mem0_utils_test.py b/tests/mem0_utils_test.pynew file mode 100644index 000000000..5b11cccd4--- /dev/null+++ b/tests/mem0_utils_test.py@@ -0,0 +1,207 @@+# -*- coding: utf-8 -*-+"""Unit tests for AgentScopeLLM with Ollama using asyncio.gather() for+parallel calls."""+import asyncio+from typing import Any+from unittest.async_case import IsolatedAsyncioTestCase+from unittest.mock import patch, AsyncMock, MagicMock++from agentscope.memory._long_term_memory._mem0._mem0_utils import AgentScopeLLM+from agentscope.model import OllamaChatModel++# Try to import BaseLlmConfig, but handle ImportError gracefully+try:+ from mem0.configs.llms.base import BaseLlmConfig+except ImportError:+ # If mem0 is not installed, create a mock class+ BaseLlmConfig = MagicMock+++class OllamaMessageMock:+ """Mock class for Ollama message objects."""++ def __init__(+ self,+ content: str = "",+ thinking: str = "",+ tool_calls: list = None,+ ) -> None:+ self.content = content+ self.thinking = thinking+ self.tool_calls = tool_calls or []+++class OllamaResponseMock:+ """Mock class for Ollama response objects."""++ def __init__(+ self,+ content: str = "",+ thinking: str = "",+ tool_calls: list = None,+ prompt_eval_count: int = 10,+ eval_count: int = 20,+ ) -> None:+ self.message = OllamaMessageMock(+ content=content,+ thinking=thinking,+ tool_calls=tool_calls or [],+ )+ self.prompt_eval_count = prompt_eval_count+ self.eval_count = eval_count++ def get(self, key: str, default: Any | None = None) -> Any:+ """Mock dict-like get method."""+ return getattr(self, key, default)++ def __contains__(self, key: str) -> bool:+ """Mock dict-like contains method to support 'in' operator."""+ return hasattr(self, key)+++class TestAgentScopeLLMWithOllama(IsolatedAsyncioTestCase):+ """Test cases for AgentScopeLLM with+ OllamaChatModel using asyncio.gather()."""++ def test_agentscope_llm_parallel_calls_with_asyncio_gather(self) -> None:+ """Test parallel calls using asyncio.gather() - original bug scenario.++ This test reproduces the original bug where parallel calls using+ asyncio.gather() would cause "Event loop is closed" errors.+ The persistent event loop management fix should resolve this issue.+ """+ with patch("ollama.AsyncClient") as mock_client_class:+ mock_client = AsyncMock()+ mock_client_class.return_value = mock_client++ # Create OllamaChatModel instance+ ollama_model = OllamaChatModel(+ model_name="qwen3:14b",+ stream=False,+ enable_thinking=False,+ )+ ollama_model.client = mock_client++ # Mock Ollama chat response+ mock_ollama_response = OllamaResponseMock(+ content="Test response",+ )+ mock_client.chat = AsyncMock(+ return_value=mock_ollama_response,+ )++ # Create AgentScopeLLM config+ # Directly set model attribute, simpler than using+ # LlmConfig constructor+ llm_config = BaseLlmConfig()+ llm_config.model = ollama_model++ llm = AgentScopeLLM(config=llm_config)++ # Create multiple different messages for parallel calls+ messages_list = [+ [{"role": "user", "content": "I like staying in homestays"}],+ [{"role": "user", "content": "I prefer coffee over tea"}],+ [{"role": "user", "content": "My favorite color is blue"}],+ [{"role": "user", "content": "I work as a software engineer"}],+ [+ {+ "role": "user",+ "content": "I enjoy reading science fiction",+ },+ ],+ ]++ # Define async function to call generate_response+ async def call_llm(messages: list[dict[str, str]]) -> str | dict:+ """Call LLM generate_response in async context."""+ return llm.generate_response(messages)++ # Use asyncio.gather() to make parallel calls+ # Without the fix, this would fail with+ # "Event loop is closed" error+ loop = asyncio.new_event_loop()+ asyncio.set_event_loop(loop)+ try:+ results = loop.run_until_complete(+ asyncio.gather(+ *[call_llm(msgs) for msgs in messages_list],+ ),+ )+ finally:+ loop.close()++ # Verify all parallel calls completed successfully+ self.assertEqual(len(results), len(messages_list))+ for result in results:+ self.assertIsInstance(result, str)+ self.assertGreater(len(result), 0)++ # Verify Ollama client was called for each parallel request+ self.assertEqual(+ mock_client.chat.call_count,+ len(messages_list),+ )++ async def test_agentscope_llm_async_gather_in_async_context(self) -> None:+ """Test asyncio.gather() in an async test context.++ This test uses the async test framework to properly test+ parallel calls using asyncio.gather().+ """+ with patch("ollama.AsyncClient") as mock_client_class:+ mock_client = AsyncMock()+ mock_client_class.return_value = mock_client++ # Create OllamaChatModel instance+ ollama_model = OllamaChatModel(+ model_name="qwen3:14b",+ stream=False,+ enable_thinking=False,+ )+ ollama_model.client = mock_client++ # Mock Ollama chat response+ mock_ollama_response = OllamaResponseMock(+ content="Test response",+ )+ mock_client.chat = AsyncMock(+ return_value=mock_ollama_response,+ )++ # Create AgentScopeLLM config+ # Directly set model attribute, simpler than using+ # LlmConfig constructor+ llm_config = BaseLlmConfig()+ llm_config.model = ollama_model++ llm = AgentScopeLLM(config=llm_config)++ messages_list = [+ [{"role": "user", "content": "First message"}],+ [{"role": "user", "content": "Second message"}],+ [{"role": "user", "content": "Third message"}],+ ]++ # Define async function to call generate_response+ async def call_llm(messages: list[dict[str, str]]) -> str | dict:+ """Call LLM generate_response."""+ return llm.generate_response(messages)++ # Use asyncio.gather() to make parallel calls+ # This is the exact scenario that was causing the bug+ results = await asyncio.gather(+ *[call_llm(msgs) for msgs in messages_list],+ )++ # Verify all parallel calls completed successfully+ self.assertEqual(len(results), len(messages_list))+ for result in results:+ self.assertIsInstance(result, str)+ self.assertGreater(len(result), 0)++ # Verify Ollama client was called for each parallel request+ self.assertEqual(+ mock_client.chat.call_count,+ len(messages_list),+ )