Moving threading setup to inside the function, to avoid threads at the same time locking up the connection test

This commit is contained in:
SimplifiedPrivacy 2026-11-02 08:31:01 -05:00
parent 5d858a70ab
commit 9ade83b454

View file

@ -51,13 +51,14 @@ def is_dns_problem_worker(url: str, queue: multiprocessing.Queue):
except Exception: except Exception:
queue.put((url, True)) # Failed queue.put((url, True)) # Failed
def test(connection_observer: Optional[ConnectionObserver], port: Optional[int] = None) -> Result: def test(connection_observer: Optional[ConnectionObserver], port: Optional[int] = None) -> Result:
endpoint_list = list(API_ENDPOINT_FORMATS.keys())[:MAX_ATTEMPTS] endpoint_list = list(API_ENDPOINT_FORMATS.keys())[:MAX_ATTEMPTS]
start_time = time.time()
total_budget = 6.0 total_budget = 6.0
# 1. DNS PRE-FLIGHT (Using Processes, not Threads) # 1. DNS PRE-FLIGHT
if port is None: if port is None:
dns_start = time.time()
dns_queue = multiprocessing.Queue() dns_queue = multiprocessing.Queue()
processes = [] processes = []
@ -70,10 +71,8 @@ def test(connection_observer: Optional[ConnectionObserver], port: Optional[int]
dns_timeout = 6 dns_timeout = 6
try: try:
# Wait for the first successful resolution while time.time() - dns_start < dns_timeout:
while time.time() - start_time < dns_timeout:
try: try:
# Non-blocking check of the queue
url, is_problem = dns_queue.get(timeout=0.1) url, is_problem = dns_queue.get(timeout=0.1)
if not is_problem: if not is_problem:
any_resolved = True any_resolved = True
@ -81,17 +80,21 @@ def test(connection_observer: Optional[ConnectionObserver], port: Optional[int]
except Empty: except Empty:
continue continue
finally: finally:
# We kill every single process immediately.
# The OS kernel wipes them from memory regardless of where they are stuck.
for p in processes: for p in processes:
p.terminate() p.terminate()
p.join() # Clean up the zombie process entry p.join()
if not any_resolved: if not any_resolved:
logger.error("Aggressive DNS check failed: No internet.") logger.error("Aggressive DNS check failed: No internet.")
return Result(valid=False, error_type=ResultError.CONNECTION, message="No internet connection.") return Result(valid=False, error_type=ResultError.CONNECTION, message="No internet connection.")
# 2. HTTP REQUEST PHASE (Using 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):
# Client and Transport INSIDE the thread
transport = None transport = None
if port: if port:
transport = httpx_socks.SyncProxyTransport( transport = httpx_socks.SyncProxyTransport(
@ -100,37 +103,30 @@ def test(connection_observer: Optional[ConnectionObserver], port: Optional[int]
proxy_port=port, proxy_port=port,
) )
# We use a Queue to get the first successful Result back from daemon threads
result_queue = Queue()
def attempt_request(url: str, index: int, client: httpx.Client):
try: try:
# Each thread gets its own client instance
with httpx.Client(transport=transport, http2=False, timeout=3.0) as client:
raw_response = client.get(url) raw_response = client.get(url)
filtered_response = classify_response(raw_response) filtered_response = classify_response(raw_response)
if filtered_response.valid: if filtered_response.valid:
ip_address = extract_ip_from_response(response=filtered_response.data, api_url=url) ip_address = extract_ip_from_response(response=filtered_response.data, api_url=url)
if ip_address: if ip_address:
result_queue.put(Result(valid=True, data=ip_address)) result_queue.put(Result(valid=True, data=ip_address))
return except Exception as e:
except Exception: logger.debug(f"Request to {url} failed: {e}")
pass
with httpx.Client(transport=transport, http2=False, timeout=3.0) as client: for url in endpoint_list:
for i, url in enumerate(endpoint_list): t = Thread(target=attempt_request, args=(url,), daemon=True)
# 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() t.start()
# Wait for the first success or the remaining budget # Wait for the first success or the budget
while True: while True:
elapsed = time.time() - start_time elapsed = time.time() - http_start
remaining = total_budget - elapsed remaining = total_budget - elapsed
if remaining <= 0: if remaining <= 0:
break break
try: try:
# Block for the remaining budget
return result_queue.get(timeout=remaining) return result_queue.get(timeout=remaining)
except Empty: except Empty:
break break