Skip to content

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

fix: SIGTERM fallback for orphaned daemons in close command

ShawnPanaac48ebecafc2
diff --git a/browser_use/skill_cli/daemon.py b/browser_use/skill_cli/daemon.py
index a8f578de5..c36afcdc9 100644
--- a/browser_use/skill_cli/daemon.py
+++ b/browser_use/skill_cli/daemon.py
@@ -503,8 +503,8 @@ class Daemon:
logger.warning(f'Error closing session: {e}')
self._session = None
- # Clean up PID file before exiting — the finally block in run() won't
- # execute because os._exit() bypasses it.
+ # Delete PID file last, right before exit. If browser cleanup hangs above,
+ # the PID file still exists so `sessions` can discover the orphaned daemon.
import os
from browser_use.skill_cli.utils import get_pid_path
diff --git a/browser_use/skill_cli/main.py b/browser_use/skill_cli/main.py
index 2fd00552a..01f88bd5b 100755
--- a/browser_use/skill_cli/main.py
+++ b/browser_use/skill_cli/main.py
@@ -11,6 +11,7 @@ import asyncio
import json
import os
import re
+import signal
import socket
import subprocess
import sys
@@ -1070,10 +1071,27 @@ def main() -> int:
except Exception:
print('Browser closed')
else:
+ # Socket unreachable — check if a PID file references a live process
+ # (orphaned daemon that lost its socket). Kill it directly.
+ pid_path = Path(_get_home_dir()) / f'{session}.pid'
+ killed = False
+ if pid_path.exists():
+ try:
+ pid = int(pid_path.read_text().strip())
+ os.kill(pid, 0) # Check alive
+ os.kill(pid, signal.SIGTERM)
+ killed = True
+ except (OSError, ProcessLookupError, ValueError):
+ pass
+ pid_path.unlink(missing_ok=True)
+
if args.json:
print(json.dumps({'success': True, 'data': {'shutdown': True}}))
else:
- print('No active browser session')
+ if killed:
+ print('Browser closed')
+ else:
+ print('No active browser session')
return 0
# Resolve --connect to agent_id + CDP URL