diff --git a/proofline/_generated/models/customer_review_detail.py b/proofline/_generated/models/customer_review_detail.py index 8fdbecaa..d624a7a0 100644 --- a/proofline/_generated/models/customer_review_detail.py +++ b/proofline/_generated/models/customer_review_detail.py @@ -86,7 +86,7 @@ class CustomerReviewDetail: recorded none. change_summary (ChangeSummary | None | Unset): What the change does and which files the review read at which depth - (§55). Null until the review's publication records it, and for a + (§55). Null until the review's publication records it, and for a review that is not of a pull request. Generated text has current credentials and recorded protected values withheld; missing or expired provenance withholds the generated text whole. diff --git a/proofline/_sync_http.py b/proofline/_sync_http.py index f8273f3f..c80af136 100644 --- a/proofline/_sync_http.py +++ b/proofline/_sync_http.py @@ -63,13 +63,36 @@ def addresses(self, timeout: float | None) -> list[str]: return self._addresses +_DNS_WORKER_LIMIT = 8 +_DNS_WORKER_SLOTS = threading.BoundedSemaphore(_DNS_WORKER_LIMIT) + + +def _run_bounded_lookup(lookup: Lookup) -> None: + try: + lookup.run() + finally: + _DNS_WORKER_SLOTS.release() + + def resolve(host: str, port: int, timeout: float | None) -> list[str]: try: ipaddress.ip_address(host) except ValueError: lookup = Lookup(host, port) - threading.Thread(target=lookup.run, name="proofline-dns", daemon=True).start() - return lookup.addresses(timeout) + deadline = monotonic() + timeout if timeout is not None else None + if not _DNS_WORKER_SLOTS.acquire(timeout=timeout): + raise httpcore.ConnectTimeout("DNS worker capacity was not available in time.") + try: + threading.Thread( + target=_run_bounded_lookup, + args=(lookup,), + name="proofline-dns", + daemon=True, + ).start() + except BaseException: + _DNS_WORKER_SLOTS.release() + raise + return lookup.addresses(max(0.0, deadline - monotonic()) if deadline is not None else None) return [host] diff --git a/tests/test_dns_worker_bounds.py b/tests/test_dns_worker_bounds.py new file mode 100644 index 00000000..fba1a4a0 --- /dev/null +++ b/tests/test_dns_worker_bounds.py @@ -0,0 +1,84 @@ +"""Diagnostic regression test for abandoned DNS workers.""" + +import socket +import threading + +import httpcore +import pytest + +from proofline._sync_http import resolve + + +def test_repeated_dns_timeouts_bound_outstanding_workers(monkeypatch): + release = threading.Event() + started = threading.Event() + workers = [] + lock = threading.Lock() + + def slow_dns(*args, **kwargs): + with lock: + workers.append(threading.current_thread()) + started.set() + release.wait(timeout=5) + return [ + ( + socket.AF_INET, + socket.SOCK_STREAM, + 6, + "", + ("127.0.0.1", 443), + ) + ] + + monkeypatch.setattr(socket, "getaddrinfo", slow_dns) + + try: + for _ in range(16): + with pytest.raises(httpcore.ConnectTimeout): + resolve("slow-dns.example.invalid", 443, 0.005) + + assert started.is_set() + active = sum(worker.is_alive() for worker in workers) + print(f"Outstanding DNS workers: {active}") + + # Exploratory safety threshold, not yet a product requirement. + assert active <= 8, f"{active} DNS workers remain active after timeouts" + finally: + release.set() + for worker in workers: + worker.join(timeout=2) + + +def test_dns_slot_wait_uses_shared_timeout(monkeypatch): + """Waiting for a DNS slot consumes the lookup timeout.""" + import importlib + import time + + module = importlib.import_module("proofline._sync_http") + received = [] + + class DelayedSlots: + def acquire(self, timeout=None): + time.sleep(0.05) + return True + + def release(self): + pass + + class FakeLookup: + def __init__(self, host, port): + pass + + def addresses(self, timeout): + received.append(timeout) + return ["127.0.0.1"] + + monkeypatch.setattr(module, "_DNS_WORKER_SLOTS", DelayedSlots()) + monkeypatch.setattr(module, "Lookup", FakeLookup) + + result = module.resolve("delayed-dns.example.invalid", 443, 0.2) + + assert result == ["127.0.0.1"] + assert len(received) == 1 + assert received[0] is not None + assert 0 < received[0] < 0.18