From 0d38b3950c55337c0b37833776063f008986197e Mon Sep 17 00:00:00 2001 From: Dasemu Date: Mon, 19 Oct 2026 05:24:05 +0200 Subject: [PATCH] docs: document manager memory safeguards and wire them into the image Co-Authored-By: Claude Sonnet 5.5 --- .env.example | 8 ++++++++ CLAUDE.md | 3 +++ Dockerfile | 3 +++ LESSONS.md | 15 +++++++++++++++ README.md | 2 +- docker-compose.yml | 3 +++ docs/operations.md | 30 ++++++++++++++++++++++++++++++ 7 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 docs/operations.md diff --git a/.env.example b/.env.example index 9f86684..ce13f74 100644 --- a/.env.example +++ b/.env.example @@ -32,6 +32,14 @@ MASTER_KEY=change-me-to-a-random-string-of-at-least-32-characters CONTAINER_MEMORY_MB=512 CONTAINER_CPUS=1 +# Safeguards against a stuck upstream or a memory leak taking the host down (see docs/operations.md). +# Inactivity timeout (ms) on proxied MCP requests/streams +PROXY_TIMEOUT_MS=300000 +# Max simultaneous proxied requests; beyond this the proxy answers 503 +MAX_PROXY_REQUESTS=100 +# The manager exits (and is restarted by compose) when its heap + external memory passes this. 0 disables. +MANAGER_MEMORY_LIMIT_MB=512 + # Absolute path to an SSH dir (key + known_hosts) with clone access to the repos you'll # install, mounted read-only into the manager container so `git clone ssh://...` works for # private repos. Required only if you install from private/SSH repo URLs. diff --git a/CLAUDE.md b/CLAUDE.md index e81c779..2f6c4f8 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -36,4 +36,7 @@ authenticated requests to it. See `README.md` for the full flow and `docs/` for ## Gotchas - Builds run one at a time; no per-installation resource override (see README "Known limitations"). +- The manager enforces its own memory/socket limits (`docs/operations.md`); keep every new long-lived resource bounded. +- Local Node 24 cannot compile `better-sqlite3@11`, so `npm test` fails on the host: run the suite in a + `node:20` container (`podman run ... docker.io/library/node:20-slim`, repo copied without `node_modules`). - `.gitea/workflows/ci.yml` only runs if this repo's Gitea instance has Actions enabled with a runner. diff --git a/Dockerfile b/Dockerfile index 389015f..a88dd6b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -41,5 +41,8 @@ COPY --from=build /repo/apps/web/dist apps/web/dist WORKDIR /repo/apps/server ENV NODE_ENV=production +# Hard V8 heap ceiling, above MANAGER_MEMORY_LIMIT_MB (the soft limit the app enforces itself): +# a leak ends in a crash + restart by the compose restart policy instead of swapping the host. +ENV NODE_OPTIONS=--max-old-space-size=768 EXPOSE 3000 CMD ["node", "dist/index.js"] diff --git a/LESSONS.md b/LESSONS.md index 216077c..cd58180 100644 --- a/LESSONS.md +++ b/LESSONS.md @@ -115,3 +115,18 @@ A short image name wasn't enough to remove an image via the Docker-compatible so Mounting only `apps/server` into a test container produced `node_modules not found` — the hoisted root `node_modules` wasn't mounted. **Avoid it:** mount the whole repo root when testing a workspace package, not just its subdirectory. + +## The manager itself needs hard limits — it shares the host with every MCP + +The manager ran with no timeouts, no concurrency cap, no heap ceiling and `MemoryStore` for +sessions. When it started to grow (~1.1 GiB of RAM + swap) and Docker slowed under the +pressure, every proxied request kept waiting on `docker inspect` and held its socket, ~70 +connections piled up on port 3000, the 3 GiB LXC swapped, and even `docker ps` hung — taking +every MCP down with it. The root cause of the growth was not proven (a feedback loop of +unbounded in-flight requests is the prime suspect); what was fixed is that nothing can grow +unbounded any more. See `docs/operations.md`. + +**Avoid it:** any long-lived or per-request resource (sockets, sessions, queues, in-flight +subprocesses) needs an explicit bound or timeout. Don't rely on a cgroup limit for the manager +on the OpenRC LXC (see the cgroup lesson above) — enforce limits inside the process. Local Node +24 cannot compile `better-sqlite3@11`, so run the test suite in a `node:20` container. diff --git a/README.md b/README.md index 5495167..f1d407c 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ a runner. - Every installed container gets the same memory/CPU caps (`CONTAINER_MEMORY_MB`, `CONTAINER_CPUS`), no per-installation override yet. - No multi-user support — single operator only. -- Builds run one at a time. +- Builds run one at a time (queued in the manager; a build is killed after 30 minutes). See `LESSONS.md` for operational gotchas found while building and running this (Nixpacks runtime-version mismatches, Podman networking, SELinux, etc.). diff --git a/docker-compose.yml b/docker-compose.yml index a798e34..33b9860 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,6 +15,9 @@ services: MASTER_KEY: ${MASTER_KEY} CONTAINER_MEMORY_MB: ${CONTAINER_MEMORY_MB:-512} CONTAINER_CPUS: ${CONTAINER_CPUS:-1} + PROXY_TIMEOUT_MS: ${PROXY_TIMEOUT_MS:-300000} + MAX_PROXY_REQUESTS: ${MAX_PROXY_REQUESTS:-100} + MANAGER_MEMORY_LIMIT_MB: ${MANAGER_MEMORY_LIMIT_MB:-512} volumes: - /var/run/docker.sock:/var/run/docker.sock - ./data:/repo/data diff --git a/docs/operations.md b/docs/operations.md new file mode 100644 index 0000000..be5e71c --- /dev/null +++ b/docs/operations.md @@ -0,0 +1,30 @@ +# Operations + +## Memory and resource safeguards + +The manager runs on the same (often small) host as every MCP container, so a runaway manager +degrades all of them. After an incident where the manager's Node process grew to ~1.1 GiB +(RAM + swap), held ~70 stuck connections to port 3000 and pushed the host into swap, these +limits exist: + +| Safeguard | Where | Behaviour | +|---|---|---| +| Proxy inactivity timeout | `PROXY_TIMEOUT_MS` (default 300000) | A stalled upstream or vanished client is cut after this much silence, freeing its sockets and buffers. | +| Concurrent proxy cap | `MAX_PROXY_REQUESTS` (default 100) | Beyond it `/mcp/` answers `503` + `Retry-After` instead of queueing without bound. | +| Docker call timeout + IP cache | `docker/containers.ts` | `docker inspect` times out after 10 s and the container IP is cached for 5 s, so a wedged Docker daemon cannot stall every request at once. | +| Session store | `session/sqliteStore.ts` | Sessions live in the SQLite DB and expired ones are purged every 10 min. Replaces `MemoryStore`, which never evicts and warned it leaks in production. | +| Memory guard | `MANAGER_MEMORY_LIMIT_MB` (default 512, 0 = off) | Checked every 30 s on heap + external memory (not RSS, which hides swapped pages). When exceeded the process exits and `restart: unless-stopped` starts a fresh one. | +| V8 heap ceiling | `NODE_OPTIONS=--max-old-space-size=768` in the `Dockerfile` | Hard backstop above the soft limit. | +| Serialized builds | `service/orchestrator.ts` | One Nixpacks/Docker build at a time; git is killed after 5 min and a build after 30 min. | + +A container-level cgroup limit on the manager (`mem_limit` in compose) would be the strongest +guard, but it fails outright on hosts that do not delegate cgroup controllers (see +`LESSONS.md`), so the limits above are enforced inside the process instead. Add `mem_limit` +yourself on hosts that support it. + +## Checking on it + +`GET /health` (unauthenticated) returns `ok`, `uptimeSec`, `rssMb`, `heapUsedMb` and +`activeProxyRequests`. A heap that keeps climbing between restarts, or an +`activeProxyRequests` that stays high with no traffic, points at a leak. A line +`memory guard: ... restarting` in the manager's log means the guard fired.