mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-17 11:22:02 +03:00
fbbab5fb37
The three loopback callback listeners (tools/mcp_oauth.py, hermes_cli/auth_device_flow.py, tools/connectors/mcp_oauth.py) were single-threaded HTTPServers. A browser's idle speculative connection parked the serve loop ahead of the real redirect. The 2 s read timeout on the handlers bounded that stall but caused a new problem: any client that sends its request line more than 2 s after connecting (an SSH-tunnelled callback, for example) got its connection closed, which worked on main. And with N idle preconnects queued in front of the redirect, the stall was still about 2 s x N. Each listener now runs one daemon thread per connection (ThreadingMixIn with daemon_threads = True). An idle socket holds only its own thread, the serve loop keeps accepting connections, and shutdown()/server_close() never wait on it. The per-connection read timeout is now 60 s. It only limits how long a silent socket keeps its thread, and a slow but real client still gets through. Since handlers can now run at the same time, the check-then-write on the shared result is atomic. In tools/mcp_oauth.py, a per-flow lock (the handler's result_lock) guards the claim for the HTTP handler and for the paste reader, and the first terminal result still wins. The provider-login handler writes code, state and error under its own lock in a single update. The connectors receiver already serializes through the flow's deliver_callback lock. Tests: two idle preconnects ahead of the redirect, a client that sends its request after 2.5 s (MCP and provider login), and two concurrent callbacks with one consistent winner.
59 lines
2.8 KiB
Python
59 lines
2.8 KiB
Python
"""Provider logins (Codex browser, OpenRouter, PKCE plugins) share one loopback listener; an idle
|
|
browser preconnect accepted ahead of the redirect must not hold the authorization code."""
|
|
import socket
|
|
import threading
|
|
import time
|
|
from http.client import HTTPConnection
|
|
|
|
from hermes_cli.auth import AuthError
|
|
from hermes_cli.auth_device_flow import (
|
|
_bind_loopback_callback_server, _make_loopback_callback_handler, _serve_loopback_callback)
|
|
|
|
|
|
def test_idle_preconnect_does_not_delay_the_provider_login_code():
|
|
handler, result = _make_loopback_callback_handler("/callback", display_name="Probe")
|
|
server = _bind_loopback_callback_server("127.0.0.1", 0, handler, err=AuthError, bind_failed_code="bind")
|
|
port = server.server_address[1]
|
|
out: dict = {}
|
|
|
|
def run():
|
|
out["result"] = _serve_loopback_callback(server, result, timeout_seconds=30, err=AuthError, timeout_code="t")
|
|
out["returned_at"] = time.monotonic()
|
|
|
|
waiter = threading.Thread(target=run, daemon=True)
|
|
waiter.start()
|
|
idle = socket.create_connection(("127.0.0.1", port), timeout=5) # the browser's silent speculative socket
|
|
try:
|
|
# The accept queue is FIFO, so the serve loop takes the idle socket ahead of the redirect.
|
|
sent_at = time.monotonic()
|
|
conn = HTTPConnection("127.0.0.1", port, timeout=10)
|
|
conn.request("GET", "/callback?code=abc&state=xyz")
|
|
assert conn.getresponse().status == 200
|
|
conn.close()
|
|
waiter.join(timeout=8)
|
|
assert not waiter.is_alive(), "listener still parked on the idle preconnect"
|
|
assert out["result"]["code"] == "abc"
|
|
assert out["returned_at"] - sent_at < 5
|
|
finally:
|
|
idle.close()
|
|
|
|
|
|
def test_slow_client_still_delivers_the_provider_login_code():
|
|
"""A request line sent after 2.5 s (an SSH-tunnelled callback) is served, not dropped."""
|
|
handler, result = _make_loopback_callback_handler("/callback", display_name="Probe")
|
|
server = _bind_loopback_callback_server("127.0.0.1", 0, handler, err=AuthError, bind_failed_code="bind")
|
|
out: dict = {}
|
|
waiter = threading.Thread(target=lambda: out.update(result=_serve_loopback_callback(
|
|
server, result, timeout_seconds=30, err=AuthError, timeout_code="t")), daemon=True)
|
|
waiter.start()
|
|
try:
|
|
with socket.create_connection(server.server_address, timeout=10) as sock:
|
|
time.sleep(2.5) # health: allow HX013 -- the late request line IS the behaviour under test
|
|
sock.sendall(b"GET /callback?code=slow&state=s1 HTTP/1.0\r\nHost: localhost\r\n\r\n")
|
|
reply = sock.recv(8192)
|
|
assert reply.startswith(b"HTTP/1.0 200"), reply[:80]
|
|
finally:
|
|
waiter.join(timeout=10)
|
|
assert not waiter.is_alive(), "listener never returned the code"
|
|
assert (out["result"]["code"], out["result"]["state"]) == ("slow", "s1")
|