Close a browser instance and fall back to PID cleanup if needed.
(
self,
playwright,
browser,
context,
browser_pid: Optional[int] = None,
clear_slot_pid: bool = True,
)
| 1645 | } |
| 1646 | |
| 1647 | async def _close_browser( |
| 1648 | self, |
| 1649 | playwright, |
| 1650 | browser, |
| 1651 | context, |
| 1652 | browser_pid: Optional[int] = None, |
| 1653 | clear_slot_pid: bool = True, |
| 1654 | ): |
| 1655 | """Close a browser instance and fall back to PID cleanup if needed.""" |
| 1656 | is_shared_browser = any([ |
| 1657 | context is not None and context is self._shared_context, |
| 1658 | browser is not None and browser is self._shared_browser, |
| 1659 | playwright is not None and playwright is self._shared_playwright, |
| 1660 | ]) |
| 1661 | effective_pid = browser_pid or self._extract_browser_pid(browser) |
| 1662 | if clear_slot_pid and not effective_pid: |
| 1663 | effective_pid = self._shared_browser_pid or self._read_pid_file() |
| 1664 | if is_shared_browser: |
| 1665 | self._shared_playwright = None |
| 1666 | self._shared_browser = None |
| 1667 | self._shared_context = None |
| 1668 | self._shared_keepalive_page = None |
| 1669 | self._shared_browser_pid = None |
| 1670 | self._shared_proxy_url = None |
| 1671 | self._shared_bound_token_id = None |
| 1672 | self._shared_bound_cookie_signature = None |
| 1673 | try: |
| 1674 | if context: |
| 1675 | await asyncio.wait_for(context.close(), timeout=10) |
| 1676 | except Exception: |
| 1677 | pass |
| 1678 | try: |
| 1679 | if browser: |
| 1680 | await asyncio.wait_for(browser.close(), timeout=10) |
| 1681 | except Exception: |
| 1682 | pass |
| 1683 | try: |
| 1684 | if playwright: |
| 1685 | await asyncio.wait_for(playwright.stop(), timeout=10) |
| 1686 | except Exception: |
| 1687 | pass |
| 1688 | if effective_pid and not await self._wait_pid_exit(effective_pid, timeout_seconds=4): |
| 1689 | self._kill_pid(effective_pid, reason='close_timeout_or_orphan') |
| 1690 | await self._wait_pid_exit(effective_pid, timeout_seconds=2) |
| 1691 | if clear_slot_pid: |
| 1692 | self._write_pid_file(None) |
| 1693 | |
| 1694 | async def _wait_and_close_after_request( |
| 1695 | self, |
no test coverage detected