perf(fast-browser): bound readiness polling and verify asynchronous tab close

This commit is contained in:
jb
2026-09-21 13:42:57 -04:00
parent fe643e2d8d
commit 2dc359c48d
2 changed files with 225 additions and 9 deletions
+26 -8
View File
@@ -824,7 +824,18 @@ class RealBrowserBackend:
return self._helpers.cdp("Page.reload")
def wait_ready(self, timeout: float = NAVIGATION_TIMEOUT_SECONDS) -> bool:
return bool(self._helpers.wait_for_load(timeout=timeout))
# Keep the harness' exact readyState query, but avoid its coarse 300ms
# sleep. This is only a readiness read; it never dispatches navigation.
if timeout <= 0:
return False
deadline = time.monotonic() + timeout
while True:
if self._helpers.js("document.readyState") == "complete":
return True
remaining = deadline - time.monotonic()
if remaining <= 0:
return False
time.sleep(min(0.025, remaining))
def visible_text_content(self) -> str:
# This is a fixed, worker-owned read operation. No caller expression,
@@ -1708,13 +1719,20 @@ class BrowserRuntimeWorker(BrowserWorker):
raise self._fail("closeTab", "aborted", "close", "tab close was cancelled",
mutation_state="unknown" if self._mutation_dispatched else "not_attempted")
# Do not assume a close acknowledgement means the target disappeared;
# verify the current target inventory before reporting a verified close.
try:
self._refresh_tabs()
except BrowserOperationError:
raise self._fail("closeTab", "worker_failure", "verify", "tab close could not be verified", mutation_state="unknown")
if tab.target_id in self._target_to_tab:
raise self._fail("closeTab", "worker_failure", "verify", "tab close could not be verified", mutation_state="unknown")
# verify inventories until the target is gone or the existing action
# budget expires. The close mutation itself is dispatched exactly once.
deadline = time.monotonic() + ACTION_TIMEOUT_SECONDS
while True:
try:
self._refresh_tabs()
except BrowserOperationError:
raise self._fail("closeTab", "worker_failure", "verify", "tab close could not be verified", mutation_state="unknown")
if tab.target_id not in self._target_to_tab:
break
remaining = deadline - time.monotonic()
if remaining <= 0:
raise self._fail("closeTab", "worker_failure", "verify", "tab close could not be verified", mutation_state="unknown")
time.sleep(min(0.025, remaining))
self._target_to_tab.pop(tab.target_id, None)
self._tabs.pop(tab.tab_id, None)
if self._selected_tab_id == tab.tab_id: