Skip to content

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

fix keep-alive hang

Laith Weinberger3de7f1d9c8a1
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index 6bc7757df..cebab6297 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -3911,6 +3911,17 @@ class Agent(Generic[Context, AgentStructuredOutput]):
# Kill the browser session - this dispatches BrowserStopEvent,
# stops the EventBus with clear=True, and recreates a fresh EventBus
await self.browser_session.kill()
+ else:
+ # keep_alive=True sessions shouldn't keep the event loop alive after agent.run()
+ await self.browser_session.event_bus.stop(
+ clear=False,
+ timeout=_get_timeout('TIMEOUT_BrowserSessionEventBusStopOnAgentClose', 1.0),
+ )
+ try:
+ self.browser_session.event_bus.event_queue = None
+ self.browser_session.event_bus._on_idle = None
+ except Exception:
+ pass
# Close skill service if configured
if self.skill_service is not None: