Skip to content

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

place granular 15sec timeout on browser setup sub-steps

Nick Sweeting4428a075de92
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index 998512ae6..8ee7078e5 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -662,13 +662,15 @@ class BrowserSession(BaseModel):
if not self.browser_profile.user_data_dir:
# self.logger.debug('🌎 Launching local browser in incognito mode')
# if no user_data_dir is provided, launch an incognito context with no persistent user_data_dir
- self.browser = self.browser or await self.playwright.chromium.launch(
- **self.browser_profile.kwargs_for_launch().model_dump()
- )
+ async with asyncio.timeout(15):
+ self.browser = self.browser or await self.playwright.chromium.launch(
+ **self.browser_profile.kwargs_for_launch().model_dump()
+ )
# self.logger.debug('🌎 Launching new incognito context in browser')
- self.browser_context = await self.browser.new_context(
- **self.browser_profile.kwargs_for_new_context().model_dump()
- )
+ async with asyncio.timeout(15):
+ self.browser_context = await self.browser.new_context(
+ **self.browser_profile.kwargs_for_new_context().model_dump()
+ )
# self.logger.debug('🌎 Created new incognito context in browser')
else:
# user data dir was provided, prepare it for use
@@ -684,43 +686,46 @@ class BrowserSession(BaseModel):
break
# if a user_data_dir is provided, launch a persistent context with that user_data_dir
- try:
- self.browser_context = await self.playwright.chromium.launch_persistent_context(
- **self.browser_profile.kwargs_for_launch_persistent_context().model_dump()
- )
- except Exception as e:
- # show a nice logger hint explaining what went wrong with the user_data_dir
- # calculate the version of the browser that the user_data_dir is for, and the version of the browser we are running with
- user_data_dir_chrome_version = '???'
- test_browser_version = '???'
- try:
- # user_data_dir is corrupted or unreadable because it was migrated to a newer version of chrome than we are running with
- user_data_dir_chrome_version = (self.browser_profile.user_data_dir / 'Last Version').read_text().strip()
- except Exception:
- pass # let the logger below handle it
+ async with asyncio.timeout(15):
try:
- test_browser = await self.playwright.chromium.launch(headless=True)
- test_browser_version = test_browser.version
- await test_browser.close()
- except Exception:
- pass
-
- # failed to parse extensions == most common error text when user_data_dir is corrupted / has an unusable schema
- reason = 'due to bad' if 'Failed parsing extensions' in str(e) else 'for unknown reason with'
- driver = str(type(self.playwright).__module__).split('.')[0].lower()
- browser_channel = (
- Path(self.browser_profile.executable_path).name.replace(' ', '-').replace('.exe', '').lower()
- if self.browser_profile.executable_path
- else (self.browser_profile.channel or BROWSERUSE_DEFAULT_CHANNEL).name.lower()
- )
- self.logger.error(
- f'❌ Launching new local browser {driver}:{browser_channel} (v{test_browser_version}) failed!'
- f'\n\tFailed {reason} user_data_dir= {_log_pretty_path(self.browser_profile.user_data_dir)} (created with v{user_data_dir_chrome_version})'
- '\n\tTry using a different browser version/channel or delete the user_data_dir to start over with a fresh profile.'
- '\n\t(can happen if different versions of Chrome/Chromium/Brave/etc. tried to share one dir)'
- f'\n\n{type(e).__name__} {e}'
- )
- raise
+ self.browser_context = await self.playwright.chromium.launch_persistent_context(
+ **self.browser_profile.kwargs_for_launch_persistent_context().model_dump()
+ )
+ except Exception as e:
+ # show a nice logger hint explaining what went wrong with the user_data_dir
+ # calculate the version of the browser that the user_data_dir is for, and the version of the browser we are running with
+ user_data_dir_chrome_version = '???'
+ test_browser_version = '???'
+ try:
+ # user_data_dir is corrupted or unreadable because it was migrated to a newer version of chrome than we are running with
+ user_data_dir_chrome_version = (
+ (self.browser_profile.user_data_dir / 'Last Version').read_text().strip()
+ )
+ except Exception:
+ pass # let the logger below handle it
+ try:
+ test_browser = await self.playwright.chromium.launch(headless=True)
+ test_browser_version = test_browser.version
+ await test_browser.close()
+ except Exception:
+ pass
+
+ # failed to parse extensions == most common error text when user_data_dir is corrupted / has an unusable schema
+ reason = 'due to bad' if 'Failed parsing extensions' in str(e) else 'for unknown reason with'
+ driver = str(type(self.playwright).__module__).split('.')[0].lower()
+ browser_channel = (
+ Path(self.browser_profile.executable_path).name.replace(' ', '-').replace('.exe', '').lower()
+ if self.browser_profile.executable_path
+ else (self.browser_profile.channel or BROWSERUSE_DEFAULT_CHANNEL).name.lower()
+ )
+ self.logger.error(
+ f'❌ Launching new local browser {driver}:{browser_channel} (v{test_browser_version}) failed!'
+ f'\n\tFailed {reason} user_data_dir= {_log_pretty_path(self.browser_profile.user_data_dir)} (created with v{user_data_dir_chrome_version})'
+ '\n\tTry using a different browser version/channel or delete the user_data_dir to start over with a fresh profile.'
+ '\n\t(can happen if different versions of Chrome/Chromium/Brave/etc. tried to share one dir)'
+ f'\n\n{type(e).__name__} {e}'
+ )
+ raise
# Only restore browser from context if it's connected, otherwise keep it None to force new launch
browser_from_context = self.browser_context and self.browser_context.browser