fix: close_browser kills orphaned CDP processes after agent restart

When the agent restarts, _browser_process is lost but the CDP Edge
instance keeps running. Now close_browser also searches for processes
with 'easybot_cdp_profile' in their command line.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
David Piccinini 2026-05-17 04:09:23 -03:00
parent 64f79d4fa8
commit 8b9b059783

View File

@ -623,11 +623,29 @@ async def tool_open_browser(args: dict[str, Any]) -> dict[str, Any]:
async def tool_close_browser(args: dict[str, Any]) -> dict[str, Any]: async def tool_close_browser(args: dict[str, Any]) -> dict[str, Any]:
"""Close the CDP browser.""" """Close the CDP browser."""
global _browser_process global _browser_process
killed = False
# Try tracked process first
if _browser_process and _browser_process.poll() is None: if _browser_process and _browser_process.poll() is None:
_browser_process.terminate() _browser_process.terminate()
_browser_process = None _browser_process = None
return {"success": True} killed = True
return {"success": True, "message": "No browser was running"} # Also kill any orphaned CDP browser by profile dir (survives agent restarts)
import tempfile
cdp_profile = os.path.join(tempfile.gettempdir(), "easybot_cdp_profile")
try:
import subprocess as _sp
result = _sp.run(
["powershell", "-Command",
f"Get-Process msedge,chrome -ErrorAction SilentlyContinue | "
f"Where-Object {{$_.CommandLine -like '*easybot_cdp_profile*'}} | "
f"Stop-Process -Force"],
capture_output=True, timeout=5,
)
if result.returncode == 0:
killed = True
except Exception:
pass
return {"success": True} if killed else {"success": True, "message": "No browser was running"}
# ── Tool Registry ──────────────────────────────────────────────────────────── # ── Tool Registry ────────────────────────────────────────────────────────────