fix: use separate user-data-dir for CDP browser to avoid conflicts with existing instances

When Edge/Chrome is already running, a new instance with --remote-debugging-port
joins the existing process instead of starting CDP. Using a dedicated profile dir
forces a separate process. Also retry CDP port check up to 6 seconds.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
David Piccinini 2026-05-17 03:59:49 -03:00
parent f7acffd159
commit 64f79d4fa8

View File

@ -577,14 +577,29 @@ async def tool_open_browser(args: dict[str, Any]) -> dict[str, Any]:
_browser_process.terminate()
await asyncio.sleep(0.5)
# Use a separate user-data-dir to avoid conflicts with existing browser instances
import tempfile
cdp_profile = os.path.join(tempfile.gettempdir(), "easybot_cdp_profile")
os.makedirs(cdp_profile, exist_ok=True)
_browser_process = subprocess.Popen(
[browser_exe, f"--remote-debugging-port={port}",
"--no-first-run", "--no-default-browser-check", url],
f"--user-data-dir={cdp_profile}",
"--no-first-run", "--no-default-browser-check",
"--disable-extensions", "--disable-popup-blocking", url],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
creationflags=subprocess.CREATE_NO_WINDOW if sys.platform == "win32" else 0,
)
# Wait a bit for CDP to be ready
await asyncio.sleep(2)
# Wait for CDP to be ready (may take longer on first launch)
for _ in range(6):
await asyncio.sleep(1)
try:
reader, writer = await asyncio.open_connection("127.0.0.1", port)
writer.close()
await writer.wait_closed()
break
except OSError:
continue
# Verify CDP is listening
try: