Skip to content

The agent that grows with you

fix(agent): keep socket racer signature-complete with stock create_connection

liuhao10245a88baa79b97
diff --git a/agent/process_bootstrap.py b/agent/process_bootstrap.py
index 3fdb2507946..03de4fc9317 100644
--- a/agent/process_bootstrap.py
+++ b/agent/process_bootstrap.py
@@ -246,14 +246,17 @@ def install_happy_eyeballs_socket_connect() -> None:
_socket_original = socket.create_connection
- def _socket_racer(address, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, source_address=None):
- effective = None if timeout is socket._GLOBAL_DEFAULT_TIMEOUT else timeout
+ def _socket_racer(address, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, source_address=None, *, all_errors=False):
+ # Stock create_connection leaves the sentinel alone, so the socket keeps the
+ # process default from socket.setdefaulttimeout(); the racer re-applies the
+ # timeout on the winner, so it must resolve the sentinel the same way.
+ effective = socket.getdefaulttimeout() if timeout is socket._GLOBAL_DEFAULT_TIMEOUT else timeout
try:
return _happy_eyeballs_create_connection(address, effective, source_address=source_address)
except OSError:
raise # every candidate failed — identical semantics to the serial original
except Exception:
- return _socket_original(address, effective, source_address=source_address)
+ return _socket_original(address, timeout, source_address=source_address, all_errors=all_errors)
socket.create_connection = _socket_racer
@@ -263,7 +266,7 @@ def install_happy_eyeballs_socket_connect() -> None:
_urllib3_original = _urllib3_connection.create_connection
def _urllib3_racer(address, timeout=_urllib3_sentinel, source_address=None, socket_options=None):
- effective = None if timeout is _urllib3_sentinel else timeout
+ effective = socket.getdefaulttimeout() if timeout is _urllib3_sentinel else timeout
try:
return _happy_eyeballs_create_connection(
address, effective, source_address=source_address,
@@ -272,7 +275,7 @@ def install_happy_eyeballs_socket_connect() -> None:
raise
except Exception:
return _urllib3_original(
- address, effective, source_address=source_address, socket_options=socket_options)
+ address, timeout, source_address=source_address, socket_options=socket_options)
_urllib3_connection.create_connection = _urllib3_racer
except Exception:
diff --git a/tests/agent/test_codex_happy_eyeballs.py b/tests/agent/test_codex_happy_eyeballs.py
index 552e23d9704..0196f77fb91 100644
--- a/tests/agent/test_codex_happy_eyeballs.py
+++ b/tests/agent/test_codex_happy_eyeballs.py
@@ -440,7 +440,7 @@ def test_installed_socket_connect_races_past_blackholed_ipv6(
)
assert winner.family == socket.AF_INET
- assert winner.timeout is None # sentinel → blocking socket, like the stock connect
+ assert winner.timeout is None # sentinel resolves to the process default (None here), like stock
assert clock[0] == process_bootstrap._HAPPY_EYEBALLS_DELAY_SECONDS
assert sockets[0].closed is True
assert sockets[1] is winner
@@ -468,3 +468,35 @@ def test_installed_racer_serves_http_client_and_urllib3_connects(restored_socket
http_conn.close()
urllib3_conn.close()
listener.close()
+
+
+def test_installed_racer_honours_process_default_timeout_on_sentinel(restored_socket_connect):
+ # Stock create_connection leaves the sentinel alone, so the socket keeps the
+ # process default set by socket.setdefaulttimeout(); the racer re-applies the
+ # timeout on the winner and must resolve the sentinel to that same default
+ # instead of forcing a blocking socket.
+ listener = socket.socket()
+ listener.bind(("127.0.0.1", 0))
+ listener.listen(4)
+ port = listener.getsockname()[1]
+
+ process_bootstrap.install_happy_eyeballs_socket_connect()
+ socket.setdefaulttimeout(5.0)
+ try:
+ winner = socket.create_connection(("127.0.0.1", port), socket._GLOBAL_DEFAULT_TIMEOUT)
+ try:
+ assert winner.gettimeout() == 5.0
+ finally:
+ winner.close()
+ finally:
+ socket.setdefaulttimeout(None)
+ listener.close()
+
+
+def test_installed_racer_accepts_all_errors_keyword(restored_socket_connect):
+ # socket.create_connection gained the keyword-only all_errors parameter in
+ # Python 3.11 (the repo floor); forwarding it through the installed racer must
+ # not fail with a TypeError before the connect is even attempted.
+ process_bootstrap.install_happy_eyeballs_socket_connect()
+ with pytest.raises(OSError):
+ socket.create_connection(("127.0.0.1", 1), 1.0, None, all_errors=True)