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
use threading lock instead of asynclock to support parallel tests
Nick Sweeting43ef4d603b78
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex 447245fd1..5695cd822 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -9,6 +9,7 @@ import osimport reimport shutilimport tempfile+import threadingimport timefrom dataclasses import dataclassfrom functools import wraps@@ -57,7 +58,8 @@ GLOBAL_PLAYWRIGHT_API_OBJECT = None # never instantiate the playwright API objeGLOBAL_PATCHRIGHT_API_OBJECT = None # never instantiate the patchright API object more than once per threadGLOBAL_PLAYWRIGHT_EVENT_LOOP = None # track which event loop the global objects belong toGLOBAL_PATCHRIGHT_EVENT_LOOP = None # track which event loop the global objects belong to-GLOBAL_PLAYWRIGHT_LOCK = asyncio.Lock() # protect concurrent access to global playwright objects+# Use a threading lock to protect playwright object creation across all event loops and processes+GLOBAL_PLAYWRIGHT_THREADING_LOCK = threading.Lock()def _log_glob_warning(domain: str, glob: str, logger: logging.Logger):@@ -578,7 +580,7 @@ class BrowserSession(BaseModel):global GLOBAL_PATCHRIGHT_API_OBJECTglobal GLOBAL_PLAYWRIGHT_EVENT_LOOP # one per thread, represents a node.js playwright subprocess that relays commands to the browser via CDPglobal GLOBAL_PATCHRIGHT_EVENT_LOOP- global GLOBAL_PLAYWRIGHT_LOCK+ global GLOBAL_PLAYWRIGHT_THREADING_LOCK# Get current event looptry:@@ -597,38 +599,67 @@ class BrowserSession(BaseModel):# use playwright + chromium by defaultself.browser_profile.channel = self.browser_profile.channel or BrowserChannel.CHROMIUM- # Use lock with timeout to prevent race conditions when creating global playwright object- async with asyncio.timeout(30): # 30 second timeout to prevent deadlocks- async with GLOBAL_PLAYWRIGHT_LOCK:- # Re-check global objects inside the lock to avoid race conditions- driver_name = 'patchright' if is_stealth else 'playwright'- global_api_object = GLOBAL_PATCHRIGHT_API_OBJECT if is_stealth else GLOBAL_PLAYWRIGHT_API_OBJECT- global_event_loop = GLOBAL_PATCHRIGHT_EVENT_LOOP if is_stealth else GLOBAL_PLAYWRIGHT_EVENT_LOOP+ # Use threading lock to prevent race conditions when creating global playwright object+ # Check if we need to create the playwright object+ needs_creation = False- # Check if we need to create or recreate the global object- should_recreate = False+ # Use acquire with timeout to prevent deadlocks+ if not GLOBAL_PLAYWRIGHT_THREADING_LOCK.acquire(timeout=30): # 30 second timeout+ raise TimeoutError('Failed to acquire playwright lock within 30 seconds - potential deadlock detected')- if global_api_object and global_event_loop != current_loop:- self.logger.debug(- f'Detected event loop change. Previous {driver_name} instance was created in a different event loop. '- 'Creating new instance to avoid disconnection when the previous loop closes.'- )+ try:+ # Re-check global objects inside the lock to avoid race conditions+ driver_name = 'patchright' if is_stealth else 'playwright'+ global_api_object = GLOBAL_PATCHRIGHT_API_OBJECT if is_stealth else GLOBAL_PLAYWRIGHT_API_OBJECT+ global_event_loop = GLOBAL_PATCHRIGHT_EVENT_LOOP if is_stealth else GLOBAL_PLAYWRIGHT_EVENT_LOOP++ # Check if we need to create or recreate the global object+ should_recreate = False++ if global_api_object and global_event_loop != current_loop:+ self.logger.debug(+ f'Detected event loop change. Previous {driver_name} instance was created in a different event loop. '+ 'Creating new instance to avoid disconnection when the previous loop closes.'+ )+ should_recreate = True++ # Also check if the object exists but is no longer functional+ if global_api_object and not should_recreate:+ try:+ # Try to access the chromium property to verify the object is still valid+ _ = global_api_object.chromium.executable_path+ except Exception as e:+ self.logger.debug(f'Detected invalid {driver_name} instance: {type(e).__name__}. Creating new instance.')should_recreate = True- # Also check if the object exists but is no longer functional- if global_api_object and not should_recreate:- try:- # Try to access the chromium property to verify the object is still valid- _ = global_api_object.chromium.executable_path- except Exception as e:- self.logger.debug(f'Detected invalid {driver_name} instance: {type(e).__name__}. Creating new instance.')- should_recreate = True+ # If we already have a valid object, use it+ if global_api_object and not should_recreate:+ self.playwright = self.playwright or global_api_object+ else:+ needs_creation = True+ finally:+ GLOBAL_PLAYWRIGHT_THREADING_LOCK.release()- # Create or recreate the global object if needed- if not global_api_object or should_recreate:- self.playwright = await self._start_global_playwright_subprocess(is_stealth=is_stealth)- else:- self.playwright = self.playwright or global_api_object+ # Create the playwright object outside the lock to avoid blocking other threads+ if needs_creation:+ # Use timeout to prevent deadlocks during creation+ async with asyncio.timeout(30):+ new_playwright = await self._start_global_playwright_subprocess(is_stealth=is_stealth)++ # Re-acquire lock to update global state+ if not GLOBAL_PLAYWRIGHT_THREADING_LOCK.acquire(timeout=30): # 30 second timeout+ raise TimeoutError('Failed to re-acquire playwright lock within 30 seconds - potential deadlock detected')++ try:+ # Double-check that another thread didn't create it while we were waiting+ global_api_object = GLOBAL_PATCHRIGHT_API_OBJECT if is_stealth else GLOBAL_PLAYWRIGHT_API_OBJECT+ if not global_api_object:+ self.playwright = new_playwright+ else:+ # Another thread created it, use that one+ self.playwright = global_api_object+ finally:+ GLOBAL_PLAYWRIGHT_THREADING_LOCK.release()# Log stealth best-practices warnings if applicableif is_stealth:diff --git a/tests/ci/test_browser_session_start.py b/tests/ci/test_browser_session_start.pyindex ab3d3f52a..844b092e3 100644--- a/tests/ci/test_browser_session_start.py+++ b/tests/ci/test_browser_session_start.py@@ -978,9 +978,10 @@ class TestBrowserSessionReusePatterns:))+ print('Starting many parallel browser sessions...')await asyncio.gather(*[browser_session.start() for browser_session in browser_sessions])- # ensure all are connected and usable+ print('Ensuring all parallel browser sessions are connected and usable...')new_tab_tasks = []for browser_session in browser_sessions:assert await browser_session.is_connected()@@ -988,14 +989,14 @@ class TestBrowserSessionReusePatterns:new_tab_tasks.append(browser_session.create_new_tab('chrome://version'))await asyncio.gather(*new_tab_tasks)- # kill every 3rd browser_session+ print('killing every 3rd browser_session to test parallel shutdown')kill_tasks = []for i in range(0, len(browser_sessions), 3):kill_tasks.append(browser_sessions[i].kill())browser_sessions[i] = Noneawait asyncio.gather(*kill_tasks)- # ensure the remaining browser_sessions are still connected and usable+ print('ensuring the remaining browser_sessions are still connected and usable')new_tab_tasks = []screenshot_tasks = []for browser_session in filter(bool, browser_sessions):@@ -1007,6 +1008,7 @@ class TestBrowserSessionReusePatterns:await asyncio.gather(*screenshot_tasks)kill_tasks = []+ print('killing the remaining browser_sessions')for browser_session in filter(bool, browser_sessions):kill_tasks.append(browser_session.kill())await asyncio.gather(*kill_tasks)