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(cli): Windows compatibility fixes for session server
ShawnPana5e811e2ba4db
diff --git a/browser_use/skill_cli/main.py b/browser_use/skill_cli/main.pyindex 88f807fdd..96da3f7b5 100755--- a/browser_use/skill_cli/main.py+++ b/browser_use/skill_cli/main.py@@ -139,8 +139,9 @@ if '--template' in sys.argv:def get_socket_path(session: str) -> str:"""Get socket path for session."""if sys.platform == 'win32':+ # Use 127.0.0.1 explicitly (not localhost) to avoid IPv6 binding issuesport = 49152 + (int(hashlib.md5(session.encode()).hexdigest()[:4], 16) % 16383)- return f'tcp://localhost:{port}'+ return f'tcp://127.0.0.1:{port}'return str(Path(tempfile.gettempdir()) / f'browser-use-{session}.sock')@@ -149,6 +150,29 @@ def get_pid_path(session: str) -> Path:return Path(tempfile.gettempdir()) / f'browser-use-{session}.pid'+def _pid_exists(pid: int) -> bool:+ """Check if a process with given PID exists.++ On Windows, uses ctypes to call OpenProcess (os.kill doesn't work reliably).+ On Unix, uses os.kill(pid, 0) which is the standard approach.+ """+ if sys.platform == 'win32':+ import ctypes++ PROCESS_QUERY_LIMITED_INFORMATION = 0x1000+ handle = ctypes.windll.kernel32.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, pid)+ if handle:+ ctypes.windll.kernel32.CloseHandle(handle)+ return True+ return False+ else:+ try:+ os.kill(pid, 0)+ return True+ except OSError:+ return False++def is_server_running(session: str) -> bool:"""Check if server is running for session."""pid_path = get_pid_path(session)@@ -156,9 +180,9 @@ def is_server_running(session: str) -> bool:return Falsetry:pid = int(pid_path.read_text().strip())- os.kill(pid, 0)- return True+ return _pid_exists(pid)except (OSError, ValueError):+ # Can't read PID file or invalid PIDreturn Falsediff --git a/browser_use/skill_cli/server.py b/browser_use/skill_cli/server.pyindex f28024d52..cdde2dca5 100644--- a/browser_use/skill_cli/server.py+++ b/browser_use/skill_cli/server.py@@ -207,6 +207,7 @@ class SessionServer:self.handle_connection,host,int(port),+ reuse_address=True, # Allow rebinding ports in TIME_WAIT state)logger.info(f'Listening on TCP {host}:{port}')else:diff --git a/browser_use/skill_cli/utils.py b/browser_use/skill_cli/utils.pyindex bfc45ba64..e20338b1e 100644--- a/browser_use/skill_cli/utils.py+++ b/browser_use/skill_cli/utils.py@@ -12,13 +12,14 @@ from pathlib import Pathdef get_socket_path(session: str) -> str:"""Get socket path for session.- On Windows, returns a TCP address (tcp://localhost:PORT).+ On Windows, returns a TCP address (tcp://127.0.0.1:PORT).On Unix, returns a Unix socket path."""if sys.platform == 'win32':# Windows: use TCP on deterministic port (49152-65535)+ # Use 127.0.0.1 explicitly (not localhost) to avoid IPv6 binding issuesport = 49152 + (int(hashlib.md5(session.encode()).hexdigest()[:4], 16) % 16383)- return f'tcp://localhost:{port}'+ return f'tcp://127.0.0.1:{port}'return str(Path(tempfile.gettempdir()) / f'browser-use-{session}.sock')@@ -32,6 +33,29 @@ def get_log_path(session: str) -> Path:return Path(tempfile.gettempdir()) / f'browser-use-{session}.log'+def _pid_exists(pid: int) -> bool:+ """Check if a process with given PID exists.++ On Windows, uses ctypes to call OpenProcess (os.kill doesn't work reliably).+ On Unix, uses os.kill(pid, 0) which is the standard approach.+ """+ if sys.platform == 'win32':+ import ctypes++ PROCESS_QUERY_LIMITED_INFORMATION = 0x1000+ handle = ctypes.windll.kernel32.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, pid)+ if handle:+ ctypes.windll.kernel32.CloseHandle(handle)+ return True+ return False+ else:+ try:+ os.kill(pid, 0)+ return True+ except OSError:+ return False++def is_server_running(session: str) -> bool:"""Check if server is running for session."""pid_path = get_pid_path(session)@@ -39,11 +63,9 @@ def is_server_running(session: str) -> bool:return Falsetry:pid = int(pid_path.read_text().strip())- # Check if process exists- os.kill(pid, 0)- return True+ return _pid_exists(pid)except (OSError, ValueError):- # Process doesn't exist or invalid PID+ # Can't read PID file or invalid PIDreturn False