Skip to content

fix: size the Active Record pool from Puma's thread budget - #45

Merged
artempartos merged 1 commit into
developfrom
fix/mcp-db-pool-oversubscribed
Aug 7, 2026
Merged

artempartos merged 1 commit into
developfrom
fix/mcp-db-pool-oversubscribed

Conversation

@artempartos

@artempartos artempartos commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

What broke

MCPController#handle was raising ActiveRecord::ConnectionTimeoutError in production:

could not obtain a connection from the pool within 5.000 seconds (waited 5.007 seconds)

Not a leak. The pool was structurally smaller than the number of request threads competing for it.

value source
MCP Puma threads 10 config/puma_mcp.rb — private MCP_MAX_THREADS default
Active Record pool 5 config/settings.yml — RAILS_MAX_THREADS or 5, and that variable is set in no environment
checkout timeout 5s (default) matches the waited 5.007 seconds in the trace

Five of the ten threads could never hold a connection and died on the checkout timeout. config/puma.rb had the same 10-vs-5 mismatch; the MCP endpoint just surfaced it first, because agent containers drive it far more concurrently than the web workload.

The fix

  • MCP_MAX_THREADS now falls back to RAILS_MAX_THREADS instead of its own default, so one knob drives both the thread count and the pool and the two cannot drift apart.
  • The pool adds headroom on top of that budget for checkouts that are not request-bound — an equal pool leaves none. DB_POOL overrides outright.
  • MCPRequestHandler memoizes the session entitlement. A tools/call resolved it twice (the unavailable-tool shim, then the server build), and holding a pooled connection for a duplicate query is what tips a busy MCP pod over the edge.
  • docker-compose.yml sets the variable so dev matches: locally the web container also ran 10 Puma threads against a pool of 5.

Deploy order

This needs its infra counterpart — palad-ai/aixle-infra#23 sets RAILS_MAX_THREADS on the web and mcp workloads. Effect of each half:

pool threads
today 5 10 5 threads starve
infra only, current image 10 10 starvation gone, no headroom
this PR only 8 10 still 2 short
both 13 10 correct

The infra change can ship first and already stops the incident — under the current image the old formula reads RAILS_MAX_THREADS directly, so it yields a pool of 10 against 10 threads without waiting for an image build.

Connection budget at full autoscale (web 6 + mcp 6 replicas) is ~156 against the ~450 max_connections of db.t4g.medium. The Temporal worker is deliberately untouched — it derives its own activity budget from the same variable, and the infra change keeps the variable off the shared configMap so worker concurrency does not change.

Testing

docker compose exec -T web make check_all — green (rails-test, rubocop, brakeman, system-test, eslint, typescript, fe-test, vite-build). Existing test/integration/mcp_controller_test.rb covers both memoized paths: the shim (entitled-but-unavailable) and the server build (available).

No regression test added: the drift was between this repo and the infra repo, and a test here cannot see helm values. The structural guard is the single env key now feeding both threads and pool.

🤖 Generated with Claude Code

MCPController#handle was raising ActiveRecord::ConnectionTimeoutError in
production. Not a leak: the pool was structurally smaller than the number
of request threads competing for it.

config/settings.yml sized the pool as `RAILS_MAX_THREADS || 5`, and that
variable was set nowhere in the deployment, so the pool was 5. Both Puma
configs meanwhile defaulted to 10 threads — config/puma_mcp.rb through its
own private MCP_MAX_THREADS default, which could drift from the pool even
when RAILS_MAX_THREADS was set. Five of the ten threads could never hold a
connection and died on the 5s checkout timeout instead.

Now MCP_MAX_THREADS falls back to RAILS_MAX_THREADS, so one knob drives
both the thread count and the pool, and the pool adds headroom on top of
it for checkouts that aren't request-bound. DB_POOL overrides outright.

Also memoizes the session entitlement in MCPRequestHandler: a tools/call
resolved it twice (the unavailable-tool shim, then the server build), and
holding a pooled connection for a duplicate query is what tips a busy MCP
pod over the edge.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Signed-off-by: Artem Petrov <[email protected]>
@artempartos
artempartos merged commit c8675d1 into develop Aug 7, 2026
6 checks passed
alex-astafyev pushed a commit to alex-astafyev/flow that referenced this pull request Aug 19, 2026
* origin/develop:
  fix(sessions): stop losing session outputs with long file names (AixleHQ#47)
  chore(bmad): bump BMAD Method to 6.10.0 (AixleHQ#46)
  fix: size the Active Record pool from Puma's thread budget (AixleHQ#45)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant