Skip to content
Python

fix(client): stop both owned event loops on close (#91)

u7-u73370a715dd5c
diff --git a/hy_memory/client.py b/hy_memory/client.py
index a4e0bda..0464496 100644
--- a/hy_memory/client.py
+++ b/hy_memory/client.py
@@ -30,6 +30,7 @@ import asyncio
import json
import logging
import os
+import threading
import time
import uuid
from datetime import datetime
@@ -377,6 +378,14 @@ class _LoopThread:
return await await_concurrent_future_guarded(future, self._loop)
def stop(self):
+ if self._loop.is_closed():
+ return
+ if threading.current_thread() is self._thread:
+ # Called from inside the loop: stopping and joining ourselves would
+ # deadlock. Just ask run_forever() to return.
+ self._loop.stop()
+ return
+
def _stop():
if self._lag_handle is not None:
self._lag_handle.cancel()
@@ -386,6 +395,8 @@ class _LoopThread:
self._loop.call_soon_threadsafe(_stop)
self._thread.join(timeout=5)
+ if not self._thread.is_alive() and not self._loop.is_closed():
+ self._loop.close()
class HyMemoryClient:
@@ -2152,14 +2163,33 @@ class HyMemoryClient:
from .metrics import MetricsCollector
return await MetricsCollector.get().get_snapshot(minutes=minutes)
+ def _stop_owned_loops(self) -> None:
+ """Stop every event-loop thread this client owns.
+
+ Solo clients create both a write loop and a search loop. Stopping only
+ the write loop left the search thread (and a self-wakeup timer every
+ 50ms) alive for the process lifetime. Shared runtimes own their loops
+ and manage them through ``SharedRuntime.aclose()``.
+ """
+ if not getattr(self, "_owns_runtime", False):
+ return
+ seen = set()
+ for loop_thread in (
+ getattr(self, "_loop_thread", None),
+ getattr(self, "_search_loop_thread", None),
+ ):
+ if loop_thread is None or id(loop_thread) in seen:
+ continue
+ seen.add(id(loop_thread))
+ loop_thread.stop()
+
def close(self):
"""关闭客户端,释放所有资源"""
- if self._registry is not None:
- self._loop_thread.run(self._close_async())
- # solo 模式:本 client 拥有 loop_thread,可以安全 stop
- # shared 模式:loop_thread 由 runtime 管,留给 runtime.aclose() 关
- if hasattr(self, '_loop_thread') and self._owns_runtime:
- self._loop_thread.stop()
+ try:
+ if self._registry is not None:
+ self._loop_thread.run(self._close_async())
+ finally:
+ self._stop_owned_loops()
def history(self, memory_id: str) -> List[Dict[str, Any]]:
"""
@@ -6155,4 +6185,7 @@ class HyMemoryClient:
return self
async def __aexit__(self, *args):
- await self._close_async()
+ try:
+ await self._close_async()
+ finally:
+ self._stop_owned_loops()
diff --git a/tests/test_client_loop_lifecycle.py b/tests/test_client_loop_lifecycle.py
new file mode 100644
index 0000000..e6593af
--- /dev/null
+++ b/tests/test_client_loop_lifecycle.py
@@ -0,0 +1,45 @@
+# -*- coding: utf-8 -*-
+"""Owned event-loop threads must be stopped and closed with the client."""
+
+from hy_memory.client import HyMemoryClient, _LoopThread
+
+
+def test_loop_thread_stop_closes_the_loop_and_is_idempotent():
+ loop_thread = _LoopThread()
+ loop = loop_thread._loop
+
+ loop_thread.stop()
+ assert not loop_thread._thread.is_alive()
+ assert loop.is_closed()
+
+ # A second stop must not touch a closed loop.
+ loop_thread.stop()
+
+
+def test_close_stops_both_loops_owned_by_a_solo_client():
+ client = HyMemoryClient.__new__(HyMemoryClient)
+ client._registry = None
+ client._owns_runtime = True
+ client._loop_thread = _LoopThread()
+ client._search_loop_thread = _LoopThread()
+ loops = [client._loop_thread._loop, client._search_loop_thread._loop]
+
+ client.close()
+
+ assert all(loop.is_closed() for loop in loops)
+
+
+def test_shared_runtime_loops_are_left_to_the_runtime():
+ client = HyMemoryClient.__new__(HyMemoryClient)
+ client._registry = None
+ client._owns_runtime = False
+ client._loop_thread = _LoopThread()
+ client._search_loop_thread = _LoopThread()
+ loops = [client._loop_thread._loop, client._search_loop_thread._loop]
+
+ try:
+ client.close()
+ assert all(not loop.is_closed() for loop in loops)
+ finally:
+ client._loop_thread.stop()
+ client._search_loop_thread.stop()