Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

fix os.kill(pid, 0) on Windows: use OpenProcess instead of TerminateProcess

ShawnPanaf29aa6b5bc10
diff --git a/browser_use/skill_cli/main.py b/browser_use/skill_cli/main.py
index 85dd038e1..fccec5c38 100755
--- a/browser_use/skill_cli/main.py
+++ b/browser_use/skill_cli/main.py
@@ -700,10 +700,24 @@ def _handle_sessions(args: argparse.Namespace) -> int:
pid_file.unlink(missing_ok=True)
continue
- # Check if process is alive
- try:
- os.kill(pid, 0)
- except (OSError, ProcessLookupError):
+ # Check if process is alive (os.kill(pid, 0) terminates on Windows, use OpenProcess instead)
+ 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)
+ _alive = True
+ else:
+ _alive = False
+ else:
+ try:
+ os.kill(pid, 0)
+ _alive = True
+ except (OSError, ProcessLookupError):
+ _alive = False
+ if not _alive:
# Dead — clean up stale files
pid_file.unlink(missing_ok=True)
sock_path = _get_socket_path(name)
diff --git a/browser_use/skill_cli/tunnel.py b/browser_use/skill_cli/tunnel.py
index a4cba3a2f..fd9af89cb 100644
--- a/browser_use/skill_cli/tunnel.py
+++ b/browser_use/skill_cli/tunnel.py
@@ -150,11 +150,9 @@ def _delete_tunnel_info(port: int) -> None:
def _is_process_alive(pid: int) -> bool:
"""Check if a process is still running."""
- try:
- os.kill(pid, 0)
- return True
- except (OSError, ProcessLookupError):
- return False
+ from browser_use.skill_cli.utils import is_process_alive
+
+ return is_process_alive(pid)
def _kill_process(pid: int) -> bool:
diff --git a/browser_use/skill_cli/utils.py b/browser_use/skill_cli/utils.py
index 079206b49..f72286edf 100644
--- a/browser_use/skill_cli/utils.py
+++ b/browser_use/skill_cli/utils.py
@@ -11,6 +11,29 @@ import zlib
from pathlib import Path
+def is_process_alive(pid: int) -> bool:
+ """Check if a process is still running.
+
+ On Windows, os.kill(pid, 0) calls TerminateProcess — so we use
+ OpenProcess via ctypes instead.
+ """
+ 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, ProcessLookupError):
+ return False
+
+
def validate_session_name(session: str) -> None:
"""Validate session name — reject path traversal and special characters.
@@ -112,9 +135,7 @@ def list_sessions() -> list[dict]:
continue
# Check if process is alive
- try:
- os.kill(pid, 0)
- except (OSError, ProcessLookupError):
+ if not is_process_alive(pid):
# Dead process — clean up stale files
pid_file.unlink(missing_ok=True)
sock_path = get_socket_path(session_name)