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,59 +80,56 @@ 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
transport = None # Reset timer for the HTTP phase specifically so DNS time doesn't eat the request budget
if port: http_start = time.time()
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
result_queue = Queue() 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: try:
raw_response = client.get(url) # Each thread gets its own client instance
filtered_response = classify_response(raw_response) with httpx.Client(transport=transport, http2=False, timeout=3.0) as client:
if filtered_response.valid: raw_response = client.get(url)
ip_address = extract_ip_from_response(response=filtered_response.data, api_url=url) filtered_response = classify_response(raw_response)
if ip_address: if filtered_response.valid:
result_queue.put(Result(valid=True, data=ip_address)) ip_address = extract_ip_from_response(response=filtered_response.data, api_url=url)
return if ip_address:
except Exception: result_queue.put(Result(valid=True, data=ip_address))
pass 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 url in endpoint_list:
for i, url in enumerate(endpoint_list): t = Thread(target=attempt_request, args=(url,), daemon=True)
# daemon=True is CRITICAL. t.start()
# 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()
# 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
logger.error(f"We went through {len(endpoint_list)} attempts and could not get a reply.") 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.") return Result(valid=False, error_type=ResultError.CONNECTION, message="Could not connect.")