Skip to content
Open
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
40 changes: 40 additions & 0 deletions tests/test_apps/conftest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,55 @@
"""shared fixtures for apps tests."""

import socket
import struct
import threading
from unittest.mock import AsyncMock

import pytest

from runpod.apps import gpu as _gpu

ACCEPT_POLL_SECONDS = 0.05
# struct linger {l_onoff=1, l_linger=0}: close() sends RST instead of FIN
RESET_ON_CLOSE = struct.pack("ii", 1, 0)


@pytest.fixture(autouse=True)
def offline_gpu_catalog(monkeypatch):
"""payload builders read the live gpu catalog; keep tests offline and
deterministic on the static pool table unless a test overrides it."""
monkeypatch.setattr(_gpu, "live_pool_members", lambda: None)
monkeypatch.setattr(_gpu, "live_pool_members_async", AsyncMock(return_value=None))


@pytest.fixture
def resetting_tcp_port():
"""A localhost port that resets every connection for the whole test.

pytest-asyncio's unused_tcp_port releases the port before the test runs,
so another process (a parallel test run, say) can bind it and receive the
requests meant to fail. This fixture keeps the port held by a listener
that accepts each connection and closes it with SO_LINGER 0, which sends
a TCP reset. A bound socket that never listens would be simpler, but
macOS drops connects to it until they time out instead of refusing them.
"""
stop = threading.Event()
with socket.socket() as listener:
listener.bind(("127.0.0.1", 0))
listener.listen()
listener.settimeout(ACCEPT_POLL_SECONDS)

def reset_connections():
while not stop.is_set():
try:
conn, _ = listener.accept()
except TimeoutError:
continue
conn.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, RESET_ON_CLOSE)
conn.close()

thread = threading.Thread(target=reset_connections, daemon=True)
thread.start()
yield listener.getsockname()[1]
stop.set()
thread.join()
20 changes: 17 additions & 3 deletions tests/test_apps/test_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,23 @@ async def test_client_4xx_not_retried(flaky_server):
assert flaky_server.state["hits"] == 1


async def test_connection_error_retried(unused_tcp_port):
# nothing listening: pure connection failures, all attempts consumed
url = f"http://127.0.0.1:{unused_tcp_port}/run"
def test_resetting_port_resets_connections(resetting_tcp_port):
with socket.create_connection(("127.0.0.1", resetting_tcp_port), timeout=2) as client:
with pytest.raises(ConnectionResetError):
client.sendall(b"GET /run HTTP/1.1\r\nHost: x\r\n\r\n")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Medium: If the listener resets before sendall, macOS raises BrokenPipeError, so this new test flakes even though the fixture worked; reproduced in the 5,000-connection stress run. Assert recv before sending, or accept BrokenPipeError as another reset outcome.

client.recv(1)


def test_resetting_port_cannot_be_taken_by_another_server(resetting_tcp_port):
"""Requests sent to the port must not reach a server started elsewhere,
such as a parallel test run that binds the same ephemeral port."""
with pytest.raises(OSError):
ThreadingHTTPServer(("127.0.0.1", resetting_tcp_port), BaseHTTPRequestHandler)


async def test_connection_error_retried(resetting_tcp_port):
# every connection is reset: connection failures, all attempts consumed
url = f"http://127.0.0.1:{resetting_tcp_port}/run"
with patch("runpod.apps.targets.RETRY_BASE_DELAY", 0.01):
with pytest.raises(aiohttp.ClientConnectionError):
await _request_json("GET", url, {}, timeout=5)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Medium: Resetting during asyncio transport setup can leave the old transport registered, so a later retry raises RuntimeError: File descriptor … is used by transport instead of ClientConnectionError; reproduced with the 100-call stress test and ResourceWarning: unclosed transport. Make this a deterministic retry unit test: stub the request to raise ClientConnectionError, assert RETRY_ATTEMPTS calls, and remove the RST fixture.

Expand Down
Loading