From 92afed715a8d73b2c894113e14cb50e4f163e53f Mon Sep 17 00:00:00 2001 From: victorcavero14 Date: Fri, 27 Nov 2026 19:23:49 +0100 Subject: [PATCH] mcp-http: raise the soft open-files limit at startup A burst of connections, tenant stores and live-call rooms can exhaust a container's default soft limit (often 1024): accept() then fails with EMFILE and every new LiveKit connection with 'no native root CA certificates found'. Raise the soft limit to the hard one (cap 65536) and log it. Co-Authored-By: Claude Sonnet 5.5 --- daemon/src/agentring/mcp_http.py | 21 +++++++++++++++++++++ daemon/tests/test_mcp_http.py | 14 ++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/daemon/src/agentring/mcp_http.py b/daemon/src/agentring/mcp_http.py index 4a77f55..4cd55e1 100644 --- a/daemon/src/agentring/mcp_http.py +++ b/daemon/src/agentring/mcp_http.py @@ -23,6 +23,7 @@ import logging import os import re +import resource import secrets import signal import time @@ -1033,11 +1034,31 @@ def _describe(m: dict[str, Any]) -> str: return str(m.get("method")) +def raise_nofile_limit(cap: int = 65536) -> int | None: + """Raises this process's soft open-files limit to the hard one (at most `cap`). Every + connection, SQLite tenant store (db + wal + shm), relay client and live call room holds + descriptors; a container's default soft limit (often 1024) is hit in a burst, and then + accept() and every new LiveKit connection fail with EMFILE. Returns the limit now in + force, or None when it can't be read (no `resource` on this platform).""" + try: + soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) + want = cap if hard == resource.RLIM_INFINITY else min(hard, cap) + if soft != resource.RLIM_INFINITY and soft < want: + resource.setrlimit(resource.RLIMIT_NOFILE, (want, hard)) + soft = want + return None if soft == resource.RLIM_INFINITY else soft + except (ValueError, OSError): + return None + + def run_http(cfg: HttpConfig) -> None: async def main() -> None: loop = asyncio.get_running_loop() # Relay requests run in threads (sync httpx); many tenants waiting at once need more than the default. loop.set_default_executor(ThreadPoolExecutor(max_workers=64, thread_name_prefix="agentring-mcp")) + limit = raise_nofile_limit() + if limit: + log.info("open-file limit: %d", limit) stop = asyncio.Event() for sig in (signal.SIGINT, signal.SIGTERM): try: diff --git a/daemon/tests/test_mcp_http.py b/daemon/tests/test_mcp_http.py index 13f1e26..fffe78e 100644 --- a/daemon/tests/test_mcp_http.py +++ b/daemon/tests/test_mcp_http.py @@ -907,3 +907,17 @@ def answer(req: httpx.Request) -> httpx.Response: r = KeyResolver(hub_cfg(tmp_path), transport=httpx.MockTransport(answer)) with pytest.raises(UpstreamError): asyncio.run(r.resolve("ak_x")) + + +def test_raise_nofile_limit_lifts_the_soft_limit_to_the_hard_one(): + import resource + soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) + try: + resource.setrlimit(resource.RLIMIT_NOFILE, (256, hard)) + got = mcp_http.raise_nofile_limit(cap=4096) + want = 4096 if hard == resource.RLIM_INFINITY else min(hard, 4096) + assert got == want + assert resource.getrlimit(resource.RLIMIT_NOFILE)[0] == want + assert mcp_http.raise_nofile_limit(cap=100) == want # never lowers + finally: + resource.setrlimit(resource.RLIMIT_NOFILE, (soft, hard))