Skip to content

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

Fix #2614: Prevent QueueShutDown exception when using add_new_task + multiple runs

Louis8a11d05bf8f1
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index 657290d35..f99007193 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -1224,9 +1224,13 @@ class Agent(Generic[Context, AgentStructuredOutput]):
self._session_start_time = time.time()
self._task_start_time = self._session_start_time # Initialize task start time
- self.logger.debug('📡 Dispatching CreateAgentSessionEvent...')
- # Emit CreateAgentSessionEvent at the START of run()
- self.eventbus.dispatch(CreateAgentSessionEvent.from_agent(self))
+ # Only dispatch session events if this is the first run
+ if not self.state.session_initialized:
+ self.logger.debug('📡 Dispatching CreateAgentSessionEvent...')
+ # Emit CreateAgentSessionEvent at the START of run()
+ self.eventbus.dispatch(CreateAgentSessionEvent.from_agent(self))
+
+ self.state.session_initialized = True
self.logger.debug('📡 Dispatching CreateAgentTaskEvent...')
# Emit CreateAgentTaskEvent at the START of run()
diff --git a/browser_use/agent/views.py b/browser_use/agent/views.py
index 4af745599..b7d363804 100644
--- a/browser_use/agent/views.py
+++ b/browser_use/agent/views.py
@@ -78,6 +78,7 @@ class AgentState(BaseModel):
last_model_output: AgentOutput | None = None
paused: bool = False
stopped: bool = False
+ session_initialized: bool = False # Track if session events have been dispatched
message_manager_state: MessageManagerState = Field(default_factory=MessageManagerState)
file_system_state: FileSystemState | None = None