deer-flow/backend/tests/blocking_io/test_aio_sandbox_get.py
Zeren Wang bb75f8d736
feat(sandbox): share sandbox identity derivation and acquire serialization (#4741) (#5089)
* feat(sandbox): share sandbox identity derivation and acquire serialization (#4741)

Remote providers (AIO, E2B, BoxLite, Tenki, OpenSandbox) each inlined the
same sha256(user:thread)[:16] sandbox-id expression and kept per-scope lock
dicts that grew unboundedly until shutdown. This extracts both mechanisms
into shared components without changing provider lifecycle, ids, capacity
semantics, or public tool behavior:

- sandbox/identity.py: keyword-only derive_sandbox_scope_token (byte-pinned
  compatibility contract) + is_sandbox_scope_token; per-provider golden
  vectors pin current behavior including BoxLite's raw-None quirk and each
  provider's private user_id resolution.
- sandbox/acquire_serialization.py: AcquireSerializer — per-key lock table
  with holder/waiter refcount reclamation, bounded dedicated executor
  (async waits off both the event loop and the default executor),
  worker-owned cancellation cleanup (no event-loop callback dependency), idempotent close().
- Each provider adopts both components; AIO/E2B key by (user_id, thread_id)
  with acquire and (E2B) release serialized; BoxLite/Tenki/OpenSandbox key
  by derived sandbox id and offload the whole sync acquire to the
  serializer's executor so a cancelled awaiter cannot overlap a retried
  same-scope body (leaked-remote-VM regression caught in review).
- thread_id=None acquires stay unserialized; provider shutdown()/reset()
  close the serializer; E2B capacity/ledger/reconciliation and AIO
  ownership/flock machinery untouched.
- blocking-IO anchor proves contended OpenSandbox acquire_async stays off
  the event loop (teeth verified red/green); AGENTS.md documents the
  shared components.

* refactor(sandbox): address review on acquire serialization (#5089)

- Replace unreachable checkin branch with an assertion: run() returns
  False only after abandon(), which the except handler always re-raises;
  the old _checkin would have double-decremented the refcount.
- Document the task.cancelling() == 0 assumption in hold_async.
- Drop unused thread_id/user_id kwargs from BoxLite and Tenki
  _acquire_scope_locked (OpenSandbox still forwards them).

* fix(sandbox): preserve request ContextVars in acquire executor bridge (#5089)

loop.run_in_executor() does not copy contextvars, unlike the inherited
SandboxProvider.acquire_async() which used asyncio.to_thread(). The
BoxLite/OpenSandbox/Tenki acquire_async bridges introduced in this PR
therefore dropped the request trace id (logged as trace_id=-).

Add AcquireSerializer.run_on_executor(), which copies the calling
context and runs the callable through ctx.run, and route all three
providers through it. Add regression tests binding request_trace_context
and verifying the worker thread observes it.
2026-08-30 10:30:34 +08:00

159 lines
5.9 KiB
Python

"""Regression: ``AioSandboxProvider.get()`` must not do blocking IO.
``ensure_sandbox_initialized_async`` (``sandbox/tools.py``) calls
``provider.get()`` directly on the LangGraph event loop for every sandbox tool
lookup. A prior change renewed the cross-process lease inside ``get()``
(``mkdir`` + temp-file write + ``fsync`` + ``os.replace``), which blocks the loop
— reported on PR #4221.
Under the strict Blockbuster context (this directory's conftest), any blocking IO
reached from ``deerflow.*`` while on the event loop raises ``BlockingError``.
The ownership store is injected here as a **blocking probe**: every store method
does real file IO. That keeps the anchor honest across backends — the configured
store may be in-memory (no IO to catch), but the redis store does network IO and a
future store could do anything, so what must be pinned is that ``get()`` performs
*no store call at all*, not merely that today's default store happens to be cheap.
If ownership work is put back on this path, this test fails.
"""
from __future__ import annotations
import threading
from pathlib import Path
from unittest.mock import MagicMock
import pytest
pytestmark = pytest.mark.asyncio
class _BlockingProbeStore:
"""Ownership store whose every operation does real blocking file IO."""
supports_cross_process = True
def __init__(self, probe_path: Path):
self._probe_path = probe_path
self._probe_path.write_text("owner", encoding="utf-8")
@property
def owner_id(self) -> str:
return "worker-blockingio"
def _blocking_touch(self) -> str:
# Mirrors what a real store does on this call: sync IO the strict gate sees.
return self._probe_path.read_text(encoding="utf-8")
def take(self, sandbox_id: str) -> bool:
self._blocking_touch()
return True
def claim(self, sandbox_id: str, *, for_destroy: bool = False) -> bool:
self._blocking_touch()
return True
def renew(self, sandbox_id: str):
from deerflow.community.aio_sandbox.ownership import RenewOutcome
self._blocking_touch()
return RenewOutcome.RENEWED
def release(self, sandbox_id: str) -> None:
self._blocking_touch()
def owner(self, sandbox_id: str) -> str | None:
return self._blocking_touch()
def close(self) -> None:
pass
def _make_provider(tmp_path: Path):
"""Build an ``AioSandboxProvider`` without ``__init__`` (no Docker, no threads)."""
from deerflow.community.aio_sandbox.aio_sandbox_provider import AioSandboxProvider
from deerflow.config.sandbox_config import SandboxOwnershipConfig
from deerflow.sandbox.acquire_serialization import AcquireSerializer
provider = AioSandboxProvider.__new__(AioSandboxProvider)
provider._lock = threading.Lock()
provider._sandboxes = {}
provider._sandbox_infos = {}
provider._thread_sandboxes = {}
provider._acquire_serializer = AcquireSerializer(thread_name_prefix="aio-sandbox-lock-wait")
provider._last_activity = {}
provider._warm_pool = {}
provider._active_sandbox_identity = {}
provider._warm_pool_identity = {}
provider._local_teardown = set()
provider._acquire_epoch = {}
provider._acquire_epoch_counter = 0
provider._acquire_inflight = {}
provider._shutdown_called = False
provider._idle_checker_stop = threading.Event()
provider._idle_checker_thread = None
provider._renewal_stop = threading.Event()
provider._renewal_thread = None
provider._config = {"idle_timeout": 600, "replicas": 3}
provider._backend = MagicMock()
provider._owner_id = "worker-blockingio"
provider._ownership_config = SandboxOwnershipConfig()
provider._ownership = _BlockingProbeStore(tmp_path / "ownership-probe")
return provider
async def test_get_does_no_blocking_io_on_event_loop(tmp_path):
provider = _make_provider(tmp_path)
provider._sandboxes["sb-blockingio"] = MagicMock()
# If get() touches the ownership store, the probe's file read trips the gate.
assert provider.get("sb-blockingio") is not None
async def test_blocking_probe_store_actually_trips_the_gate(tmp_path):
"""Meta-check: prove the probe has teeth, so the test above is not vacuous.
Without this, a store that silently stopped doing IO would make the anchor
pass for the wrong reason.
"""
from blockbuster import BlockingError
provider = _make_provider(tmp_path)
with pytest.raises(BlockingError):
provider._publish_ownership("sb-blockingio")
async def test_async_acquire_offloads_ownership_publish(tmp_path, monkeypatch):
"""The async acquire paths must offload registration, not just discovery.
``_register_discovered_sandbox`` / ``_register_created_sandbox`` publish
ownership, which is blocking store IO. Every other blocking step in
``_discover_or_create_with_lock_async`` is wrapped in ``asyncio.to_thread``;
these two were called directly, putting a Redis round trip on the event loop
for every discover/create.
"""
import deerflow.community.aio_sandbox.aio_sandbox_provider as aio_mod
from deerflow.community.aio_sandbox.sandbox_info import SandboxInfo
provider = _make_provider(tmp_path)
info = SandboxInfo(
sandbox_id="sb-async",
sandbox_url="http://localhost:8080",
container_name="deer-flow-sandbox-sb-async",
created_at=1.0,
)
provider._backend.discover = MagicMock(return_value=info)
# Stub the path layer: `get_paths()` resolves the base dir via os.getcwd on
# the event loop, which is a pre-existing blocking call in this coroutine and
# not what this anchor is about. Scoping it out keeps the test pinned to the
# ownership publish this diff added.
fake_paths = MagicMock()
fake_paths.thread_dir.return_value = tmp_path
monkeypatch.setattr(aio_mod, "get_paths", lambda: fake_paths)
sandbox_id = await provider._discover_or_create_with_lock_async("t-async", "sb-async", user_id="u1")
assert sandbox_id == "sb-async"