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: Move HTTPX client cleanup from agent to browser close to avoid breaking multi-agent task
lynnee6a625160c0
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex 93f5af44d..cfe2a5092 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -981,25 +981,6 @@ class Agent(Generic[Context]):def message_manager(self) -> MessageManager:return self._message_manager- async def cleanup_httpx_clients(self):- """Cleanup all httpx clients"""- import httpx- import gc-- # Force garbage collection to make sure all clients are in memory- gc.collect()-- # Get all httpx clients- clients = [obj for obj in gc.get_objects() if isinstance(obj, httpx.AsyncClient)]-- # Close all clients- for client in clients:- if not client.is_closed:- try:- await client.aclose()- except Exception as e:- logger.debug(f"Error closing httpx client: {e}")-async def close(self):"""Close all resources"""try:@@ -1009,9 +990,6 @@ class Agent(Generic[Context]):if self.browser and not self.injected_browser:await self.browser.close()- # Then cleanup httpx clients- await self.cleanup_httpx_clients()-# Force garbage collectiongc.collect()diff --git a/browser_use/browser/browser.py b/browser_use/browser/browser.pyindex 18937d8e7..32c0de789 100644--- a/browser_use/browser/browser.py+++ b/browser_use/browser/browser.py@@ -253,7 +253,8 @@ class Browser:if self.playwright:await self.playwright.stop()del self.playwright-+ # Then cleanup httpx clients+ await self.cleanup_httpx_clients()except Exception as e:logger.debug(f'Failed to close browser properly: {e}')finally:@@ -273,3 +274,22 @@ class Browser:asyncio.run(self.close())except Exception as e:logger.debug(f'Failed to cleanup browser in destructor: {e}')++ async def cleanup_httpx_clients(self):+ """Cleanup all httpx clients"""+ import httpx+ import gc++ # Force garbage collection to make sure all clients are in memory+ gc.collect()++ # Get all httpx clients+ clients = [obj for obj in gc.get_objects() if isinstance(obj, httpx.AsyncClient)]++ # Close all clients+ for client in clients:+ if not client.is_closed:+ try:+ await client.aclose()+ except Exception as e:+ logger.debug(f"Error closing httpx client: {e}")\ No newline at end of file