Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(proxy): use selector loop on Windows (#1496)

weijie_chen17c7347402fa
diff --git a/headroom/proxy/server.py b/headroom/proxy/server.py
index 87e378297..34f6fa126 100644
--- a/headroom/proxy/server.py
+++ b/headroom/proxy/server.py
@@ -4200,6 +4200,11 @@ def run_server(
app_target: Any
uvicorn_kwargs: dict[str, Any] = {}
+ if sys.platform == "win32":
+ # ProactorEventLoop can close the listening socket on transient
+ # AcceptEx failures (for example WinError 64 from keep-alive RSTs).
+ # The selector loop keeps accept errors scoped to the connection.
+ uvicorn_kwargs["loop"] = "asyncio:SelectorEventLoop"
if workers > 1:
# CompressionCache and PrefixTracker are always per-worker instance vars.
# Python CompressionStore defaults to InMemoryBackend (per-process), so
diff --git a/tests/test_proxy_scalability.py b/tests/test_proxy_scalability.py
index effa361a0..864386f7e 100644
--- a/tests/test_proxy_scalability.py
+++ b/tests/test_proxy_scalability.py
@@ -276,3 +276,39 @@ class TestWorkerConfiguration:
# value and re-restores it on teardown, leaking the config into
# later tests (e.g. _proxy_config_from_env then ignores HEADROOM_*).
os.environ.pop(_MULTI_WORKER_CONFIG_ENV, None)
+
+ def test_run_server_uses_selector_loop_on_windows(self, monkeypatch):
+ from headroom.proxy import server as server_mod
+ from headroom.proxy.models import ProxyConfig
+
+ captured = {}
+
+ def fake_run(app, **kwargs):
+ captured["app"] = app
+ captured["kwargs"] = kwargs
+
+ monkeypatch.setattr(server_mod.sys, "platform", "win32")
+ monkeypatch.setattr(server_mod, "create_app", lambda config: "app")
+
+ with patch("headroom.proxy.server.uvicorn.run", fake_run):
+ server_mod.run_server(ProxyConfig(), print_banner=False)
+
+ assert captured["app"] == "app"
+ assert captured["kwargs"]["loop"] == "asyncio:SelectorEventLoop"
+
+ def test_run_server_keeps_default_loop_off_windows(self, monkeypatch):
+ from headroom.proxy import server as server_mod
+ from headroom.proxy.models import ProxyConfig
+
+ captured = {}
+
+ def fake_run(app, **kwargs):
+ captured["kwargs"] = kwargs
+
+ monkeypatch.setattr(server_mod.sys, "platform", "linux")
+ monkeypatch.setattr(server_mod, "create_app", lambda config: "app")
+
+ with patch("headroom.proxy.server.uvicorn.run", fake_run):
+ server_mod.run_server(ProxyConfig(), print_banner=False)
+
+ assert "loop" not in captured["kwargs"]