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
more error handling for browser closing
Gregor Žunič75d28b01f596
diff --git a/browser_use/browser/browser.py b/browser_use/browser/browser.pyindex 29826fdce..a76effeb5 100644--- a/browser_use/browser/browser.py+++ b/browser_use/browser/browser.py@@ -136,6 +136,7 @@ class Browser:raise RuntimeError(' To start chrome in Debug mode, you need to close all existing Chrome instances and try again otherwise we can not connect to the instance.')+else:try:disable_security_args = []@@ -162,7 +163,7 @@ class Browser:'--no-default-browser-check','--no-startup-window','--window-position=0,0',- '--window-size=3000,3000',+ # '--window-size=3000,3000',]+ disable_security_args+ self.config.extra_chromium_args,@@ -175,18 +176,25 @@ class Browser:async def close(self):"""Close the browser instance"""- if self.playwright_browser:- await self.playwright_browser.close()- if self.playwright:- await self.playwright.stop()+ try:+ if self.playwright_browser:+ await self.playwright_browser.close()+ if self.playwright:+ await self.playwright.stop()+ except Exception as e:+ logger.debug(f'Failed to close browser properly: {e}')+ finally:+ self.playwright_browser = None+ self.playwright = Nonedef __del__(self):"""Async cleanup when object is destroyed"""try:- loop = asyncio.get_running_loop()- if loop.is_running():- loop.create_task(self.close())- else:- asyncio.run(self.close())- except RuntimeError:- asyncio.run(self.close())+ if self.playwright_browser or self.playwright:+ loop = asyncio.get_running_loop()+ if loop.is_running():+ loop.create_task(self.close())+ else:+ asyncio.run(self.close())+ except Exception as e:+ logger.debug(f'Failed to cleanup browser in destructor: {e}')diff --git a/browser_use/browser/context.py b/browser_use/browser/context.pyindex 1e2285d32..0d8dfcbfa 100644--- a/browser_use/browser/context.py+++ b/browser_use/browser/context.py@@ -126,18 +126,27 @@ class BrowserContext:"""Close the browser instance"""logger.debug('Closing browser context')- # check if already closed- if self.session is None:- return+ try:+ # check if already closed+ if self.session is None:+ return- await self.save_cookies()+ await self.save_cookies()- if self.config.trace_path:- await self.session.context.tracing.stop(- path=os.path.join(self.config.trace_path, f'{self.context_id}.zip')- )+ if self.config.trace_path:+ try:+ await self.session.context.tracing.stop(+ path=os.path.join(self.config.trace_path, f'{self.context_id}.zip')+ )+ except Exception as e:+ logger.debug(f'Failed to stop tracing: {e}')- await self.session.context.close()+ try:+ await self.session.context.close()+ except Exception as e:+ logger.debug(f'Failed to close context: {e}')+ finally:+ self.session = Nonedef __del__(self):"""Cleanup when object is destroyed"""