agent-zero icon indicating copy to clipboard operation
agent-zero copied to clipboard

Browser runtime not cleaned up on abandoned chats: Chromium idle-burns ~0.8 cores for days (cleanup hooks only fire on reset/remove)

Open nico7master opened this issue 3 weeks ago • 1 comments

Summary

A browser surface (Chromium under Playwright/Patchright + Xvfb) opened for a chat kept running for 2 days 11 hours after its chat was abandoned, with the Chromium GPU process sustained at ~78–83% of one core on an idle page (~46 CPU-hours). /a0/tmp/browser/displays/ had accumulated 28 stale browser-* dirs back to Aug 25 — a slow leak across chats. Cleanup required a manual kill; host load average fell from 3.18 to ~1.5 within minutes of killing the tree.

Cleanup hooks do exist, but they only fire on context reset/remove — so an abandoned chat (switched away from, or left idle) never triggers them, and nothing reaps the runtime until the container dies or the same chat is reset/removed.

Reproduction

  1. In a chat, let the browser tool open a real surface (creates tmp/browser/displays/<token>/, Xvfb, Chromium).
  2. Abandon the chat — switch to another chat, or leave it idle for days — without a context reset/remove.
  3. Wait days. Observe: Chromium/Xvfb/xpra trees still alive; GPU process %CPU sustained high while all sibling chrome processes idle (crashpad 0:00, network utility 0:33, main browser parent 1:29 over 2.5 days).
  4. ls /a0/tmp/browser/displays/ — stale dirs accumulate; nothing sweeps them.

Verified evidence (single instance)

# GPU process — idle page, busy core (before manual kill)
PID 227420  STARTED Nov 9 07:15:16  ELAPSED 2-10:54:32  CPU-TIME 1-22:08:36  %CPU 78.3

# siblings of same tree over the same 2.5 days — all idle
chrome (browser parent)   0:01:29
chrome (network utility)  0:00:33
chrome (utility)          0:00:05
chrome (renderer)         1:30:08   ← accumulated during earlier actual use
chrome_crashpad ×2        0:00:00

# after manual kill
load avg 3.18 → ~1.5 within minutes

Root cause (verified in source, commit ba9b67df)

Cleanup machinery exists, but its triggers never fire for an abandoned chat:

  1. Cleanup hooks only fire on context reset/remove. plugins/_browser/extensions/python/_functions/agent/AgentContext/reset/start/_10_cleanup_browser_runtime.py and the sibling .../remove/start/... hook call close_runtime_sync(context_id) → close_runtime (plugins/_browser/helpers/runtime.py:3222,3236) → close_context. Abandoning a chat is neither a reset nor a remove. Switching to another chat or leaving a chat idle keeps _runtimes[context_id] registered (runtime.py module cache) and Chromium alive. Our leaked tree survived 2.5 days across many WebUI reconnects, so the webui_ws_disconnect hook doesn't cover this case either.
  2. The orphan-sweep is lazy, not periodic. _release_orphaned_profile_singleton (runtime.py:1270) kills an orphaned Chromium only when the same profile is opened again (open() → ensure_started()). No periodic sweep of registered-but-dead chats exists.
  3. Display dirs are never reaped. plugins/_browser/helpers/interactive_view.py:61 allocates tmp/browser/displays/<token>; nothing removes entries whose backing processes are gone.
  4. Idle GPU busy-loop amplifies cost. On the Xvfb/software-GL stack the Chromium GPU process busy-loops (~0.8 cores) even on an idle page, so every leak costs real CPU continuously, not just memory.

Suggested fixes

  1. Reap on chat switching. Fire close_runtime(old_context_id) (or an LRU cap) when the WebUI switches the active chat, so the outgoing chat's runtime closes like reset/remove.
  2. Periodic reaper. Sweep _runtimes/display dirs: close runtimes with no backing live chat for N hours, and remove tmp/browser/displays/browser-* dirs with no live backing processes.
  3. Investigate the idle GPU busy-loop for Xvfb-backed Chromium (--disable-gpu / SwiftShader test) so even a leaked surface costs memory only, not ~0.8 cores.

Impact

Each leaked surface holds ~170 MB RSS and burns ~0.8 cores indefinitely. The 28 stale dirs over ~2.5 months show it accumulates silently on any instance where chats get abandoned (very common). A handful of leaks can saturate a small self-hosted server's cores.

Environment

  • Agent Zero: dockerized, commit ba9b67df (Nov 9 image), self-hosted Linux host
  • Chromium: bundled Playwright build /a0/tmp/playwright/chromium-1228
  • Per leaked surface: Xvfb + Patchright driver + Chromium tree

nico7master avatar Nov 12 '26 06:11 nico7master

Hi @nico7master Confirmed the lifecycle gap in the current code. The internal browser is shared across chats, so closing it on chat switching could interrupt another agent’s work.

A fix is prepared and tested locally, I will push this on ready branch and then merge to main for the v2.16 release, shortly:

  • One-hour idle timeout, with protection for running and paused agents, including subordinate work.
  • Active viewers, browser calls, and downloads keep the runtime alive.
  • Manual Stop in Browser Settings and cleanup on global plugin disable.
  • Passive status polling no longer restores a stopped browser.

I haven’t reproduced the GPU busy-loop, so this addresses the lifecycle issue, but for this reason I'm keeping this issue open until the change lands in v2.16 and can be verified end to end. Related report: #1892. I've just closed that issue as duplicate of this one.

Thanks again, Alessandro

3clyp50 avatar Nov 12 '26 19:11 3clyp50