Skip to content

Agent Zero AI framework

Preserve the shared browser cleanup thread across context resets

Alessandro30dc0e2d10f0
diff --git a/plugins/_browser/AGENTS.md b/plugins/_browser/AGENTS.md
index d84790343..071bc3a00 100644
--- a/plugins/_browser/AGENTS.md
+++ b/plugins/_browser/AGENTS.md
@@ -28,6 +28,7 @@
- Notify the active Xpra client of its new frame geometry before resizing the backing display; after an interactive canvas/modal handoff, reconcile once after Xpra's deferred resize so Chromium cannot retain the previous surface size.
- Present the Xpra shadow window as the raw browser canvas: remove its HTML decoration and shadow pointer while preserving exact viewport geometry.
- Keep one internal Chromium, Xvfb, and Xpra runtime per Agent Zero process with one unguessable gateway token.
+- Synchronous context cleanup shares the named BrowserCleanup event loop. Cancel only the cleanup task on completion/timeout; an individual context must not terminate the shared thread used by concurrent resets.
- Bind Browser Xpra endpoints to loopback, route them through the authenticated virtual-desktop gateway, and keep file transfer, URL opening, printing, and audio disabled.
- Paint live screencast frames through the Browser panel canvas/ImageBitmap path when available; keep the `<img>`/data URL path for snapshots and fallback rendering.
- Push internal screencast frames from the runtime to the WebSocket consumer after subscription; keep `read/pop_screencast_frame` as fallback/tooling APIs, not the WebUI hot path.
diff --git a/plugins/_browser/helpers/runtime.py b/plugins/_browser/helpers/runtime.py
index 0911eb441..a00f1622d 100644
--- a/plugins/_browser/helpers/runtime.py
+++ b/plugins/_browser/helpers/runtime.py
@@ -3066,7 +3066,7 @@ def close_runtime_sync(context_id: str, *, delete_profile: bool = True) -> None:
try:
task.result_sync(timeout=30)
finally:
- task.kill(terminate_thread=True)
+ task.kill()
async def close_all_runtimes(*, delete_profiles: bool = False) -> None:
diff --git a/tests/test_browser_agent_regressions.py b/tests/test_browser_agent_regressions.py
index 7425005f0..204cf8673 100644
--- a/tests/test_browser_agent_regressions.py
+++ b/tests/test_browser_agent_regressions.py
@@ -4930,3 +4930,32 @@ def test_legacy_browser_dependency_is_removed():
assert ("browser" + "-use") not in (PROJECT_ROOT / "requirements.txt").read_text(
encoding="utf-8"
)
+
+
[email protected]("timeout", [False, True])
+def test_context_cleanup_preserves_shared_event_loop(monkeypatch, timeout):
+ calls = []
+
+ class CleanupTask:
+ def __init__(self, thread_name):
+ calls.append(("thread", thread_name))
+
+ def start_task(self, fn, context_id, **kwargs):
+ calls.append(("close", context_id))
+
+ def result_sync(self, timeout):
+ if should_timeout:
+ raise TimeoutError("cleanup timed out")
+
+ def kill(self, terminate_thread=False):
+ assert not terminate_thread, "Other context cleanup tasks share this thread"
+ calls.append(("cancel", None))
+
+ should_timeout = timeout
+ monkeypatch.setattr(browser_runtime_module, "DeferredTask", CleanupTask)
+ if timeout:
+ with pytest.raises(TimeoutError):
+ browser_runtime_module.close_runtime_sync("first")
+ else:
+ browser_runtime_module.close_runtime_sync("first")
+ assert calls == [("thread", "BrowserCleanup"), ("close", "first"), ("cancel", None)]