Skip to content
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
14 changes: 8 additions & 6 deletions app/nullius/catalogue/benchmarks/osworld2/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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):
Expand Down
11 changes: 11 additions & 0 deletions app/tests/test_osworld2_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading