From 42063aabf88ebd0811a4f813ffe83e973ee3a119 Mon Sep 17 00:00:00 2001 From: xlyoung Date: Tue, 2 Jun 2026 08:14:54 +0800 Subject: [PATCH] fix(browser): register event loop on current thread in _execute_async When stream_async() dispatches _execute_async to a worker thread, the event loop created in __init__ is not registered on that thread, causing 'RuntimeError: There is no current event loop in thread'. Fix by calling asyncio.set_event_loop(self._loop) at the start of _execute_async to ensure the loop is always available regardless of which thread executes the method. Fixes #453 --- src/strands_tools/browser/browser.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/strands_tools/browser/browser.py b/src/strands_tools/browser/browser.py index 03d44808..1f85d3f6 100644 --- a/src/strands_tools/browser/browser.py +++ b/src/strands_tools/browser/browser.py @@ -946,6 +946,14 @@ def close(self, action: CloseAction) -> Dict[str, Any]: return {"status": "error", "content": [{"text": f"Error: {str(e)}"}]} def _execute_async(self, action_coro) -> Any: + # Ensure the event loop is registered on the current thread. + # When stream_async() dispatches this method to a worker thread, + # the loop created in __init__ may not be set as the current + # thread's event loop, causing run_until_complete to fail with + # "RuntimeError: There is no current event loop in thread". + # See: strands-agents/tools#453 + asyncio.set_event_loop(self._loop) + # Apply nest_asyncio if not already applied if not self._nest_asyncio_applied: nest_asyncio.apply()