diff --git a/core/services/networking/general_connection_tools/ip.py b/core/services/networking/general_connection_tools/ip.py index 557124f..4126820 100644 --- a/core/services/networking/general_connection_tools/ip.py +++ b/core/services/networking/general_connection_tools/ip.py @@ -51,13 +51,14 @@ def is_dns_problem_worker(url: str, queue: multiprocessing.Queue): except Exception: queue.put((url, True)) # Failed + def test(connection_observer: Optional[ConnectionObserver], port: Optional[int] = None) -> Result: endpoint_list = list(API_ENDPOINT_FORMATS.keys())[:MAX_ATTEMPTS] - start_time = time.time() total_budget = 6.0 - # 1. DNS PRE-FLIGHT (Using Processes, not Threads) + # 1. DNS PRE-FLIGHT if port is None: + dns_start = time.time() dns_queue = multiprocessing.Queue() processes = [] @@ -70,10 +71,8 @@ def test(connection_observer: Optional[ConnectionObserver], port: Optional[int] dns_timeout = 6 try: - # Wait for the first successful resolution - while time.time() - start_time < dns_timeout: + while time.time() - dns_start < dns_timeout: try: - # Non-blocking check of the queue url, is_problem = dns_queue.get(timeout=0.1) if not is_problem: any_resolved = True @@ -81,59 +80,56 @@ def test(connection_observer: Optional[ConnectionObserver], port: Optional[int] except Empty: continue finally: - # We kill every single process immediately. - # The OS kernel wipes them from memory regardless of where they are stuck. for p in processes: p.terminate() - p.join() # Clean up the zombie process entry + p.join() if not any_resolved: logger.error("Aggressive DNS check failed: No internet.") return Result(valid=False, error_type=ResultError.CONNECTION, message="No internet connection.") - # 2. HTTP REQUEST PHASE (Using Daemon Threads) - transport = None - if port: - transport = httpx_socks.SyncProxyTransport( - proxy_type=ProxyType.SOCKS5, - proxy_host="127.0.0.1", - proxy_port=port, - ) - - # We use a Queue to get the first successful Result back from daemon threads + # 2. HTTP REQUEST PHASE + # Reset timer for the HTTP phase specifically so DNS time doesn't eat the request budget + http_start = time.time() result_queue = Queue() - def attempt_request(url: str, index: int, client: httpx.Client): + def attempt_request(url: str): + # Client and Transport INSIDE the thread + transport = None + if port: + transport = httpx_socks.SyncProxyTransport( + proxy_type=ProxyType.SOCKS5, + proxy_host="127.0.0.1", + proxy_port=port, + ) + try: - raw_response = client.get(url) - filtered_response = classify_response(raw_response) - if filtered_response.valid: - ip_address = extract_ip_from_response(response=filtered_response.data, api_url=url) - if ip_address: - result_queue.put(Result(valid=True, data=ip_address)) - return - except Exception: - pass + # Each thread gets its own client instance + with httpx.Client(transport=transport, http2=False, timeout=3.0) as client: + raw_response = client.get(url) + filtered_response = classify_response(raw_response) + if filtered_response.valid: + ip_address = extract_ip_from_response(response=filtered_response.data, api_url=url) + if ip_address: + result_queue.put(Result(valid=True, data=ip_address)) + except Exception as e: + logger.debug(f"Request to {url} failed: {e}") - with httpx.Client(transport=transport, http2=False, timeout=3.0) as client: - for i, url in enumerate(endpoint_list): - # daemon=True is CRITICAL. - # It tells Python: "If the main program exits, kill this thread immediately." - t = Thread(target=attempt_request, args=(url, i, client), daemon=True) - t.start() + for url in endpoint_list: + t = Thread(target=attempt_request, args=(url,), daemon=True) + t.start() - # Wait for the first success or the remaining budget - while True: - elapsed = time.time() - start_time - remaining = total_budget - elapsed - if remaining <= 0: - break - - try: - # Block for the remaining budget - return result_queue.get(timeout=remaining) - except Empty: - break + # Wait for the first success or the budget + while True: + elapsed = time.time() - http_start + remaining = total_budget - elapsed + if remaining <= 0: + break + + try: + return result_queue.get(timeout=remaining) + except Empty: + break logger.error(f"We went through {len(endpoint_list)} attempts and could not get a reply.") return Result(valid=False, error_type=ResultError.CONNECTION, message="Could not connect.")