Skip to content

TargetClosedError during sleep after click-triggered download in PlaywrightController #6413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
oscarvdl opened this issue Apr 27, 2025 · 0 comments · May be fixed by #6415
Open

TargetClosedError during sleep after click-triggered download in PlaywrightController #6413

oscarvdl opened this issue Apr 27, 2025 · 0 comments · May be fixed by #6415

Comments

@oscarvdl
Copy link

oscarvdl commented Apr 27, 2025

What happened?

Describe the bug
When using MultimodalWebSurfer, if clicking an element triggers a file download, the agent sometimes crashes with playwright._impl._errors.TargetClosedError during the sleep() call in PlaywrightController.

Although the code checks page.is_closed() before calling page.wait_for_timeout(), the page might still close between the check and the await due to the download handling behavior.

In cases where the browser cancels navigation after triggering a download (e.g., with net::ERR_ABORTED), the page closes, and an unhandled TargetClosedError is raised.

To Reproduce
Steps to reproduce the behavior..

  • Extend MultimodalWebSurfer or use it as-is.
  • Navigate to a webpage with a download link (e.g., a direct JSON, PDF or CSV download link).
  • Invoke a click_id() action on the download button.
  • Observe that after the click, during the sleep (sleep(page, duration)), a TargetClosedError is thrown.

Expected behavior
After triggering a download and closing the page or context, the agent should handle the closure gracefully.
It should not crash but should either:

  • Log that the page was closed, or
  • Skip sleeping on closed pages.

Example

playwright._impl._errors.TargetClosedError: Page.wait_for_timeout: Target page, context or browser has been closed
  File "playwright_controller.py", line 86, in sleep
    await page.wait_for_timeout(duration * 1000)

Additional context
After a download is triggered, MultimodalWebSurfer continues with operations (like sleep) assuming the page still exists.
However, downloads can lead to canceled navigation and implicit page closure, which causes race conditions in Playwright.
Proposed solution: catch TargetClosedError specifically in sleep() and/or validate page state after downloads.

Which packages was the bug in?

Python Extensions (autogen-ext)

AutoGen library version.

Python 0.5.5

Other library version.

No response

Model used

gpt-4o

Model provider

Azure OpenAI

Other model provider

No response

Python version

3.12

.NET version

None

Operating system

Windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant