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
kill chrome subprocess if keep_alive=False
Nick Sweeting649e3fa757e6
diff --git a/browser_use/browser/browser.py b/browser_use/browser/browser.pyindex 88fc7ba17..c6262b121 100644--- a/browser_use/browser/browser.py+++ b/browser_use/browser/browser.py@@ -9,6 +9,7 @@ import osimport subprocessfrom typing import Literal+import psutilimport requestsfrom dotenv import load_dotenvfrom playwright.async_api import Browser as PlaywrightBrowser@@ -193,10 +194,13 @@ class Browser:*self.config.extra_browser_args,},]- subprocess.Popen(- chrome_launch_cmd,- stdout=subprocess.DEVNULL,- stderr=subprocess.DEVNULL,+ self._chrome_subprocess = psutil.Process(+ subprocess.Popen(+ chrome_launch_cmd,+ stdout=subprocess.DEVNULL,+ stderr=subprocess.DEVNULL,+ shell=False,+ ).pid)# Attempt to connect again after starting a new instance@@ -300,6 +304,15 @@ class Browser:if self.playwright:await self.playwright.stop()del self.playwright+ if chrome_proc := getattr(self, '_chrome_subprocess', None):+ try:+ # always kill all children processes, otherwise chrome leaves a bunch of zombie processes+ for proc in chrome_proc.children(recursive=True):+ proc.kill()+ chrome_proc.kill()+ except Exception as e:+ logger.debug(f'Failed to terminate chrome subprocess: {e}')+# Then cleanup httpx clientsawait self.cleanup_httpx_clients()except Exception as e:@@ -308,6 +321,7 @@ class Browser:finally:self.playwright_browser = Noneself.playwright = None+ self._chrome_subprocess = Nonegc.collect()def __del__(self):diff --git a/pyproject.toml b/pyproject.tomlindex 27c1736a4..79e51de33 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -27,6 +27,7 @@ dependencies = ["pyobjc>=11.0; platform_system == 'darwin'","screeninfo>=0.8.1; platform_system != 'darwin'","typing-extensions>=4.12.2",+ "psutil>=7.0.0",]# google-api-core: only used for Google LLM APIs# pyperclip: only used for examples that use copy/paste