Skip to content

fix(browser): register event loop on current thread in _execute_async#485

Open
xlyoung wants to merge 1 commit into
strands-agents:mainfrom
xlyoung:fix/browser-event-loop-cross-thread
Open

fix(browser): register event loop on current thread in _execute_async#485
xlyoung wants to merge 1 commit into
strands-agents:mainfrom
xlyoung:fix/browser-event-loop-cross-thread

Conversation

@xlyoung
Copy link
Copy Markdown

@xlyoung xlyoung commented Jun 2, 2026

Summary

Fixes #453

When stream_async() dispatches _execute_async to a worker thread, the event loop created in Browser.__init__() is not registered on that thread. This causes self._loop.run_until_complete() to fail with:

RuntimeError: There is no current event loop in thread 'asyncio_0'

Root Cause

  • __init__ calls asyncio.set_event_loop(self._loop) on the creating thread
  • _execute_async is a synchronous method that Strands dispatches to a different thread via _stream
  • The worker thread has no event loop registered, so run_until_complete raises

Fix

Add asyncio.set_event_loop(self._loop) at the start of _execute_async to ensure the loop is registered on whichever thread executes the method. This is idempotent and safe - if the loop is already set, it's a no-op.

Testing

  • Verified that AgentCoreBrowser works with agent.stream_async() when called from within an already-running async event loop
  • Existing sync agent() calls continue to work unchanged

Closes #453

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 strands-agents#453
@xlyoung xlyoung requested a deployment to manual-approval June 2, 2026 00:15 — with GitHub Actions Waiting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Browser tool fails when used with stream_async

1 participant