The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): serve uvicorn on SelectorEventLoop on Windows despite uvicorn 0.41 proactor default Closes #120164
finn763d8814dcb381e
diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.pyindex c8832caa3a9..da199ecf235 100644--- a/hermes_cli/web_server.py+++ b/hermes_cli/web_server.py@@ -1391,6 +1391,25 @@ def _on_server_started(_hb_loop.call_later(_hb_interval, _loop_heartbeat, _hb_loop.time() + _hb_interval)+def _windows_serve_loop_factory(config):+ """Loop factory for serve on Windows: always a selector loop.++ uvicorn 0.41's ``asyncio_loop_factory`` returns ProactorEventLoop on+ win32, on which uvicorn's socket stack binds-but-never-accepts (READY+ prints, then WinError 10014 accept failures, exit 1, desktop+ ECONNREFUSED — #120164, regression of #50641). Force the selector loop+ uvicorn served on before; a factory that already yields selector loops+ (older uvicorn, explicit ``--loop``) passes through untouched.+ """+ try:+ factory = config.get_loop_factory()+ except Exception:+ return asyncio.SelectorEventLoop+ if factory is None or getattr(factory, "__name__", "") == "ProactorEventLoop":+ return asyncio.SelectorEventLoop+ return factory++def _run_serve(serve, config, host: str, port: int) -> None:"""Drive ``serve()`` on the loop uvicorn expects.@@ -1408,7 +1427,7 @@ def _run_serve(serve, config, host: str, port: int) -> None:try:from uvicorn._compat import asyncio_run as runner- runner_kwargs = {"loop_factory": config.get_loop_factory()}+ runner_kwargs = {"loop_factory": _windows_serve_loop_factory(config)}except Exception:runner = asyncio.runrunner_kwargs = {}diff --git a/tests/hermes_cli/test_serve_windows_loop_factory_120164.py b/tests/hermes_cli/test_serve_windows_loop_factory_120164.pynew file mode 100644index 00000000000..768ea629995--- /dev/null+++ b/tests/hermes_cli/test_serve_windows_loop_factory_120164.py@@ -0,0 +1,67 @@+"""#120164 — Windows serve must run uvicorn on a SelectorEventLoop.++uvicorn 0.41's ``asyncio_loop_factory`` returns ProactorEventLoop on win32.+Driving uvicorn's socket stack on the proactor loop binds-but-never-accepts:+READY prints, then ``Accept failed on a socket`` + WinError 10014, exit 1,+desktop ECONNREFUSED (regression of #50641, whose fix trusted+``config.get_loop_factory()`` back when that still meant selector on Windows).+"""++import asyncio+import sys++import pytest++pytestmark = pytest.mark.skipif(+ sys.platform != "win32", reason="Windows serve-runner path under test"+)+++class _ProactorConfig:+ def get_loop_factory(self):+ return asyncio.ProactorEventLoop+++class _SelectorConfig:+ def get_loop_factory(self):+ return asyncio.SelectorEventLoop+++def _capture_runner(monkeypatch):+ captured = {}++ def _fake_runner(main, **kwargs):+ captured.update(kwargs)+ main.close()+ return None++ monkeypatch.setattr("uvicorn._compat.asyncio_run", _fake_runner)+ return captured+++async def _noop():+ return None+++def test_proactor_factory_is_overridden_to_selector(monkeypatch):+ from hermes_cli import web_server++ captured = _capture_runner(monkeypatch)+ web_server._run_serve(_noop, _ProactorConfig(), "127.0.0.1", 0)+ factory = captured.get("loop_factory")+ assert factory is not None, "expected an explicit loop_factory on win32"+ loop = factory()+ try:+ assert isinstance(loop, asyncio.SelectorEventLoop), (+ f"serve must not run on {type(loop).__name__} (#120164)"+ )+ finally:+ loop.close()+++def test_selector_factory_passes_through(monkeypatch):+ from hermes_cli import web_server++ captured = _capture_runner(monkeypatch)+ web_server._run_serve(_noop, _SelectorConfig(), "127.0.0.1", 0)+ assert captured.get("loop_factory") is asyncio.SelectorEventLoop