From 8b9b0597832ba020efdf117936e49a6c3d213992 Mon Sep 17 00:00:00 2001 From: David Piccinini Date: Sun, 17 May 2026 04:09:23 -0300 Subject: [PATCH] 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 --- easybot_windows_agent/main.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/easybot_windows_agent/main.py b/easybot_windows_agent/main.py index 91f16bc..6ec45cb 100644 --- a/easybot_windows_agent/main.py +++ b/easybot_windows_agent/main.py @@ -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]: """Close the CDP browser.""" global _browser_process + killed = False + # Try tracked process first if _browser_process and _browser_process.poll() is None: _browser_process.terminate() _browser_process = None - return {"success": True} - return {"success": True, "message": "No browser was running"} + killed = True + # 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 ────────────────────────────────────────────────────────────