From 85826afc5f1a3317ba47ff2d5e87f3e6b2f1c592 Mon Sep 17 00:00:00 2001 From: umar Date: Mon, 16 Nov 2026 12:57:50 +0100 Subject: [PATCH 1/2] fix: bound DNS lookup workers and add regression test --- proofline/_sync_http.py | 26 ++++++++++++++++- tests/test_dns_worker_bounds.py | 51 +++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 tests/test_dns_worker_bounds.py diff --git a/proofline/_sync_http.py b/proofline/_sync_http.py index f8273f3f..7ff9fc5a 100644 --- a/proofline/_sync_http.py +++ b/proofline/_sync_http.py @@ -63,12 +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() + 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(timeout) return [host] diff --git a/tests/test_dns_worker_bounds.py b/tests/test_dns_worker_bounds.py new file mode 100644 index 00000000..df297537 --- /dev/null +++ b/tests/test_dns_worker_bounds.py @@ -0,0 +1,51 @@ +"""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) From e1720408a8f4badabfe06ee24249fde240c78867 Mon Sep 17 00:00:00 2001 From: umar Date: Tue, 17 Nov 2026 21:35:57 +0100 Subject: [PATCH 2/2] fix: bound DNS workers and add timeout regression tests --- .../models/customer_review_detail.py | 2 +- proofline/_sync_http.py | 7 ++-- tests/test_dns_worker_bounds.py | 41 +++++++++++++++++-- 3 files changed, 41 insertions(+), 9 deletions(-) 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 7ff9fc5a..c80af136 100644 --- a/proofline/_sync_http.py +++ b/proofline/_sync_http.py @@ -79,10 +79,9 @@ def resolve(host: str, port: int, timeout: float | None) -> list[str]: ipaddress.ip_address(host) except ValueError: lookup = Lookup(host, port) + 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." - ) + raise httpcore.ConnectTimeout("DNS worker capacity was not available in time.") try: threading.Thread( target=_run_bounded_lookup, @@ -93,7 +92,7 @@ def resolve(host: str, port: int, timeout: float | None) -> list[str]: except BaseException: _DNS_WORKER_SLOTS.release() raise - return lookup.addresses(timeout) + 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 index df297537..fba1a4a0 100644 --- a/tests/test_dns_worker_bounds.py +++ b/tests/test_dns_worker_bounds.py @@ -1,4 +1,4 @@ -"""Diagnostic regression test for abandoned DNS workers.""" +"""Diagnostic regression test for abandoned DNS workers.""" import socket import threading @@ -42,10 +42,43 @@ def slow_dns(*args, **kwargs): 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" - ) + 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