Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
fix socket FD leaks on connect() failure in CLI daemon infrastructure
ShawnPana55665f70b84d
diff --git a/browser_use/skill_cli/main.py b/browser_use/skill_cli/main.pyindex 29dcaa310..8698eb6d2 100755--- a/browser_use/skill_cli/main.py+++ b/browser_use/skill_cli/main.py@@ -178,12 +178,17 @@ def _connect_to_daemon(timeout: float = 60.0, session: str = 'default') -> socke_, hostport = sock_path.split('://', 1)host, port = hostport.split(':')sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)- sock.settimeout(timeout)- sock.connect((host, int(port)))+ addr: str | tuple[str, int] = (host, int(port))else:sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)+ addr = sock_path++ try:sock.settimeout(timeout)- sock.connect(sock_path)+ sock.connect(addr)+ except Exception:+ sock.close()+ raisereturn sock@@ -771,25 +776,27 @@ def _migrate_legacy_files() -> None:# Clean up old single-socket daemon (pre-multi-session)legacy_path = Path(tempfile.gettempdir()) / 'browser-use-cli.sock'if sys.platform == 'win32':+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)try:- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)sock.settimeout(0.5)sock.connect(('127.0.0.1', 49200))req = json.dumps({'id': 'legacy', 'action': 'shutdown', 'params': {}}) + '\n'sock.sendall(req.encode())- sock.close()except OSError:pass+ finally:+ sock.close()elif legacy_path.exists():+ sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)try:- sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)sock.settimeout(0.5)sock.connect(str(legacy_path))req = json.dumps({'id': 'legacy', 'action': 'shutdown', 'params': {}}) + '\n'sock.sendall(req.encode())- sock.close()except OSError:legacy_path.unlink(missing_ok=True)+ finally:+ sock.close()# Clean up old ~/.browser-use/run/ directory (stale PID/socket files)old_run_dir = Path.home() / '.browser-use' / 'run'diff --git a/browser_use/skill_cli/utils.py b/browser_use/skill_cli/utils.pyindex 80c0a1da5..079206b49 100644--- a/browser_use/skill_cli/utils.py+++ b/browser_use/skill_cli/utils.py@@ -64,28 +64,30 @@ def is_daemon_alive(session: str = 'default') -> bool:if sock_path.startswith('tcp://'):_, hostport = sock_path.split('://', 1)host, port_str = hostport.split(':')+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)try:- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.settimeout(0.5)s.connect((host, int(port_str)))- s.close()return Trueexcept OSError:return False+ finally:+ s.close()else:sock_file = Path(sock_path)if not sock_file.exists():return False+ s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)try:- s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)s.settimeout(0.5)s.connect(sock_path)- s.close()return Trueexcept OSError:# Stale socket file — remove itsock_file.unlink(missing_ok=True)return False+ finally:+ s.close()def list_sessions() -> list[dict]:@@ -257,14 +259,15 @@ def discover_chrome_cdp_url() -> str:"""Check if something is listening on 127.0.0.1:{port}."""import socket+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)try:- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.settimeout(1)s.connect(('127.0.0.1', port))- s.close()return Trueexcept OSError:return False+ finally:+ s.close()# --- Phase 1: DevToolsActivePort files ---for data_dir in get_chrome_user_data_dirs():