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
fix async-await issues preventing crash
Ajith7675b12558e5
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex e7f687de6..01424147d 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -10,6 +10,7 @@ import sysimport timefrom collections.abc import Awaitable, Callablefrom pathlib import Path+from threading import Threadfrom typing import Any, Generic, TypeVarfrom dotenv import load_dotenv@@ -592,7 +593,33 @@ class Agent(Generic[Context]):return results# Execute async tests- results = asyncio.run(test_all_methods())+ try:+ loop = asyncio.get_running_loop()+ # Running loop: create a new loop in a separate thread+ result = {}++ def run_in_thread():+ new_loop = asyncio.new_event_loop()+ asyncio.set_event_loop(new_loop)+ try:+ result['value'] = new_loop.run_until_complete(test_all_methods())+ except Exception as e:+ result['error'] = e+ finally:+ new_loop.close()++ t = Thread(target=run_in_thread)+ t.start()+ t.join()+ if 'error' in result:+ raise result['error']+ results = result['value']++ except RuntimeError as e:+ if 'no running event loop' in str(e):+ results = asyncio.run(test_all_methods())+ else:+ raise# Process results in order of preferencefor i, method in enumerate(methods_to_try):