Skip to content

Conversation

osdemah
Copy link

@osdemah osdemah commented Sep 9, 2025

Description

Playwright instance is not safe to be used across different event loops. The bad practice of using nested event loops causes deadlock if the playwright instance is used for further operations after the agent is done. (e.g. processing cookies, checking history, etc).

Example code to reproduce the deadlock:

async def test():
    browser = LocalChromiumBrowser()
    browser._start()
    browser.init_session(InitSessionAction(type="init_session", session_name="session-1234", description="my-frist-session"))
    print(browser.navigate(NavigateAction(type="navigate", session_name="session-1234", url="https://amazon.com")))
    print(browser.get_cookies(GetCookiesAction(type="get_cookies", session_name="session-1234")))
    page = browser.get_session_page("session-1234")
    assert page
    # following line never finishes!
    print(await page.context.cookies())

Considering strands support of asyncio, we can now simply call Playwright APIs in the same event loop as agent's. The only exception is the __del__ function which is itself a bad practice but kept it to avoid resource leakage in the consumers already using this tool. Ideally the consumers should switch to using provided async context manager that calls __aexit__ when block is finished.

Type of Change

Bug fix

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@osdemah osdemah requested a review from a team as a code owner September 9, 2025 00:57
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.

1 participant