mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-11-27 17:00:37 +00:00
fix(opensandbox): keep stale callbacks from destroying replacement sandboxes (#8507)
* chore: initialize OpenSandbox callback fix draft * fix(opensandbox): keep stale callbacks from destroying replacement sandboxes --------- Co-authored-by: Leeminjing <247553113+Leeminjing@users.noreply.github.com>
This commit is contained in:
parent
e212596a02
commit
6cefec0c9d
@ -291,15 +291,19 @@ class OpenSandboxProvider(WarmPoolLifecycleMixin[OpenSandboxSandbox], SandboxPro
|
||||
except Exception as exc:
|
||||
logger.warning("Error destroying OpenSandbox %s (%s): %s", sandbox.id, context, exc)
|
||||
|
||||
def _invalidate_sandbox(self, sandbox_id: str, reason: str) -> None:
|
||||
def _invalidate_sandbox(self, sandbox: OpenSandboxSandbox, reason: str) -> None:
|
||||
sandbox_id = sandbox.id
|
||||
with self._lock:
|
||||
active = self._sandboxes.pop(sandbox_id, None)
|
||||
warm_entry = self._warm_pool.pop(sandbox_id, None)
|
||||
active = self._sandboxes.get(sandbox_id)
|
||||
warm_entry = self._warm_pool.get(sandbox_id)
|
||||
if active is sandbox:
|
||||
self._sandboxes.pop(sandbox_id)
|
||||
elif warm_entry is not None and warm_entry[0] is sandbox:
|
||||
self._warm_pool.pop(sandbox_id)
|
||||
else:
|
||||
return
|
||||
for key in [key for key, value in self._thread_sandboxes.items() if value == sandbox_id]:
|
||||
self._thread_sandboxes.pop(key, None)
|
||||
sandbox = active or (warm_entry[0] if warm_entry is not None else None)
|
||||
if sandbox is None:
|
||||
return
|
||||
logger.warning("Invalidating OpenSandbox %s after terminal failure: %s", sandbox_id, reason)
|
||||
self._destroy_quietly(sandbox, context="terminal failure")
|
||||
|
||||
@ -429,15 +433,16 @@ class OpenSandboxProvider(WarmPoolLifecycleMixin[OpenSandboxSandbox], SandboxPro
|
||||
logger.warning("Failed to destroy OpenSandbox %s after bootstrap failure", remote.id, exc_info=True)
|
||||
raise
|
||||
|
||||
return OpenSandboxSandbox(
|
||||
sandbox = OpenSandboxSandbox(
|
||||
sandbox_id,
|
||||
remote,
|
||||
run_command_opts_cls=run_command_opts_cls,
|
||||
default_env=self._config["environment"],
|
||||
sandbox_timeout=None if self._config["sandbox_timeout"] is None else timedelta(seconds=self._config["sandbox_timeout"]),
|
||||
default_command_timeout=self._config["command_timeout"],
|
||||
on_terminal_failure=self._invalidate_sandbox,
|
||||
on_terminal_failure=lambda _sandbox_id, reason: self._invalidate_sandbox(sandbox, reason),
|
||||
)
|
||||
return sandbox
|
||||
|
||||
def _reclaim_warm_pool(self, sandbox_id: str) -> OpenSandboxSandbox | None:
|
||||
with self._lock:
|
||||
@ -507,7 +512,7 @@ class OpenSandboxProvider(WarmPoolLifecycleMixin[OpenSandboxSandbox], SandboxPro
|
||||
self._park_failed_skill_upload(sandbox, was_active=True)
|
||||
raise
|
||||
except BaseException:
|
||||
self._invalidate_sandbox(sandbox_id, "skill projection setup failed")
|
||||
self._invalidate_sandbox(sandbox, "skill projection setup failed")
|
||||
raise
|
||||
|
||||
def _replace_skill_projection(self, sandbox: OpenSandboxSandbox, projection: SkillProjectionPaths | None) -> None:
|
||||
|
||||
@ -1786,6 +1786,90 @@ def test_terminal_error_evicts_active_sandbox(monkeypatch: pytest.MonkeyPatch) -
|
||||
provider.shutdown()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("warm", [False, True], ids=["active", "warm"])
|
||||
def test_late_terminal_failure_cannot_evict_replacement(monkeypatch: pytest.MonkeyPatch, tmp_path: Path, warm: bool) -> None:
|
||||
paths = Paths(base_dir=tmp_path)
|
||||
monkeypatch.setattr("deerflow.config.paths.get_paths", lambda: paths)
|
||||
provider, sdk = _install(monkeypatch)
|
||||
try:
|
||||
sandbox_id = provider.acquire("thread-1", user_id="user-1")
|
||||
old = provider.get(sandbox_id)
|
||||
assert isinstance(old, OpenSandboxSandbox)
|
||||
notify = old._on_terminal_failure
|
||||
assert notify is not None
|
||||
notification_started = threading.Event()
|
||||
resume_notification = threading.Event()
|
||||
notification_delivered = threading.Event()
|
||||
|
||||
def delay_notification(failed_id: str, reason: str) -> None:
|
||||
# The adapter has released its operation lock when it calls us.
|
||||
delayed = threading.current_thread() is worker
|
||||
if delayed:
|
||||
notification_started.set()
|
||||
if not resume_notification.wait(10):
|
||||
return
|
||||
notify(failed_id, reason)
|
||||
if delayed:
|
||||
notification_delivered.set()
|
||||
|
||||
monkeypatch.setattr(old, "_on_terminal_failure", delay_notification)
|
||||
old_remote = sdk.remotes[0]
|
||||
old_remote.command_error = _TerminalApiError("old command endpoint is gone", status_code=404)
|
||||
results: list[str] = []
|
||||
worker = threading.Thread(target=lambda: results.append(old.execute_command("true")))
|
||||
worker.start()
|
||||
try:
|
||||
assert notification_started.wait(10)
|
||||
old_remote.renew_error = _TerminalApiError("old sandbox expired", status_code=410)
|
||||
assert provider.acquire("thread-1", user_id="user-1") == sandbox_id
|
||||
replacement = provider.get(sandbox_id)
|
||||
assert isinstance(replacement, OpenSandboxSandbox)
|
||||
assert replacement.id == old.id == sandbox_id
|
||||
assert replacement.remote_id != old.remote_id
|
||||
if warm:
|
||||
provider.release(sandbox_id)
|
||||
assert provider.get(sandbox_id) is None
|
||||
finally:
|
||||
resume_notification.set()
|
||||
worker.join(10)
|
||||
|
||||
assert not worker.is_alive()
|
||||
assert notification_delivered.is_set()
|
||||
assert results == ["Error: old command endpoint is gone"]
|
||||
assert [remote.destroy_calls for remote in sdk.remotes] == [1, 0]
|
||||
if not warm:
|
||||
assert provider.get(sandbox_id) is replacement
|
||||
assert replacement.execute_command("true") == "(no output)"
|
||||
assert provider.acquire("thread-1", user_id="user-1") == sandbox_id
|
||||
assert provider.get(sandbox_id) is replacement
|
||||
assert len(sdk.create_calls) == 2
|
||||
finally:
|
||||
provider.shutdown()
|
||||
|
||||
|
||||
def test_terminal_error_evicts_current_warm_sandbox(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
|
||||
paths = Paths(base_dir=tmp_path)
|
||||
monkeypatch.setattr("deerflow.config.paths.get_paths", lambda: paths)
|
||||
provider, sdk = _install(monkeypatch)
|
||||
try:
|
||||
sandbox_id = provider.acquire("thread-1", user_id="user-1")
|
||||
box = provider.get(sandbox_id)
|
||||
assert box is not None
|
||||
provider.release(sandbox_id)
|
||||
assert sandbox_id in provider._warm_pool
|
||||
assert sdk.remotes[0].destroy_calls == 0
|
||||
|
||||
sdk.remotes[0].command_error = _TerminalApiError("warm sandbox is gone")
|
||||
assert box.execute_command("true") == "Error: warm sandbox is gone"
|
||||
assert sandbox_id not in provider._warm_pool
|
||||
assert sdk.remotes[0].destroy_calls == 1
|
||||
assert provider.acquire("thread-1", user_id="user-1") == sandbox_id
|
||||
assert provider.get(sandbox_id) is not box
|
||||
assert len(sdk.create_calls) == 2
|
||||
finally:
|
||||
provider.shutdown()
|
||||
|
||||
|
||||
def test_concurrent_same_scope_acquire_creates_once(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
provider, sdk = _install(monkeypatch)
|
||||
original_create = sdk.create
|
||||
|
||||
@ -98,6 +98,8 @@ same-size content edits are still copied on release.
|
||||
Outputs with case- or Unicode-equivalent file or directory names are skipped
|
||||
before copying, and aliases of existing local paths are rejected, preserving
|
||||
previous complete copies.
|
||||
Rebuilding an OpenSandbox for the same user and thread can reuse its logical ID,
|
||||
and terminal failures affect only the instance that reported them.
|
||||
|
||||
OpenSandbox also uploads enabled skills to `skills.container_path`. An explicit
|
||||
Agent skill list replaces the managed skill directories before tools run; an
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user