Skip to content

Commit 794bc96

Browse files
committed
Fix: Run unasync and ruff formatting
1 parent 3d39b82 commit 794bc96

3 files changed

Lines changed: 145 additions & 7 deletions

File tree

httpcore/_sync/socks_proxy.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ def _init_socks5_connection(
6060
stream.write(outgoing_bytes, timeout=timeout) # <--- FIX 2: Pass timeout
6161

6262
# Auth method response
63-
incoming_bytes = stream.read(max_bytes=4096, timeout=timeout) # <--- FIX 3: Pass timeout
63+
incoming_bytes = stream.read(
64+
max_bytes=4096, timeout=timeout
65+
) # <--- FIX 3: Pass timeout
6466
response = conn.receive_data(incoming_bytes)
6567
assert isinstance(response, socksio.socks5.SOCKS5AuthReply)
6668
if response.method != auth_method:
@@ -76,10 +78,12 @@ def _init_socks5_connection(
7678
username, password = auth
7779
conn.send(socksio.socks5.SOCKS5UsernamePasswordRequest(username, password))
7880
outgoing_bytes = conn.data_to_send()
79-
stream.write(outgoing_bytes, timeout=timeout) # <--- FIX 4: Pass timeout
81+
stream.write(outgoing_bytes, timeout=timeout) # <--- FIX 4: Pass timeout
8082

8183
# Username/password response
82-
incoming_bytes = stream.read(max_bytes=4096, timeout=timeout) # <--- FIX 5: Pass timeout
84+
incoming_bytes = stream.read(
85+
max_bytes=4096, timeout=timeout
86+
) # <--- FIX 5: Pass timeout
8387
response = conn.receive_data(incoming_bytes)
8488
assert isinstance(response, socksio.socks5.SOCKS5UsernamePasswordReply)
8589
if not response.success:
@@ -92,10 +96,12 @@ def _init_socks5_connection(
9296
)
9397
)
9498
outgoing_bytes = conn.data_to_send()
95-
stream.write(outgoing_bytes, timeout=timeout) # <--- FIX 6: Pass timeout
99+
stream.write(outgoing_bytes, timeout=timeout) # <--- FIX 6: Pass timeout
96100

97101
# Connect response
98-
incoming_bytes = stream.read(max_bytes=4096, timeout=timeout) # <--- FIX 7: Pass timeout
102+
incoming_bytes = stream.read(
103+
max_bytes=4096, timeout=timeout
104+
) # <--- FIX 7: Pass timeout
99105
response = conn.receive_data(incoming_bytes)
100106
assert isinstance(response, socksio.socks5.SOCKS5Reply)
101107
if response.reply_code != socksio.socks5.SOCKS5ReplyCode.SUCCEEDED:
@@ -211,7 +217,7 @@ def handle_request(self, request: Request) -> Response:
211217
"host": self._remote_origin.host.decode("ascii"),
212218
"port": self._remote_origin.port,
213219
"auth": self._proxy_auth,
214-
"timeout": timeout, # <--- FIX 8: Pass timeout to handshake
220+
"timeout": timeout, # <--- FIX 8: Pass timeout argument
215221
}
216222
with Trace(
217223
"setup_socks5_connection", logger, request, kwargs
@@ -313,4 +319,4 @@ def info(self) -> str:
313319
return self._connection.info()
314320

315321
def __repr__(self) -> str:
316-
return f"<{self.__class__.__name__} [{self.info()}]>"
322+
return f"<{self.__class__.__name__} [{self.info()}]>"

reproduce_httpcore.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import httpcore
2+
import socket
3+
import threading
4+
import time
5+
6+
# --- Same Server Setup as before ---
7+
TIMEOUT = 2.0
8+
HANG_TIME = 20
9+
10+
def get_free_port():
11+
with socket.socket() as s:
12+
s.bind(('', 0))
13+
return s.getsockname()[1]
14+
15+
def blackhole_proxy_server(port, stop_event):
16+
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
17+
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
18+
server.bind(('127.0.0.1', port))
19+
server.listen(1)
20+
server.settimeout(1.0)
21+
while not stop_event.is_set():
22+
try:
23+
client, _ = server.accept()
24+
time.sleep(HANG_TIME) # Hang the handshake
25+
client.close()
26+
except socket.timeout: continue
27+
except: break
28+
server.close()
29+
30+
# --- The Test ---
31+
def run_test():
32+
proxy_port = get_free_port()
33+
stop_event = threading.Event()
34+
t = threading.Thread(target=blackhole_proxy_server, args=(proxy_port, stop_event))
35+
t.start()
36+
time.sleep(0.5)
37+
38+
print(f"[*] Testing httpcore SOCKSProxy with {TIMEOUT}s timeout...")
39+
start_time = time.time()
40+
41+
# We use the low-level SOCKSProxy directly
42+
with httpcore.SOCKSProxy(
43+
proxy_url=f"socks5://127.0.0.1:{proxy_port}"
44+
) as pool:
45+
try:
46+
# We assume httpcore 1.0+ style request
47+
pool.request(
48+
"GET",
49+
"http://example.com",
50+
extensions={'timeout': {'connect': TIMEOUT, 'read': TIMEOUT}}
51+
)
52+
except httpcore.TimeoutException:
53+
print("[SUCCESS] Caught timeout correctly!")
54+
except Exception as e:
55+
print(f"[ERROR] {e}")
56+
finally:
57+
duration = time.time() - start_time
58+
print(f"[*] Duration: {duration:.2f}s")
59+
stop_event.set()
60+
t.join()
61+
62+
if __name__ == "__main__":
63+
run_test()

reproduce_httpcore_async.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import httpcore
2+
import socket
3+
import threading
4+
import time
5+
import asyncio
6+
7+
# --- Server Setup (Same as before) ---
8+
HANG_TIME = 20
9+
TIMEOUT = 2.0
10+
11+
def get_free_port():
12+
with socket.socket() as s:
13+
s.bind(('', 0))
14+
return s.getsockname()[1]
15+
16+
def blackhole_proxy_server(port, stop_event):
17+
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
18+
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
19+
server.bind(('127.0.0.1', port))
20+
server.listen(1)
21+
server.settimeout(1.0)
22+
23+
while not stop_event.is_set():
24+
try:
25+
client, _ = server.accept()
26+
# print("[Server] Accepted connection, sleeping...")
27+
time.sleep(HANG_TIME)
28+
client.close()
29+
except socket.timeout: continue
30+
except: break
31+
server.close()
32+
33+
# --- Async Test ---
34+
async def run_async_test(port):
35+
print(f"[*] Testing ASYNC httpcore SOCKSProxy with {TIMEOUT}s timeout...")
36+
start_time = time.time()
37+
38+
async with httpcore.AsyncSOCKSProxy(
39+
proxy_url=f"socks5://127.0.0.1:{port}"
40+
) as pool:
41+
try:
42+
await pool.request(
43+
"GET",
44+
"http://example.com",
45+
extensions={'timeout': {'connect': TIMEOUT, 'read': TIMEOUT}}
46+
)
47+
except httpcore.TimeoutException:
48+
print("[SUCCESS] Caught timeout correctly!")
49+
except Exception as e:
50+
print(f"[ERROR] {type(e).__name__}: {e}")
51+
finally:
52+
duration = time.time() - start_time
53+
print(f"[*] Duration: {duration:.2f}s")
54+
55+
def main():
56+
port = get_free_port()
57+
stop_event = threading.Event()
58+
t = threading.Thread(target=blackhole_proxy_server, args=(port, stop_event))
59+
t.start()
60+
time.sleep(0.5)
61+
62+
try:
63+
asyncio.run(run_async_test(port))
64+
finally:
65+
stop_event.set()
66+
t.join()
67+
68+
if __name__ == "__main__":
69+
main()

0 commit comments

Comments
 (0)