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
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
13 changes: 12 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
5 changes: 4 additions & 1 deletion src/agent_env/env/envs/computer/utils/site_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

async def _answer(self, data: bytes, addr) -> None:
reply = await self.answer(data, stream=False)
Expand Down
42 changes: 42 additions & 0 deletions tst/unit/env/envs/test_computer_site_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import contextlib
import dataclasses
import email.message
import gc
import io
import json
import os
Expand Down Expand Up @@ -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):
Expand Down
Loading