TargetClosedError during sleep after click-triggered download in PlaywrightController
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
MultimodalWebSurferor 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)), aTargetClosedErroris 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