fix(mcp): prevent OOM from long-lived stream retention - #467
Draft
JayZeeDesign wants to merge 1 commit into
Draft
JayZeeDesign wants to merge 1 commit into
JayZeeDesign wants to merge 1 commit into
Conversation
Production OOM pattern: long-lived GET /mcp/ connections (Cursor, claude-code, opencode) hold SSE streams for hours, accumulating memory (~10 MiB/connection) until the 4 GiB limit kills the pod. Add MCPStreamLimitsMiddleware that enforces: - Per-instance concurrent connection limit (default 200) - Per-org concurrent connection limit (default 20) - Max connection lifetime (default 30 min, force reconnection) - Idle timeout (default 5 min, close inactive streams) - Prompt cleanup on client disconnect (499) - Structured logging with periodic metrics Configuration via TREG_MCP_* environment variables: - TREG_MCP_MAX_CONNECTIONS: per-instance limit - TREG_MCP_MAX_CONNECTIONS_PER_ORG: fair sharing across teams - TREG_MCP_MAX_LIFETIME_S: force reconnection after N seconds - TREG_MCP_IDLE_TIMEOUT_S: close after N seconds of inactivity Clients that implement MCP reconnection (all major ones do) will automatically reconnect when limits trigger. The 503 response includes Retry-After: 5. Includes 23 tests covering all limit types and cleanup scenarios. Co-authored-by: Jason Zhou <JayZeeDesign@users.noreply.github.com>
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 this does
Fixes production OOM caused by long-lived MCP stream retention. The incident pattern: GET
/mcp/connections from Cursor, claude-code, and opencode hold SSE streams open for minutes to hours, accumulating memory (~10 MiB worst-case per connection) until RSS spikes from ~1 GiB to ~3+ GiB and OOM kills the pod at 4 GiB.Adds
MCPStreamLimitsMiddlewarethat enforces connection lifecycle limits:Configuration
New environment variables in
config.py:TREG_MCP_MAX_CONNECTIONSTREG_MCP_MAX_CONNECTIONS_PER_ORGTREG_MCP_MAX_LIFETIME_STREG_MCP_IDLE_TIMEOUT_SWhy these defaults
How it was tested
Verification in production
After deploy:
mcp_stream_metricsentries — should show reasonable connection countsmcp_connection_rejectedlogs — indicates limits are workingRollback
Set very high limits to effectively disable:
Checklist
uv run --with pytest-xdist pytest -n auto -qpasses locally (ran subset; full suite in CI)test_mcp_limits.py)docs/context/fragment (no existing MCP fragment).envvalues)