diff --git a/app/nullius/catalogue/benchmarks/osworld2/proxy.py b/app/nullius/catalogue/benchmarks/osworld2/proxy.py index ed79b1691..937278951 100644 --- a/app/nullius/catalogue/benchmarks/osworld2/proxy.py +++ b/app/nullius/catalogue/benchmarks/osworld2/proxy.py @@ -58,13 +58,13 @@ class NoAnswer(RuntimeError): def _docker(*args, env=None, until=None): - seconds = DOCKER_SECONDS if until is None else min(DOCKER_SECONDS, round(until - time.monotonic(), 1)) + seconds = DOCKER_SECONDS if until is None else min(DOCKER_SECONDS, until - time.monotonic()) if seconds <= 0: raise NoAnswer(f"docker {args[0]} was not asked: the time given for it ran out") try: return subprocess.run(["docker", *args], capture_output=True, text=True, timeout=seconds, env=env) except subprocess.TimeoutExpired: - raise NoAnswer(f"docker {args[0]} got no answer in {seconds:g} s: is its daemon running?") + raise NoAnswer(f"docker {args[0]} got no answer in {round(seconds, 1):g} s: is its daemon running?") except OSError: raise RuntimeError("no docker on this machine's PATH: the proxy needs it") @@ -152,15 +152,17 @@ def _ready(name, until): status, code, host = (seen.stdout.strip().split("\t") + ["", ""])[:3] if status not in ("running", "created"): raise RuntimeError(f"the proxy's container stopped, exit code {code or 'unknown'}") - if status == "running" and host: + left = until - time.monotonic() + if status == "running" and host and left > 0: try: - socket.create_connection((host, PORT), timeout=1).close() + socket.create_connection((host, PORT), timeout=min(1, left)).close() return host except OSError: pass - if time.monotonic() >= until: + left = until - time.monotonic() + if left <= 0: raise RuntimeError(f"the proxy at {host or 'no address'}:{PORT} {late}") - time.sleep(0.2) + time.sleep(min(0.2, left)) def start(dir, disk=None): diff --git a/app/tests/test_osworld2_proxy.py b/app/tests/test_osworld2_proxy.py index c699340c3..5592d813a 100644 --- a/app/tests/test_osworld2_proxy.py +++ b/app/tests/test_osworld2_proxy.py @@ -304,6 +304,17 @@ def test_a_proxy_that_stops_or_never_answers_is_an_error_and_is_removed(self): self.proxy.start(self.unit) self.assertIn("did not answer", str(raised.exception)) + def test_a_docker_call_gets_only_the_time_left_and_none_once_it_has_run_out(self): + self.proxy.time = types.SimpleNamespace(monotonic=lambda: 10.0) + with mock.patch.object(self.proxy.subprocess, "run") as run: + self.proxy._docker("ps", until=10.04) + self.assertEqual(run.call_count, 1) + self.assertTrue(0 < run.call_args.kwargs["timeout"] <= 0.04) + for until in (10.0, 9.96): + with self.assertRaises(self.proxy.NoAnswer): + self.proxy._docker("ps", until=until) + self.assertEqual(run.call_count, 1) + def test_a_refused_container_is_an_error_with_dockers_words(self): os.environ["FAKE_RUN_FAIL"] = "1" with self.assertRaises(RuntimeError) as raised, self.proxy.held(self.unit, True):