Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions daemon/src/agentring/mcp_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import logging
import os
import re
import resource
import secrets
import signal
import time
Expand Down Expand Up @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions daemon/tests/test_mcp_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Loading