diff --git a/AGENTS.md b/AGENTS.md index b981213f9..daae0725a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -39,7 +39,7 @@ or one of them only where it is used. | Command | What it does | |---|---| -| `make lint` | `ruff check` over the repository, with the rules in `pyproject.toml` (pyflakes) | +| `make lint` | `ruff check` over the repository, with the rules in `pyproject.toml` (pyflakes and the async checks) | | `make unit-test` | `tst/unit` and `packages/agentenv-protocol/tests`, in parallel, no network | | `make int-test-fast` | `tst/integration` minus `int_test_slow`, in parallel; needs Docker and a local OCI registry (`docker run -d -p 5000:5000 public.ecr.aws/docker/library/registry:2`) | | `make int-test-slow` | the `int_test_slow` tests, serially (they build the same Docker tags from module fixtures and race under xdist) | diff --git a/pyproject.toml b/pyproject.toml index 472b5cc94..451575869 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -240,4 +240,15 @@ log_cli_level = "INFO" log_cli_format = "%(asctime)s %(levelname)s %(name)s: %(message)s" [tool.ruff.lint] -select = ["F"] +select = ["F", "ASYNC", "RUF006"] +# Left out: a timeout parameter is this codebase's style (ASYNC109); its polling loops wait on processes, files and +# other threads, which no asyncio.Event can signal (ASYNC110); its async file I/O is small writes and chunked streams +# (ASYNC230, ASYNC240). +ignore = ["ASYNC109", "ASYNC110", "ASYNC230", "ASYNC240"] + +[tool.ruff.lint.per-file-ignores] +# Tests block the loop on purpose, to stand in for slow work or to drive a subprocess. +"tst/**" = ["ASYNC2", "RUF006"] +"packages/agentenv-protocol/tests/**" = ["ASYNC2", "RUF006"] +# Lume's VM and its port relay are detached processes that outlive the call that starts them. +"src/agent_env/providers/sandbox_providers/lume/provider.py" = ["ASYNC220"] diff --git a/src/agent_env/env/envs/computer/utils/site_proxy.py b/src/agent_env/env/envs/computer/utils/site_proxy.py index b6926a218..de4bf4499 100644 --- a/src/agent_env/env/envs/computer/utils/site_proxy.py +++ b/src/agent_env/env/envs/computer/utils/site_proxy.py @@ -1036,12 +1036,15 @@ class Resolver(asyncio.DatagramProtocol): def __init__(self, config: Config): self.config = config self.transport: Optional[asyncio.DatagramTransport] = None + self._answering: set[asyncio.Task] = set() def connection_made(self, transport) -> None: self.transport = transport def datagram_received(self, data: bytes, addr) -> None: - asyncio.ensure_future(self._answer(data, addr)) + task = asyncio.ensure_future(self._answer(data, addr)) + self._answering.add(task) + task.add_done_callback(self._answering.discard) async def _answer(self, data: bytes, addr) -> None: reply = await self.answer(data, stream=False) diff --git a/tst/unit/env/envs/test_computer_site_proxy.py b/tst/unit/env/envs/test_computer_site_proxy.py index 3e8aabb5b..18fd597d2 100644 --- a/tst/unit/env/envs/test_computer_site_proxy.py +++ b/tst/unit/env/envs/test_computer_site_proxy.py @@ -9,6 +9,7 @@ import contextlib import dataclasses import email.message +import gc import io import json import os @@ -858,6 +859,47 @@ def datagram_received(self, data, addr): assert passed == [_query("example.org", 1)] and other[2:4] == b"\x81\x80" +@pytest.mark.asyncio +async def test_dns_over_udp_answers_every_query_a_client_sends_while_a_slow_nameserver_holds_them_open(): + loop = asyncio.get_running_loop() + passed_on, nameserver_delay_s = 20, 0.2 + + class Nameserver(asyncio.DatagramProtocol): + def connection_made(self, transport): + self.transport = transport + + def datagram_received(self, data, addr): + loop.call_later(nameserver_delay_s, self.transport.sendto, data[:2] + b"\x81\x80" + data[4:], addr) + + class Client(asyncio.DatagramProtocol): + def datagram_received(self, data, addr): + answered.append(data) + + answered = [] + upstream, _ = await loop.create_datagram_endpoint(Nameserver, local_addr=("127.0.0.1", 0)) + with pytest.MonkeyPatch.context() as patch: + patch.setattr(site_proxy, "NAMESERVER_PORT", upstream.get_extra_info("sockname")[1]) + config = Config(user=None, sites=[Site("chat", "http://gw/website/chat/", ["*.chat.test"])], + nameservers=["127.0.0.1"]) + server, _ = await loop.create_datagram_endpoint(lambda: site_proxy.Resolver(config), + local_addr=("127.0.0.1", 0)) + client, _ = await loop.create_datagram_endpoint(Client, remote_addr=server.get_extra_info("sockname")) + try: + deadline = loop.time() + 10 * nameserver_delay_s + client.sendto(_query("eu.chat.test", 1)) + for _ in range(passed_on): + client.sendto(_query("example.org", 1)) + while loop.time() < deadline and len(answered) < passed_on + 1: + gc.collect() + await asyncio.sleep(0.05) + finally: + for transport in (client, server, upstream): + transport.close() + + assert len(answered) == passed_on + 1 + assert sum(answer.endswith(socket.inet_aton("127.0.0.1")) for answer in answered) == 1 + + @pytest.mark.asyncio async def test_dns_passes_on_an_answer_larger_than_a_classic_datagram_whole(): class Nameserver(asyncio.DatagramProtocol):