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
dont kill playwright at browser_session shutdown time
Nick Sweetingdf3c38ffc796
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex bc532b854..a85a55b3b 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -470,31 +470,8 @@ class BrowserSession(BaseModel):self.browser_profile.keep_alive = Falseawait self.stop()- # Clean up playwright instance to prevent background tasks from running- if self.playwright:- try:- await self.playwright.stop()- # Give playwright tasks a moment to clean up properly- # This prevents "Task was destroyed but it is pending!" warnings- await asyncio.sleep(0.1)- # self.logger.debug('🎭 Stopped playwright node.js API worker')- except Exception as e:- self.logger.warning(f'❌ Error stopping playwright node.js API subprocess: {type(e).__name__}: {e}')- finally:- # Clear global references if they match this instance- global GLOBAL_PLAYWRIGHT_API_OBJECT, GLOBAL_PATCHRIGHT_API_OBJECT- global GLOBAL_PLAYWRIGHT_EVENT_LOOP, GLOBAL_PATCHRIGHT_EVENT_LOOP-- if self.playwright == GLOBAL_PLAYWRIGHT_API_OBJECT:- GLOBAL_PLAYWRIGHT_API_OBJECT = None- GLOBAL_PLAYWRIGHT_EVENT_LOOP = None- # self.logger.debug('🧹 Cleared global playwright references')- elif self.playwright == GLOBAL_PATCHRIGHT_API_OBJECT:- GLOBAL_PATCHRIGHT_API_OBJECT = None- GLOBAL_PATCHRIGHT_EVENT_LOOP = None- # self.logger.debug('🧹 Cleared global patchright references')-- self.playwright = None+ # do not stop self.playwright here as its likely used by other parallel browser_sessions+ # let it be cleaned up by the garbage collector when no refs use it anymoreasync def new_context(self, **kwargs):"""Deprecated: Provides backwards-compatibility with old class method Browser().new_context()."""diff --git a/tests/ci/test_browser_session_start.py b/tests/ci/test_browser_session_start.pyindex 93af0a9cc..283366b7d 100644--- a/tests/ci/test_browser_session_start.py+++ b/tests/ci/test_browser_session_start.py@@ -953,3 +953,41 @@ class TestBrowserSessionReusePatterns:await window1.kill()await window2.kill()auth_json_path.unlink(missing_ok=True)++ async def test_browser_shutdown_isolated(self):+ """Test that browser shutdown doesnt affect other browser_sessions"""+ from browser_use import BrowserSession++ browser_session1 = BrowserSession(+ browser_profile=BrowserProfile(+ user_data_dir=None,+ headless=True,+ keep_alive=True, # Keep the browser alive for reuse+ ),+ )+ browser_session2 = BrowserSession(+ browser_profile=BrowserProfile(+ user_data_dir=None,+ headless=True,+ keep_alive=True, # Keep the browser alive for reuse+ ),+ )+ await browser_session1.start()+ await browser_session2.start()++ assert browser_session1.is_connected()+ assert browser_session2.is_connected()+ assert browser_session1.browser_context != browser_session2.browser_context++ await browser_session1.create_new_tab('chrome://version')+ await browser_session2.create_new_tab('chrome://settings')++ await browser_session2.kill()++ # ensure that the browser_session1 is still connected and unaffected by the kill of browser_session2+ assert browser_session1.is_connected()+ assert browser_session1.browser_context is not None+ await browser_session1.create_new_tab('chrome://settings')+ await browser_session1.browser_context.pages[0].evaluate('alert(1)')++ await browser_session1.kill()