Skip to content

The agent that grows with you

fix(bootstrap): a racer bug surfaces instead of silently falling back to the serial connect

teknium1d029cddfd746
diff --git a/hermes_bootstrap.py b/hermes_bootstrap.py
index 405a328dcb3..313e08a6ac7 100644
--- a/hermes_bootstrap.py
+++ b/hermes_bootstrap.py
@@ -150,18 +150,14 @@ def _patch_urllib3_create_connection(module) -> None:
"""Point ``urllib3.util.connection.create_connection`` (its own serial walker) at the racer."""
if getattr(module.create_connection, "_hermes_happy_eyeballs", False):
return
- urllib3_original = module.create_connection
urllib3_sentinel = module._DEFAULT_TIMEOUT
def _urllib3_racer(address, timeout=urllib3_sentinel, source_address=None, socket_options=None):
effective = socket.getdefaulttimeout() if timeout is urllib3_sentinel else timeout
- try:
- return _happy_eyeballs_create_connection(
- address, effective, source_address=source_address, socket_options=tuple(socket_options or ()))
- except OSError:
- raise
- except Exception:
- return urllib3_original(address, timeout, source_address=source_address, socket_options=socket_options)
+ # OSError = every candidate failed (identical to the serial original); anything else is a
+ # racer bug and must surface rather than silently fall back to the serial stall.
+ return _happy_eyeballs_create_connection(
+ address, effective, source_address=source_address, socket_options=tuple(socket_options or ()))
_urllib3_racer._hermes_happy_eyeballs = True # type: ignore[attr-defined]
module.create_connection = _urllib3_racer
@@ -208,19 +204,15 @@ def install_happy_eyeballs_socket_connect() -> None:
"""
if getattr(socket.create_connection, "_hermes_happy_eyeballs", False):
return
- socket_original = socket.create_connection
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, timeout, source_address=source_address, all_errors=all_errors)
+ # OSError = every candidate failed (identical to the serial original); anything else is a
+ # racer bug and must surface rather than silently fall back to the serial stall.
+ return _happy_eyeballs_create_connection(address, effective, source_address=source_address)
_socket_racer._hermes_happy_eyeballs = True # type: ignore[attr-defined]
socket.create_connection = _socket_racer
diff --git a/tests/test_hermes_bootstrap.py b/tests/test_hermes_bootstrap.py
index 341b2786530..c1ea9aaec7a 100644
--- a/tests/test_hermes_bootstrap.py
+++ b/tests/test_hermes_bootstrap.py
@@ -468,3 +468,21 @@ class TestHappyEyeballsSocketConnect:
assert winner.timeout is None # sentinel resolves to the process default, like stock
assert clock[0] == hb._HAPPY_EYEBALLS_DELAY_SECONDS
assert sockets[0].closed is True and sockets[1] is winner
+
+ def test_racer_bug_raises_instead_of_falling_back_to_the_serial_walk(self, monkeypatch):
+ """A non-OSError from the racer is a bug in the racer, not a network outcome: it must
+ surface, never silently reroute the connect through the serial stock walker (which
+ would reintroduce the exact stall the racer exists to remove). OSError still means
+ "every candidate failed" and propagates unchanged."""
+ import urllib3.util.connection as urllib3_connection
+
+ _fresh_import()
+
+ def boom(*_args, **_kwargs):
+ raise RuntimeError("racer bug")
+
+ for racer in (socket.create_connection, urllib3_connection.create_connection):
+ assert getattr(racer, "_hermes_happy_eyeballs", False)
+ monkeypatch.setitem(racer.__globals__, "_happy_eyeballs_create_connection", boom)
+ with pytest.raises(RuntimeError, match="racer bug"):
+ racer(("127.0.0.1", 1), 1.0)