Skip to content
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

shu/do not raise TargetClosedError in cleanup #1220

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions skyvern/forge/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,11 @@ async def execute_task_webhook(
async def cleanup_browser_and_create_artifacts(
self, close_browser_on_completion: bool, last_step: Step, task: Task
) -> None:
"""
Developer notes: we should not expect any exception to be raised here.
This function should handle exceptions gracefully.
If errors are raised and not caught inside this function, please catch and handle them.
"""
# We need to close the browser even if there is no webhook callback url or api key
browser_state = await app.BROWSER_MANAGER.cleanup_for_task(task.task_id, close_browser_on_completion)
if browser_state:
Expand Down
21 changes: 15 additions & 6 deletions skyvern/webeye/browser_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,20 +514,29 @@ async def close(self, close_browser_on_completion: bool = True) -> None:
async with asyncio.timeout(BROWSER_CLOSE_TIMEOUT):
if self.browser_context and close_browser_on_completion:
LOG.info("Closing browser context and its pages")
await self.browser_context.close()
try:
await self.browser_context.close()
except Exception:
LOG.warning("Failed to close browser context", exc_info=True)
LOG.info("Main browser context and all its pages are closed")
if self.browser_cleanup is not None:
self.browser_cleanup()
LOG.info("Main browser cleanup is excuted")
try:
self.browser_cleanup()
LOG.info("Main browser cleanup is excuted")
except Exception:
LOG.warning("Failed to execute browser cleanup", exc_info=True)
except asyncio.TimeoutError:
LOG.error("Timeout to close browser context, going to stop playwright directly")

try:
async with asyncio.timeout(BROWSER_CLOSE_TIMEOUT):
if self.pw and close_browser_on_completion:
LOG.info("Stopping playwright")
await self.pw.stop()
LOG.info("Playwright is stopped")
try:
LOG.info("Stopping playwright")
await self.pw.stop()
LOG.info("Playwright is stopped")
except Exception:
LOG.warning("Failed to stop playwright", exc_info=True)
except asyncio.TimeoutError:
LOG.error("Timeout to close playwright, might leave the broswer opening forever")

Expand Down
4 changes: 4 additions & 0 deletions skyvern/webeye/browser_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ async def close(cls) -> None:
LOG.info("BrowserManger is closed")

async def cleanup_for_task(self, task_id: str, close_browser_on_completion: bool = True) -> BrowserState | None:
"""
Developer notes: handle errors here. Do not raise error from this function.
If error occurs, log it and address the cleanup error.
"""
LOG.info("Cleaning up for task")
browser_state_to_close = self.pages.pop(task_id, None)
if browser_state_to_close:
Expand Down
Loading