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
clone session inside model_copy
Nick Sweeting892db27b3cad
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex ada631d97..83a099cc0 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -348,24 +348,9 @@ class Agent(Generic[Context]):'Call: await browser_session.start()')- # always copy sessions that are passed in to avoid agents overwriting each other's agent_current_page and human_current_page by accident- # model_copy() doesn't copy excluded fields, so we need to manually copy them- self.browser_session = browser_session.model_copy(- # update={- # 'agent_current_page': None, # dont reset these, let the next agent start on the same page as the last agent- # 'human_current_page': None,- # },- )- # Manually copy over the excluded fields that are needed for browser connection- self.browser_session.playwright = browser_session.playwright- self.browser_session.browser = browser_session.browser- self.browser_session.browser_context = browser_session.browser_context- self.browser_session.agent_current_page = browser_session.agent_current_page- self.browser_session.human_current_page = browser_session.human_current_page-- # Keep a reference to the original browser session to prevent it from being garbage collected- # This ensures the browser connection stays alive even if this agent goes out of scope- self.browser_session._original_browser_session = browser_session+ # Always copy sessions that are passed in to avoid agents overwriting each other's agent_current_page and human_current_page by accident+ # The model_copy() method now handles copying all necessary fields and setting up ownership+ self.browser_session = browser_session.model_copy()else:if browser is not None:assert isinstance(browser, Browser), 'Browser is not set up'diff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex c051fdc1d..4946f46e0 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -461,7 +461,13 @@ class BrowserSession(BaseModel):await self.stop(_hint='(context manager exit)')def model_copy(self, **kwargs) -> Self:- """Create a copy of this BrowserSession that shares the browser resources but doesn't own them."""+ """Create a copy of this BrowserSession that shares the browser resources but doesn't own them.++ This method creates a copy that:+ - Shares the same browser, browser_context, and playwright objects+ - Doesn't own the browser resources (won't close them when garbage collected)+ - Keeps a reference to the original to prevent premature garbage collection+ """# Create the copy using the parent class methodcopy = super().model_copy(**kwargs)@@ -471,6 +477,14 @@ class BrowserSession(BaseModel):# Keep a reference to the original to prevent garbage collectioncopy._original_browser_session = self+ # Manually copy over the excluded fields that are needed for browser connection+ # These fields are excluded in the model config but need to be shared+ copy.playwright = self.playwright+ copy.browser = self.browser+ copy.browser_context = self.browser_context+ copy.agent_current_page = self.agent_current_page+ copy.human_current_page = self.human_current_page+return copydef __del__(self):