Repository navigation
fix: size the Active Record pool from Puma's thread budget - #45
Merged
Merged
Conversation
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]>
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What broke
MCPController#handlewas raisingActiveRecord::ConnectionTimeoutErrorin production:Not a leak. The pool was structurally smaller than the number of request threads competing for it.
config/puma_mcp.rb— privateMCP_MAX_THREADSdefaultconfig/settings.yml—RAILS_MAX_THREADSor 5, and that variable is set in no environmentwaited 5.007 secondsin the traceFive of the ten threads could never hold a connection and died on the checkout timeout.
config/puma.rbhad 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_THREADSnow falls back toRAILS_MAX_THREADSinstead of its own default, so one knob drives both the thread count and the pool and the two cannot drift apart.DB_POOLoverrides outright.MCPRequestHandlermemoizes the session entitlement. Atools/callresolved 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.ymlsets 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_THREADSon thewebandmcpworkloads. Effect of each half:The infra change can ship first and already stops the incident — under the current image the old formula reads
RAILS_MAX_THREADSdirectly, 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_connectionsofdb.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). Existingtest/integration/mcp_controller_test.rbcovers 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