Skip to content

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

set up a unique CloudSync for each agent

Nick Sweeting1d43d6729196
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index 82ddfddf6..c899e75a3 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -1304,7 +1304,8 @@ class Agent(Generic[Context]):
await self.cloud_sync.wait_for_auth()
# Stop the event bus gracefully, waiting for all events to be processed
- await self.eventbus.stop(timeout=5.0)
+ # Use longer timeout to avoid deadlocks in tests with multiple agents
+ await self.eventbus.stop(timeout=10.0)
await self.close()
diff --git a/tests/ci/test_sync_agent_events.py b/tests/ci/test_sync_agent_events.py
index 61790958a..7757bbd39 100644
--- a/tests/ci/test_sync_agent_events.py
+++ b/tests/ci/test_sync_agent_events.py
@@ -136,7 +136,7 @@ class TestAgentEventLifecycle:
assert len(gif_bytes) > 100 # Should be a real GIF file
@pytest.mark.usefixtures('mock_llm', 'browser_session', 'event_collector', 'httpserver')
- async def test_step_screenshot_capture(self, mock_llm, browser_session, cloud_sync, event_collector, httpserver):
+ async def test_step_screenshot_capture(self, mock_llm, browser_session, event_collector, httpserver):
"""Test that screenshots are captured for each step"""
# Setup test page
@@ -145,13 +145,12 @@ class TestAgentEventLifecycle:
)
await browser_session.navigate(httpserver.url_for('/'))
- # Create agent
+ # Create agent without cloud sync (not needed for screenshot test)
agent = Agent(
task='Test screenshot capture',
llm=mock_llm,
browser_session=browser_session,
generate_gif=False,
- cloud_sync=cloud_sync,
)
# Subscribe to all events
@@ -313,20 +312,29 @@ class TestAgentCloudIntegration:
assert result is not None
assert result.is_done()
- @pytest.mark.usefixtures('browser_session', 'cloud_sync', 'event_collector', 'httpserver')
- async def test_session_id_persistence(self, browser_session, cloud_sync, event_collector, httpserver):
+ @pytest.mark.usefixtures('browser_session', 'event_collector', 'httpserver')
+ async def test_session_id_persistence(self, browser_session, event_collector, httpserver):
"""Test that agent session ID persists across runs."""
# Set up httpserver endpoint
httpserver.expect_request('/api/v1/events', method='POST').respond_with_json(
{'processed': 1, 'failed': 0, 'results': [{'success': True}]}
)
+ # Import CloudSync to create instances
+ from browser_use.sync.service import CloudSync
+
+ # Create first CloudSync instance
+ cloud_sync1 = CloudSync(
+ base_url=httpserver.url_for(''),
+ enable_auth=False,
+ )
+
# Create first agent
agent1 = Agent(
task='First task',
llm=create_mock_llm(),
browser_session=browser_session,
- cloud_sync=cloud_sync,
+ cloud_sync=cloud_sync1,
)
agent1.eventbus.on('*', event_collector.collect_event)
@@ -341,12 +349,18 @@ class TestAgentCloudIntegration:
# Clear event collector
event_collector.clear()
+ # Create second CloudSync instance
+ cloud_sync2 = CloudSync(
+ base_url=httpserver.url_for(''),
+ enable_auth=False,
+ )
+
# Create second agent (will have different session ID)
agent2 = Agent(
task='Second task',
llm=create_mock_llm(),
browser_session=browser_session,
- cloud_sync=cloud_sync,
+ cloud_sync=cloud_sync2,
)
agent2.eventbus.on('*', event_collector.collect_event)