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): tunnel processes now survive CLI exit
ShawnPana9420c9056902
diff --git a/browser_use/skill_cli/main.py b/browser_use/skill_cli/main.pyindex 746dee39b..990ff9e90 100755--- a/browser_use/skill_cli/main.py+++ b/browser_use/skill_cli/main.py@@ -584,6 +584,7 @@ Examples:help='Port number to tunnel, or subcommand (list, stop)',)tunnel_p.add_argument('port_arg', nargs='?', type=int, help='Port number (for stop subcommand)')+ tunnel_p.add_argument('--all', action='store_true', help='Stop all tunnels (use with: tunnel stop --all)')# -------------------------------------------------------------------------# Session Management@@ -1304,15 +1305,14 @@ def main() -> int:result = tunnel_manager.list_tunnels()elif pos == 'stop':port_arg = getattr(args, 'port_arg', None)- if port_arg is None:- # Check if it's 'stop --all'- if '--all' in sys.argv:- result = asyncio.get_event_loop().run_until_complete(tunnel_manager.stop_all_tunnels())- else:- print('Usage: browser-use tunnel stop <port>', file=sys.stderr)- return 1- else:+ if getattr(args, 'all', False):+ # stop --all+ result = asyncio.get_event_loop().run_until_complete(tunnel_manager.stop_all_tunnels())+ elif port_arg is not None:result = asyncio.get_event_loop().run_until_complete(tunnel_manager.stop_tunnel(port_arg))+ else:+ print('Usage: browser-use tunnel stop <port> | --all', file=sys.stderr)+ return 1elif pos is not None:try:port = int(pos)diff --git a/browser_use/skill_cli/tunnel_manager.py b/browser_use/skill_cli/tunnel_manager.pyindex 98bda7870..b590de68b 100644--- a/browser_use/skill_cli/tunnel_manager.py+++ b/browser_use/skill_cli/tunnel_manager.py@@ -5,13 +5,20 @@ Cloudflared must be installed via install.sh or manually by the user.Tunnels are managed independently of browser sessions - they are purelya network utility for exposing local ports via Cloudflare quick tunnels.++Tunnels survive CLI process exit by:+1. Spawning cloudflared as a daemon (start_new_session=True)+2. Tracking tunnel info via PID files in ~/.browser-use/tunnels/"""import asyncio+import jsonimport logging+import osimport reimport shutil-from dataclasses import dataclass+import signal+from pathlib import Pathfrom typing import Anylogger = logging.getLogger(__name__)@@ -19,18 +26,8 @@ logger = logging.getLogger(__name__)# Pattern to extract tunnel URL from cloudflared output_URL_PATTERN = re.compile(r'(https://\S+\.trycloudflare\.com)')--@dataclass-class TunnelInfo:- """Information about an active cloudflare tunnel."""-- port: int- url: str- process: asyncio.subprocess.Process---# Module-level storage for running tunnels (independent of browser sessions)-_active_tunnels: dict[int, TunnelInfo] = {}+# Directory for tunnel PID files+_TUNNELS_DIR = Path.home() / '.browser-use' / 'tunnels'class TunnelManager:@@ -107,6 +104,73 @@ def get_tunnel_manager() -> TunnelManager:return _tunnel_manager+# =============================================================================+# PID File Management+# =============================================================================+++def _get_tunnel_file(port: int) -> Path:+ """Get the path to a tunnel's info file."""+ return _TUNNELS_DIR / f'{port}.json'+++def _save_tunnel_info(port: int, pid: int, url: str) -> None:+ """Save tunnel info to disk."""+ _TUNNELS_DIR.mkdir(parents=True, exist_ok=True)+ _get_tunnel_file(port).write_text(json.dumps({'port': port, 'pid': pid, 'url': url}))+++def _load_tunnel_info(port: int) -> dict[str, Any] | None:+ """Load tunnel info from disk, returning None if not found or process dead."""+ tunnel_file = _get_tunnel_file(port)+ if not tunnel_file.exists():+ return None++ try:+ info = json.loads(tunnel_file.read_text())+ pid = info.get('pid')+ if pid and _is_process_alive(pid):+ return info+ # Process dead, clean up stale file+ tunnel_file.unlink(missing_ok=True)+ return None+ except (json.JSONDecodeError, OSError):+ tunnel_file.unlink(missing_ok=True)+ return None+++def _delete_tunnel_info(port: int) -> None:+ """Delete tunnel info file."""+ _get_tunnel_file(port).unlink(missing_ok=True)+++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+++def _kill_process(pid: int) -> bool:+ """Kill a process by PID. Returns True if killed, False if already dead."""+ try:+ os.kill(pid, signal.SIGTERM)+ # Give it a moment to terminate gracefully+ for _ in range(10):+ if not _is_process_alive(pid):+ return True+ import time++ time.sleep(0.1)+ # Force kill if still alive+ os.kill(pid, signal.SIGKILL)+ return True+ except (OSError, ProcessLookupError):+ return False++# =============================================================================# Standalone Tunnel Functions (no browser session required)# =============================================================================@@ -115,6 +179,8 @@ def get_tunnel_manager() -> TunnelManager:async def start_tunnel(port: int) -> dict[str, Any]:"""Start a cloudflare quick tunnel for a local port.+ The tunnel runs as a daemon process that survives CLI exit.+Args:port: Local port to tunnel@@ -122,9 +188,9 @@ async def start_tunnel(port: int) -> dict[str, Any]:Dict with 'url' and 'port' on success, or 'error' on failure"""# Check if tunnel already exists for this port- if port in _active_tunnels:- info = _active_tunnels[port]- return {'url': info.url, 'port': port, 'existing': True}+ existing = _load_tunnel_info(port)+ if existing:+ return {'url': existing['url'], 'port': port, 'existing': True}# Get cloudflared binarytry:@@ -133,7 +199,7 @@ async def start_tunnel(port: int) -> dict[str, Any]:except RuntimeError as e:return {'error': str(e)}- # Spawn cloudflared process+ # Spawn cloudflared as a daemon (start_new_session=True makes it survive parent exit)process = await asyncio.create_subprocess_exec(cloudflared_binary,'tunnel',@@ -141,6 +207,7 @@ async def start_tunnel(port: int) -> dict[str, Any]:f'http://localhost:{port}',stdout=asyncio.subprocess.DEVNULL,stderr=asyncio.subprocess.PIPE,+ start_new_session=True, # Daemon mode - survives parent exit)# Read stderr lines until we find the tunnel URL@@ -168,9 +235,9 @@ async def start_tunnel(port: int) -> dict[str, Any]:process.terminate()return {'error': 'Timed out waiting for cloudflare tunnel URL (15s)'}- # Store tunnel info- _active_tunnels[port] = TunnelInfo(port=port, url=url, process=process)- logger.info(f'Tunnel started: localhost:{port} -> {url}')+ # Save tunnel info to disk so it persists across CLI invocations+ _save_tunnel_info(port, process.pid, url)+ logger.info(f'Tunnel started: localhost:{port} -> {url} (pid={process.pid})')return {'url': url, 'port': port}@@ -181,7 +248,16 @@ def list_tunnels() -> dict[str, Any]:Returns:Dict with 'tunnels' list and 'count'"""- tunnels = [{'port': info.port, 'url': info.url} for info in _active_tunnels.values()]+ tunnels = []+ if _TUNNELS_DIR.exists():+ for tunnel_file in _TUNNELS_DIR.glob('*.json'):+ try:+ port = int(tunnel_file.stem)+ info = _load_tunnel_info(port)+ if info:+ tunnels.append({'port': info['port'], 'url': info['url']})+ except (ValueError, json.JSONDecodeError):+ continuereturn {'tunnels': tunnels, 'count': len(tunnels)}@@ -194,18 +270,17 @@ async def stop_tunnel(port: int) -> dict[str, Any]:Returns:Dict with 'stopped' port and 'url' on success, or 'error'"""- if port not in _active_tunnels:+ info = _load_tunnel_info(port)+ if not info:return {'error': f'No tunnel running on port {port}'}- info = _active_tunnels.pop(port)- info.process.terminate()- try:- await asyncio.wait_for(info.process.wait(), timeout=5)- except TimeoutError:- info.process.kill()+ url = info['url']+ pid = info['pid']+ _kill_process(pid)+ _delete_tunnel_info(port)logger.info(f'Tunnel stopped: localhost:{port}')- return {'stopped': port, 'url': info.url}+ return {'stopped': port, 'url': url}async def stop_all_tunnels() -> dict[str, Any]:@@ -215,9 +290,14 @@ async def stop_all_tunnels() -> dict[str, Any]:Dict with 'stopped' list of ports"""stopped = []- for port in list(_active_tunnels.keys()):- result = await stop_tunnel(port)- if 'stopped' in result:- stopped.append(port)+ if _TUNNELS_DIR.exists():+ for tunnel_file in _TUNNELS_DIR.glob('*.json'):+ try:+ port = int(tunnel_file.stem)+ result = await stop_tunnel(port)+ if 'stopped' in result:+ stopped.append(port)+ except ValueError:+ continuereturn {'stopped': stopped, 'count': len(stopped)}