Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(smoke): wait for target IO reclamation within fixed resource budgets
ldm095094ddb6062
diff --git a/moli-cdp-smoke/moli_cdp_smoke/groups/target_lifecycle.py b/moli-cdp-smoke/moli_cdp_smoke/groups/target_lifecycle.pyindex 92d14752b..93b8cf7e5 100644--- a/moli-cdp-smoke/moli_cdp_smoke/groups/target_lifecycle.py+++ b/moli-cdp-smoke/moli_cdp_smoke/groups/target_lifecycle.py@@ -5,14 +5,12 @@ import sysfrom pathlib import Pathfrom typing import Any-from ..assertions import SmokeError, assert_equal, record+from ..assertions import SmokeError, assert_equal, record, wait_untilfrom ..raw_cdp import RawCdpClient, connect_raw_cdpfrom ..serve import BrowserProcess-def process_resources(pid: int, profile_dir: str | None = None) -> dict[str, int] | None:- if not sys.platform.startswith("linux"):- return None+def process_descriptors(pid: int) -> dict[int, str]:# Inspect only the managed server, not this client or other smoke workers.# A missing/inaccessible managed process is a failure, not a skipped check.descriptors: dict[int, str] = {}@@ -22,6 +20,13 @@ def process_resources(pid: int, profile_dir: str | None = None) -> dict[str, intexcept FileNotFoundError:# A transient socket can close between directory listing/readlink.continue+ return descriptors+++def process_resources(pid: int, profile_dir: str | None = None) -> dict[str, int] | None:+ if not sys.platform.startswith("linux"):+ return None+ descriptors = process_descriptors(pid)return {"fds": len(descriptors),"profileFileFds": sum(value.startswith(profile_dir.rstrip("/") + "/")@@ -48,6 +53,37 @@ def assert_resources_bounded(baseline: dict[str, int], current: dict[str, int])raise SmokeError(f"target teardown leaked {key}: baseline={baseline}, current={current}")+async def wait_for_resources_bounded(+ pid: int, profile_dir: str | None, baseline: dict[str, int],+) -> dict[str, int]:+ # Target.targetDestroyed retires the target registry entry. Its IO resources+ # can still be closing asynchronously; require reclamation within the same+ # ten-second bound used by target commands, without relaxing any budget.+ current: dict[str, int] | None = None+ violation: SmokeError | None = None++ def reclaimed() -> bool:+ nonlocal current, violation+ current = process_resources(pid, profile_dir)+ if current is None:+ raise RuntimeError("managed process resource sampling became unavailable")+ try:+ assert_resources_bounded(baseline, current)+ except SmokeError as error:+ violation = error+ return False+ return True++ try:+ await wait_until(reclaimed, "target IO resource reclamation")+ except SmokeError as error:+ if violation is not None:+ raise violation from error+ raise+ assert current is not None+ return current++class LifecycleProbe:def __init__(self, client: RawCdpClient) -> None:self.client = client@@ -148,6 +184,7 @@ async def run_target_lifecycle_group(baseline = process_resources(pid, profile_dir) if pid is not None else Nonerecord(results, "target_lifecycle_baseline", {"pid": pid, "resources": baseline,+ "descriptors": process_descriptors(pid) if baseline is not None else None,"fdBudget": "non-profile-fds" if profile_dir else "all-fds","fdSampling": ("external-endpoint-without-owned-pid" if pid is None else"linux-proc" if baseline is not None else "unavailable-on-this-platform"),@@ -165,7 +202,25 @@ async def run_target_lifecycle_group(print(f"[moli-cdp-smoke] target-lifecycle {mode} {index + 1}/{count} {current}",file=sys.stderr, flush=True)if baseline is not None and current is not None:- assert_resources_bounded(baseline, current)+ try:+ assert_resources_bounded(baseline, current)+ except SmokeError:+ record(results, "target_lifecycle_reclaim_pending", {+ "mode": mode, "closed": index + 1, "resources": current,+ "descriptors": process_descriptors(pid),+ })+ try:+ current = await wait_for_resources_bounded(pid, profile_dir, baseline)+ except SmokeError:+ record(results, "target_lifecycle_reclaim_failed", {+ "mode": mode, "closed": index + 1,+ "resources": process_resources(pid, profile_dir),+ "descriptors": process_descriptors(pid),+ })+ raise+ record(results, "target_lifecycle_reclaimed", {+ "mode": mode, "closed": index + 1, "resources": current,+ })await probe.navigate(f"{fixture}/plain?after-{mode}-{count}")sentinel = await probe.call("Runtime.evaluate", {diff --git a/moli-cdp-smoke/moli_cdp_smoke/serve.py b/moli-cdp-smoke/moli_cdp_smoke/serve.pyindex 7a00ed670..0a2703f94 100644--- a/moli-cdp-smoke/moli_cdp_smoke/serve.py+++ b/moli-cdp-smoke/moli_cdp_smoke/serve.py@@ -165,6 +165,9 @@ async def start_browser(port: int = 0, *, extra_args: tuple[str, ...] = ()) -> Bcommand = [str(binary), "--headless=new", "--no-sandbox", "--disable-dev-shm-usage","--disable-back-forward-cache",+ # Keep profile services from adding unrelated network descriptors during+ # local-fixture resource accounting (also used by Playwright's launcher).+ "--disable-background-networking", "--disable-component-update", "--disable-sync","--no-first-run", "--no-default-browser-check", "--remote-allow-origins=*","--remote-debugging-address=127.0.0.1", f"--remote-debugging-port={port}",f"--user-data-dir={profile}", *extra_args, "about:blank",diff --git a/moli-cdp-smoke/tests/test_target_lifecycle.py b/moli-cdp-smoke/tests/test_target_lifecycle.pyindex 7314f3194..3e159af62 100644--- a/moli-cdp-smoke/tests/test_target_lifecycle.py+++ b/moli-cdp-smoke/tests/test_target_lifecycle.py@@ -9,7 +9,7 @@ from unittest.mock import patchfrom moli_cdp_smoke.assertions import SmokeErrorfrom moli_cdp_smoke.groups.target_lifecycle import (- LifecycleProbe, assert_resources_bounded, process_resources,+ LifecycleProbe, assert_resources_bounded, process_resources, wait_for_resources_bounded,)@@ -57,3 +57,38 @@ class TargetLifecycleTests(unittest.TestCase):patch("moli_cdp_smoke.groups.target_lifecycle.Path.iterdir", side_effect=FileNotFoundError):with self.assertRaises(FileNotFoundError):process_resources(1234)+++class ResourceReclamationTests(unittest.IsolatedAsyncioTestCase):+ baseline = {"fds": 24, "eventpoll": 7, "eventfd": 4, "threads": 20}++ async def poll_twice(self, predicate, label):+ for _ in range(2):+ if predicate():+ return+ raise SmokeError(f"timed out waiting for {label}")++ async def test_target_destruction_can_precede_io_reclamation(self) -> None:+ pending = dict(self.baseline, fds=40)+ with patch("moli_cdp_smoke.groups.target_lifecycle.process_resources",+ side_effect=[pending, self.baseline]), \+ patch("moli_cdp_smoke.groups.target_lifecycle.wait_until", self.poll_twice):+ self.assertEqual(await wait_for_resources_bounded(1234, None, self.baseline), self.baseline)++ async def test_persistent_leaks_still_fail_the_original_fixed_budgets(self) -> None:+ for key, value in (("fds", 224), ("eventpoll", 10), ("eventfd", 7), ("threads", 23)):+ with self.subTest(key=key), \+ patch("moli_cdp_smoke.groups.target_lifecycle.process_resources",+ return_value=dict(self.baseline, **{key: value})), \+ patch("moli_cdp_smoke.groups.target_lifecycle.wait_until", self.poll_twice):+ with self.assertRaisesRegex(SmokeError, f"target teardown leaked {key}") as error:+ await wait_for_resources_bounded(1234, None, self.baseline)+ self.assertIn("timed out", str(error.exception.__cause__))++ async def test_missing_or_inaccessible_managed_process_is_not_retried(self) -> None:+ for error in (FileNotFoundError, PermissionError):+ with self.subTest(error=error), \+ patch("moli_cdp_smoke.groups.target_lifecycle.process_resources", side_effect=error) as sample:+ with self.assertRaises(error):+ await wait_for_resources_bounded(1234, None, self.baseline)+ sample.assert_called_once_with(1234, None)