Skip to content

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

fix async errors in features/pause_agent.py

Nick Sweeting1cf4104ed362
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py
index 394396661..1c8f86afc 100644
--- a/browser_use/agent/service.py
+++ b/browser_use/agent/service.py
@@ -234,8 +234,12 @@ class Agent(Generic[Context]):
# Start non-blocking LLM connection verification using create_task, checked later in step()
# This will run in parallel with browser launch without leaving dangling coroutines on unclean exits
self.llm._verified_api_keys = False
- self._verification_task = asyncio.create_task(self._verify_llm_connection())
- self._verification_task.add_done_callback(lambda _: None)
+ try:
+ self._verification_task = asyncio.create_task(self._verify_llm_connection())
+ self._verification_task.add_done_callback(lambda _: None)
+ except RuntimeError:
+ # ppl kept reporting _verify_llm_connection was never awaited errors but it's a red-herring:
+ logger.warning('No running event loop found. All browser-use code must be run inside an async context.')
# Initialize available actions for system prompt (only non-filtered actions)
# These will be used for the system prompt to maintain caching
@@ -803,9 +807,14 @@ class Agent(Generic[Context]):
pass
# Check that verification was successful
- assert self.llm._verified_api_keys or SKIP_LLM_API_KEY_VERIFICATION, (
- 'Failed to connect to LLM API or LLM API is not responding correctly'
- )
+ try:
+ assert self.llm._verified_api_keys or SKIP_LLM_API_KEY_VERIFICATION, (
+ 'Failed to connect to LLM API or LLM API is not responding correctly'
+ )
+ except AssertionError:
+ logger.warning(
+ '⚠️ Could not connect to LLM API! Continuing anyway for now... (are your API keys valid and do you have enough money in your account?)'
+ )
try:
self._log_agent_run()
diff --git a/browser_use/browser/browser.py b/browser_use/browser/browser.py
index fec6f1ea4..dcde49434 100644
--- a/browser_use/browser/browser.py
+++ b/browser_use/browser/browser.py
@@ -342,7 +342,8 @@ class Browser:
# Then cleanup httpx clients
await self.cleanup_httpx_clients()
except Exception as e:
- logger.debug(f'Failed to close browser properly: {e}')
+ if 'OpenAI error' not in str(e):
+ logger.debug(f'Failed to close browser properly: {e}')
finally:
self.playwright_browser = None
diff --git a/examples/features/pause_agent.py b/examples/features/pause_agent.py
index e021224cf..8e66a84e7 100644
--- a/examples/features/pause_agent.py
+++ b/examples/features/pause_agent.py
@@ -1,9 +1,9 @@
+import asyncio
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
-import asyncio
import threading
import time
@@ -55,7 +55,7 @@ def print_menu():
print('5. Exit')
-def main():
+async def main():
controller = AgentController()
agent_thread = None
@@ -95,4 +95,4 @@ def main():
if __name__ == '__main__':
- main()
+ asyncio.run(main())