Skip to content

Fix: Catch TargetClosedError in PlaywrightController.sleep() to avoid crashes on download-triggered page closes #6415

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
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from typing import Any, Callable, Dict, Optional, Tuple, Union, cast

from playwright._impl._errors import Error as PlaywrightError
from playwright._impl._errors import TimeoutError
from playwright._impl._errors import TimeoutError, TargetClosedError
from playwright.async_api import Download, Page

from ._types import (
Expand Down Expand Up @@ -83,7 +83,11 @@
duration (Union[int, float]): The duration to sleep in milliseconds.
"""
assert page is not None
await page.wait_for_timeout(duration * 1000)
try:
await page.wait_for_timeout(duration * 1000)
except (TargetClosedError, PlaywrightError):

Check warning on line 88 in python/packages/autogen-ext/src/autogen_ext/agents/web_surfer/playwright_controller.py

View check run for this annotation

Codecov / codecov/patch

python/packages/autogen-ext/src/autogen_ext/agents/web_surfer/playwright_controller.py#L88

Added line #L88 was not covered by tests
# Target closed (browser, context, or page) — safely skip sleeping
print("[PlaywrightController] sleep skipped: Target closed during sleep.")

Check warning on line 90 in python/packages/autogen-ext/src/autogen_ext/agents/web_surfer/playwright_controller.py

View check run for this annotation

Codecov / codecov/patch

python/packages/autogen-ext/src/autogen_ext/agents/web_surfer/playwright_controller.py#L90

Added line #L90 was not covered by tests

async def get_interactive_rects(self, page: Page) -> Dict[str, InteractiveRegion]:
"""
Expand Down
Loading