Skip to content

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

fix(storage): settle AGFS copies before propagating cancellation (#6178)

bot-of-qin-ctxdfc22d1def8c
diff --git a/openviking/pyagfs/async_client.py b/openviking/pyagfs/async_client.py
index 481d30d61..9b2bae15f 100644
--- a/openviking/pyagfs/async_client.py
+++ b/openviking/pyagfs/async_client.py
@@ -92,22 +92,16 @@ class AsyncAGFSClient:
self._client = client
async def run(self, method_name: str, /, *args: Any, **kwargs: Any) -> Any:
- """Run a sync client method in a worker thread, preserving ctx when supported."""
+ """Run a sync method off-thread, settling I/O before propagating cancellation."""
+ method = getattr(self._client, method_name)
try:
- return await run_to_completion(
- lambda: asyncio.to_thread(getattr(self._client, method_name), *args, **kwargs)
- )
+ return await run_to_completion(lambda: asyncio.to_thread(method, *args, **kwargs))
except TypeError as exc:
message = str(exc)
if "ctx" not in kwargs or "unexpected keyword argument 'ctx'" not in message:
raise
- legacy_kwargs = dict(kwargs)
- legacy_kwargs.pop("ctx", None)
- return await run_to_completion(
- lambda: asyncio.to_thread(
- getattr(self._client, method_name), *args, **legacy_kwargs
- )
- )
+ kwargs.pop("ctx")
+ return await run_to_completion(lambda: asyncio.to_thread(method, *args, **kwargs))
async def ls(
self,
@@ -275,15 +269,17 @@ class AsyncAGFSClient:
"""
from .helpers import cp
- return await asyncio.to_thread(
- cp,
- self._client,
- src_path,
- dst_path,
- recursive=recursive,
- stream=True,
- fs_ctx=_fs_ctx_with_auto_pathlock(src_path, fs_ctx, auto_pathlock),
- allow_same_mount_fast_path=allow_same_mount_fast_path,
+ return await run_to_completion(
+ lambda: asyncio.to_thread(
+ cp,
+ self._client,
+ src_path,
+ dst_path,
+ recursive=recursive,
+ stream=True,
+ fs_ctx=_fs_ctx_with_auto_pathlock(src_path, fs_ctx, auto_pathlock),
+ allow_same_mount_fast_path=allow_same_mount_fast_path,
+ )
)
async def grep(self, **kwargs: Any) -> Dict[str, Any]:
diff --git a/tests/agfs/test_async_client.py b/tests/agfs/test_async_client.py
index 9c8b025d4..8a084da57 100644
--- a/tests/agfs/test_async_client.py
+++ b/tests/agfs/test_async_client.py
@@ -11,8 +11,8 @@ from openviking.storage.viking_vector_index_backend import _AsyncVectorAdapter
@pytest.mark.asyncio
[email protected]("backend", ["agfs", "vector"])
-async def test_async_agfs_client_hides_threadpool(backend):
[email protected]("backend", ["agfs", "agfs_cp", "vector"])
+async def test_async_agfs_client_hides_threadpool(backend, monkeypatch):
"""Cancellation settles physical writes before cleanup may remove their data."""
started, release = threading.Event(), threading.Event()
writes = []
@@ -25,17 +25,22 @@ async def test_async_agfs_client_hides_threadpool(backend):
writes.append((path, data))
writer = Writer()
+ monkeypatch.setattr(
+ "openviking.pyagfs.helpers.cp", lambda client, src, dst, **_: client.write(dst, b"data")
+ )
operation = asyncio.create_task(
- AsyncAGFSClient(writer).write("/local/account/file", b"data")
+ AsyncAGFSClient(writer).cp("/local/account/source", "/local/account/file")
+ if backend == "agfs_cp"
+ else AsyncAGFSClient(writer).write("/local/account/file", b"data")
if backend == "agfs"
else _AsyncVectorAdapter(writer).call("write", "record", b"data")
)
try:
assert await asyncio.to_thread(started.wait, 5)
- operation.cancel()
- await asyncio.sleep(0)
- await asyncio.sleep(0)
- assert not operation.done()
+ for _ in range(2):
+ operation.cancel()
+ await asyncio.sleep(0)
+ assert not operation.done()
finally:
release.set()
with pytest.raises(asyncio.CancelledError):