Skip to content

Self-improving AI Employees for everyone

fix: agent-browser orphan daemon on shutdown

trohitg6275a37931e0
diff --git a/server/services/browser_service.py b/server/services/browser_service.py
index d2ad3fd0..20a51872 100644
--- a/server/services/browser_service.py
+++ b/server/services/browser_service.py
@@ -11,6 +11,8 @@ import subprocess
import sys
from typing import Any, Dict, List, Optional
+import psutil
+
from core.logging import get_logger
logger = get_logger(__name__)
@@ -18,6 +20,27 @@ logger = get_logger(__name__)
_MAX_OUTPUT = 100_000
+def _kill_process_tree(pid: int) -> None:
+ """Terminate a process and all its descendants (cross-platform via psutil)."""
+ try:
+ parent = psutil.Process(pid)
+ except psutil.NoSuchProcess:
+ return
+
+ # Collect descendants before killing parent so we don't lose the tree.
+ descendants = parent.children(recursive=True)
+ for child in descendants:
+ try:
+ child.kill()
+ except psutil.NoSuchProcess:
+ pass
+
+ try:
+ parent.kill()
+ except psutil.NoSuchProcess:
+ pass
+
+
class BrowserService:
"""Subprocess wrapper for the agent-browser CLI."""
@@ -103,7 +126,11 @@ class BrowserService:
return line
finally:
- proc.kill()
+ # With shell=True on Windows, proc is the shell wrapper (cmd.exe)
+ # and the real agent-browser daemon runs as its child. Killing only
+ # proc leaves the daemon orphaned. psutil.children(recursive=True)
+ # walks the process tree natively on every platform.
+ _kill_process_tree(proc.pid)
proc.wait()